-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Description
I am using commander 2.9.0 (graceful-readlink 1.0.1) on node v4.4.7 (on Windows, running in MSYS2, if it matters).
I run the following test program:
var commander = require("commander")
commander
.version("0.0.1")
.option('-x, --x-flag', 'A flag')
.arguments("<file>")
.parse(process.argv)
if (commander.xFlag)
console.log("Got -x")
console.log("Args:", commander.args)
I find that if I do not supply the <file> argument, if I pass a nonsense flag like --nonsense, I will get a nice error message. If I do supply the <file> argument, then any nonsense flags included will be silently discarded. Moreover, commander appears to assume each nonsense flag takes one option-argument, and it silently discards that, too. These flags are not appended to commander.args or anything. They just disappear.
A demonstration:
$ node testcommander.js
Args: []
$ node testcommander.js -x
Got -x
Args: []
$ node testcommander.js --nonsense -x
error: unknown option `--nonsense'
$ node testcommander.js -x 1 2 3
Got -x
Args: [ '1', '2', '3' ]
$ node testcommander.js --NONSENSE 1 2 3
Args: [ '2', '3' ]
$ node testcommander.js 1 2 3 --NON-SENSE
Args: [ '1', '2', '3' ]
$ node testcommander.js 1 2 3 --NON-SENSE QQDFFR -x
Got -x
Args: [ '1', '2', '3' ]
Expected behavior: A --flag which is not recognized should result in commander printing an error.
This is a pretty bad bug. Not only does commander allow something which should be an error, and not only does it do it inconsistently for the same script depending on input, but since commander does this totally silently I cannot even write my own recovery code. If the lost arguments had been appended to commander.args at least I could detect it and raise my own error.