nopt icon indicating copy to clipboard operation
nopt copied to clipboard

[BUG] Unexpected arguments not parsed correctly?

Open cowboy opened this issue 12 years ago • 3 comments

I've noticed a behavior change from [email protected] -> [email protected] with the parsing of unexpected arguments.

  • In [email protected] '-a a' and '-a=a' (as well as with --) were equivalent.
  • In [email protected] '-a a' and '-a=a' (as well as with --) behave differently. Basically, -a a works like -a (setting prop a to true) but adds "a" the the remain array. Shouldn't both set prop a to string "a"?
$ npm install nopt@~1.0.10
npm WARN package.json [email protected] No README.md file found!
npm http GET https://registry.npmjs.org/nopt
npm http 304 https://registry.npmjs.org/nopt
npm http GET https://registry.npmjs.org/abbrev
npm http 304 https://registry.npmjs.org/abbrev
[email protected] node_modules/nopt
└── [email protected]

$ node -pe "require('nopt')({}, {}, ['-a', 'a', '-b=b', '--c', 'c', '--d=d'], 0)"
{ a: 'a',
  b: 'b',
  c: 'c',
  d: 'd',
  argv:
   { remain: [],
     cooked: [ '-a', 'a', '-b', 'b', '--c', 'c', '--d', 'd' ],
     original: [ '-a', 'a', '-b=b', '--c', 'c', '--d=d' ],
     toString: [Function] } }

$ npm install nopt@latest
npm WARN package.json [email protected] No README.md file found!
npm http GET https://registry.npmjs.org/nopt
npm http 304 https://registry.npmjs.org/nopt
npm http GET https://registry.npmjs.org/abbrev
npm http 304 https://registry.npmjs.org/abbrev
[email protected] node_modules/nopt
└── [email protected]

$ node -pe "require('nopt')({}, {}, ['-a', 'a', '-b=b', '--c', 'c', '--d=d'], 0)"
{ a: true,
  b: 'b',
  c: true,
  d: 'd',
  argv:
   { remain: [ 'a', 'c' ],
     cooked: [ '-a', 'a', '-b', 'b', '--c', 'c', '--d', 'd' ],
     original: [ '-a', 'a', '-b=b', '--c', 'c', '--d=d' ] } }

cowboy avatar Sep 11 '13 18:09 cowboy

FWIW, I think the new default behaviour is better; an unknown option should be assumed to be a flag (edit: unless they are in the form -a=b of course). But if major-version back compat is desired (which would be very surprising), then yes, this is a bug.

michaelficarra avatar Sep 11 '13 18:09 michaelficarra

I'm just happy to know either way what the intention is, and to see some documentation around how unexpected arguments are handled.

cowboy avatar Sep 11 '13 18:09 cowboy

Yes, this is an intentional change, and the reason for bumping the version to 2.x from 1.x.

Docs would be lovely. First one to send a pull request with docs gets the honor of closing this issue :)

isaacs avatar Sep 12 '13 00:09 isaacs