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

Undocumented behavior change since 2.x #1203

Closed
guimard opened this issue Feb 26, 2020 · 8 comments
Closed

Undocumented behavior change since 2.x #1203

guimard opened this issue Feb 26, 2020 · 8 comments

Comments

@guimard
Copy link

guimard commented Feb 26, 2020

Hi,

this simple program has 2 different behavior depending on commander version:

var p = require('commander');
p.option('-d, --debug [level]', 'debug level')
 .option('-m, --mangle [test]', 'mangle')
 .option('--out', 'output')
 
p.parse(process.argv);
console.error('Debug args:', p.opts());

Resuilt of node test.js -dm --out:

  • 2.x : Debug args: { debug: true, mangle: true, out: true }
  • 4.x : Debug args: { debug: 'm', mangle: undefined, out: true }

This has strange effect when using uglify-js.

Cheers,
Xavier

@shadowspawn
Copy link
Collaborator

shadowspawn commented Feb 26, 2020

Quick response: I think this is due to changes from #599 for #395, released in Commander 3.0.0

https://github.com/tj/commander.js/releases/tag/v3.0.0

Can call like this for consistent behaviour across versions:

node test.js -d -m --out

@shadowspawn
Copy link
Collaborator

shadowspawn commented Feb 27, 2020

The implementation of option parsing is refined further in Commander v5, and the updated README description is:

Multiple short flags may optionally be combined in a single argument following the dash: boolean flags, the last flag may take a value, and the value. For example -a -b -p 80 may be written as -ab -p80 or even -abp80.

if (option.required || option.optional) {

@guimard
Copy link
Author

guimard commented Mar 3, 2020

Hi,

commander 5.0.0-4 does not fix the problem!

Cheers,
Xavier

@guimard
Copy link
Author

guimard commented Mar 3, 2020

Following the doc:

Multiple short flags may optionally be combined in a single argument following the dash: boolean flags, the last flag may take a value, and the value. For example -a -b -p 80 may be written as -ab -p80 or even -abp80.

It becomes a bug

@shadowspawn
Copy link
Collaborator

shadowspawn commented Mar 3, 2020

Drat, the new wording is not as clear as I hoped! The behaviour is as intended in Command 5, but I will recheck the source documentation for behaviour and wording of description.

@shadowspawn
Copy link
Collaborator

The standard I consult is: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html

Commander is more flexible with optional values than described there, but Commander is deliberately supporting the pattern that a value can directly follow the short flag, from item 2:

If the SYNOPSIS shows an optional option-argument (as with [ -f[ option_argument]] in the example), a conforming application shall place any option-argument for that option directly adjacent to the option in the same argument string, without intervening characters.

And Guideline 5:

One or more options without option-arguments, followed by at most one option that takes an option-argument, should be accepted when grouped behind one '-' delimiter.

@shadowspawn
Copy link
Collaborator

An answer was provided, and no further activity in a month. Closing this as resolved.

Feel free to open a new issue if it comes up again, with new information and renewed interest.

Thank you for your contributions.

@shadowspawn
Copy link
Collaborator

I think this behaviour change will be a problem for heavy users of optional values when they upgrade Commander, even though it is deliberate. Opened a PR to make it configurable.

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

2 participants