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

requiredOption doesn't work for option named 'action' #1160

Closed
andrui opened this issue Jan 31, 2020 · 3 comments
Closed

requiredOption doesn't work for option named 'action' #1160

andrui opened this issue Jan 31, 2020 · 3 comments

Comments

@andrui
Copy link

andrui commented Jan 31, 2020

STR:

const program = require('commander');

program
  .requiredOption('-a, --action <action>', 'service action');

program.parse(process.argv);

AR:
No "required option" error

@shadowspawn
Copy link
Collaborator

The reason for the lack of error is that program.action is a function and clashes with using program.action as an option value. From Commander 4.1, you can avoid option name clashes by disabling the old behaviour using .storeOptionsAsProperties(false);

https://github.com/tj/commander.js#avoiding-option-name-clashes

const program = require('commander');

program
  . storeOptionsAsProperties(false);

program
  .requiredOption('-a, --action <action>', 'service action');

program.parse(process.argv)
% node .
error: required option '-a, --action <action>' not specified

@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.

@shadowspawn
Copy link
Collaborator

Opened a PR to add a warning for option name clashes: #1275

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