GithubHelp home page GithubHelp logo

Comments (22)

roman-vanesyan avatar roman-vanesyan commented on May 21, 2024 2

Okay, I'll investigate

from commander.js.

danmilon avatar danmilon commented on May 21, 2024

Bumped on this also.
Here: 672c7d0#L11R188

if ('-' == arg[0]) return this.optionMissingArgument(option, arg)

It throws because it treads the negative number as another command line argument.

@visionmedia How should this be fixed?

from commander.js.

tj avatar tj commented on May 21, 2024

we could assume digits cannot be flags. personally I've never used -5 etc, but some people may

from commander.js.

motin avatar motin commented on May 21, 2024

Or you can (at least in the interim) detect if the value is quoted. At least --value='-10' ought to work (does not today)

from commander.js.

danmilon avatar danmilon commented on May 21, 2024

They need to be escaped

$ node -p --eval 'process.argv' script --value='-10' --value2=\'-10\'
[ 'node', 'script', '--value=-10', '--value2=\'-10\'' ]

from commander.js.

dresende avatar dresende commented on May 21, 2024

I don't think this is necessary. If the param is a defined flag, it's ok (even if it's a number). If the previous param requires a value then use it like a value. If you get to the point where you use a flag (like -5) in a value-required parameter, just swap the positions of the parameters.

from commander.js.

davidcl64 avatar davidcl64 commented on May 21, 2024

I've gotten around this locally by adding the following:

// see http://stackoverflow.com/questions/18082/validate-numbers-in-javascript-isnumeric
function isNumber(n) {
  return !isNaN(parseFloat(n)) && isFinite(n);
}

and adding the following checks:

in Command.prototype.normalize:

if (arg.length > 1 && '-' == arg[0] && '-' != arg[1] && !isNumber(arg) ) {

in Command.prototype.parseOptions:

if ('-' == arg[0] && !isNumber(arg)) return this.optionMissingArgument(option, arg);

and:

if (argv[i+1] && ('-' != argv[i+1][0] || isNumber(argv[i+1]))) {

from commander.js.

BrandonE avatar BrandonE commented on May 21, 2024

I have encountered an edge case for the provided solution.

node something.js --value=-10

It is valid for an equal sign to be provided. Because the minus is not in the first position of the argument, this fix won't work. Further:

node something.js --value='-Minus'

String values that start with a leading minus fail as well.

from commander.js.

lucasfcosta avatar lucasfcosta commented on May 21, 2024

Is this going to be fixed?

from commander.js.

Christilut avatar Christilut commented on May 21, 2024

Also encountered this issue, working around it by doing: -t \'-123\' which results in the value being '-123' so after removing the first and last quote, its all good.

from commander.js.

shotamatsuda avatar shotamatsuda commented on May 21, 2024

Escaping a leading space also seems to work around this issue with less side effects: -t \ -123

from commander.js.

roman-vanesyan avatar roman-vanesyan commented on May 21, 2024

You can just simply pass the value in quotes and should works as expected. Imho, it's bad idea to assume digits as value, rather than flags, some of linux programs use digits as flags.

from commander.js.

roman-vanesyan avatar roman-vanesyan commented on May 21, 2024

Closed, answered. Feel free to reopen.

from commander.js.

kachkaev avatar kachkaev commented on May 21, 2024

--value \ -42 works indeed, but neither of --value=-42, --value="-42" or --value='-42' do. The options that do not work at the moment look more intuitive so probably need to be implemented at some point. Maybe PR #583 will help?

from commander.js.

roman-vanesyan avatar roman-vanesyan commented on May 21, 2024

you can run --value '-42' and it should perfectly work.

from commander.js.

kachkaev avatar kachkaev commented on May 21, 2024

Looks like it does not both for --value '-42' and --value "-42"

[email protected]

from commander.js.

DavyKoravand avatar DavyKoravand commented on May 21, 2024

Is this 2 year old issue going to be fixed? Many command-line interfaces use negative numbers as arguments.

from commander.js.

the-townsend avatar the-townsend commented on May 21, 2024

Hi
We're really pleased with commander.js and are using in our project - Thank you! It would be great if we could hand it negative numbers if someone has the capacity to make that change; our project has a messy work around at the moment. Thanks again.

from commander.js.

ecruzado avatar ecruzado commented on May 21, 2024

any updates on this issue? there is a timeline to fix it?

from commander.js.

shadowspawn avatar shadowspawn commented on May 21, 2024

The current behaviour is that an option with a required value takes the following argument, whether or not it starts with a dash, so works with negative numbers.

Hopefully this covers your use cases @Simplonium @the-townsend @ecruzado ?

const program = require("commander");

program
.option("-n, --number <value>");

program.parse(process.argv);
console.log(program.number);
$ node index.js 
undefined
$ node index.js -n 1
1
$ node index.js -n -2
-2
$ node index.js --number=3
3
$ node index.js --number=-4
-4

Limitation: negative numbers are not recognised for options with a value which is only optional (e.g. -n, --number [value]), but I suspect this form gets used accidentally more often than by design. The complication with making that case work with negative numbers is that it would conflict with digits as flags as @vanesyan noted earlier.

from commander.js.

the-townsend avatar the-townsend commented on May 21, 2024

from commander.js.

shadowspawn avatar shadowspawn commented on May 21, 2024

An answer was provided, and no further activity in a month. Closing this as resolved.

Feel free to open a new issue if it comes up again, with new information and renewed interest.

from commander.js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.