Comments (22)
Okay, I'll investigate
from commander.js.
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.
we could assume digits cannot be flags. personally I've never used -5
etc, but some people may
from commander.js.
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.
They need to be escaped
$ node -p --eval 'process.argv' script --value='-10' --value2=\'-10\'
[ 'node', 'script', '--value=-10', '--value2=\'-10\'' ]
from commander.js.
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.
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.
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.
Is this going to be fixed?
from commander.js.
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.
Escaping a leading space also seems to work around this issue with less side effects: -t \ -123
from commander.js.
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.
Closed, answered. Feel free to reopen.
from commander.js.
--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.
you can run --value '-42'
and it should perfectly work.
from commander.js.
Looks like it does not both for --value '-42'
and --value "-42"
from commander.js.
Is this 2 year old issue going to be fixed? Many command-line interfaces use negative numbers as arguments.
from commander.js.
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.
any updates on this issue? there is a timeline to fix it?
from commander.js.
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.
from commander.js.
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)
- Double Dash Feature HOT 5
- Missing `this` type for `action` HOT 5
- Hey ! My project got crashed.. please check description for more detail HOT 2
- Feature/multiple usages HOT 14
- The root `--help` doesn't respect `.usage()` in a command HOT 2
- [Bug] Unexpected arguments ignored HOT 2
- Commander v12 breaks implicit negative flag. HOT 1
- Dev: purging stale release branches HOT 1
- 115 HOT 1
- Add support for option to output object HOT 8
- Handle the case when node called with the "-e" argument HOT 4
- Compatible with deno HOT 2
- Neutral JavaScript runtime support (Deno, Bun, etc) HOT 11
- Allow option to conflict with the negation of another option. HOT 5
- When use .command("dtsgen <files>"),this params files in macOS and Windows is errors caused by differences HOT 3
- Requiring via `<` does not work. HOT 4
- how to use the ESM module to import and export ? HOT 1
- Option.fullDescription() does not exist HOT 2
- Is it possible for an option to conflict with an argument? HOT 5
- Usage with Bun HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from commander.js.