Commander.js: Accept negative number values

Created on 1 Jun 2012  Â·  22Comments  Â·  Source: tj/commander.js

I noticed that negative values are seen as possible options because of the minus sign. What I'm trying to do:

node something.js --value -10

Please note that --value is defined as --value <n> so it should accept it, I think.

bug

Most helpful comment

Okay, I'll investigate

All 22 comments

Bumped on this also.
Here: https://github.com/visionmedia/commander.js/commit/672c7d01d8382257226d67c39c6e1002c881d95f#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?

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

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

They need to be escaped

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

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.

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]))) {

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.

Is this going to be fixed?

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.

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

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.

Closed, answered. Feel free to reopen.

--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 https://github.com/tj/commander.js/pull/583 will help?

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

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

[email protected]

Okay, I'll investigate

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

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.

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

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.

Thanks for this John, I’ll take a look.


From: John Gee notifications@github.com
Sent: Thursday, April 25, 2019 9:49:37 PM
To: tj/commander.js
Cc: Christopher Townsend; Mention
Subject: Re: [tj/commander.js] Accept negative number values (#61)

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 @Simploniumhttps://github.com/Simplonium @the-townsendhttps://github.com/the-townsend @ecruzadohttps://github.com/ecruzado ?

const program = require("commander");

program
.option("-n, --number ");

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 @vanesyanhttps://github.com/vanesyan noted earlier.

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHubhttps://github.com/tj/commander.js/issues/61#issuecomment-486832703, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AGRUBYOFPNCYPXGICU7TPQDPSIKODANCNFSM4AADX6HQ.

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

goloroden picture goloroden  Â·  15Comments

develmts picture develmts  Â·  22Comments

ghost picture ghost  Â·  17Comments

open-source-explorer picture open-source-explorer  Â·  17Comments

sr258 picture sr258  Â·  14Comments