Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unknown option swallows a known one #11

Closed
mfncooper opened this issue Dec 17, 2011 · 4 comments
Closed

Unknown option swallows a known one #11

mfncooper opened this issue Dec 17, 2011 · 4 comments

Comments

@mfncooper
Copy link

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.

@isaacs
Copy link
Contributor

isaacs commented Dec 17, 2011

I agree, that would be better. What about this case?

my-program --unknown1 --unknown2

Should that be:

{ unknown1: true
, unknown2: true
}

or

{ unknown1: "unknown2" }

Though it's a bit trickier, I'm tempted to say that the first makes more sense. Ie, an unknown option will not ever take a - prefixed thing as its argument. (If you want to make sure that it takes an argument, you can define it to do so.)

Thoughts?

@mfncooper
Copy link
Author

Yes, that would make sense to me; that if you need to handle a -- prefixed value, you need to define the option, and otherwise any -- prefix introduces a new option.

@sbrandwoo
Copy link

I've just been bitten by this issue when using Grunt. Any options that Grunt itself doesn't know about are made available to the various tasks, meaning that there are a lot of unknown options being used in the system. As a result of this issue you can only use one --flag and it must be the last argument of the command.

I think the general solution of assuming that unknown arguments won't be prefixed with -- is a good one.

@isaacs
Copy link
Contributor

isaacs commented Jan 17, 2013

Currently in nopt, any unknown argument must have a = to be assigned a value, otherwise it will be assumed to be a boolean. This is no longer an issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants