isaacs/nopt — Issue #11: Unknown option swallows a known one
Author: mfncooper
Created: 2011-12-17 02:19:26+00:00
State: open

Here's an example of everything working properly:

$ ./my-program.js --flag --foo hello
parsed =
{ flag: true,
foo: 'hello',
argv:
{ remain: [],
cooked: [ '--flag', '--foo', 'hello' ],
original: [ '--flag', '--foo', 'hello' ],
toString: [Function] } }

So far, so good. However, a mistaken name for the boolean option throws everything off, so if --myflag was mistakenly used instead of --flag, we get this:

$ ./my-program.js --myflag --foo hello
parsed =
{ myflag: '--foo',
argv:
{ remain: [ 'hello' ],
cooked: [ '--myflag', '--foo', 'hello' ],
original: [ '--myflag', '--foo', 'hello' ],
toString: [Function] } }

Note how the mistyped 'myflag' option has now consumed the --foo option as its value, leaving what was intended as foo's value as part of 'remain'.

Since it can't be determined whether an unknown option is intended to have a value or not, and since nopt is designed to allow for the use of unknown options, it would make sense to check a possible value for an unknown option to see if it may, in fact, be the name of a known option. In the above example, this would mean that 'foo' would be correctly identified as an option name rather than being consumed as a value.