npm / nopt

Node/npm Option Parsing
ISC License
528 stars 43 forks source link

[BUG] Unexpected arguments not parsed correctly? #23

Open cowboy opened 11 years ago

cowboy commented 11 years ago

I've noticed a behavior change from nopt@1.0.10 -> nopt@2.1.2 with the parsing of unexpected arguments.

$ npm install nopt@~1.0.10
npm WARN package.json foo@0.0.0 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
nopt@1.0.10 node_modules/nopt
└── abbrev@1.0.4

$ 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 foo@0.0.0 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
nopt@2.1.2 node_modules/nopt
└── abbrev@1.0.4

$ 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' ] } }
michaelficarra commented 11 years ago

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.

cowboy commented 11 years ago

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

isaacs commented 11 years ago

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 :)