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

Default value for option assumes option is being used #440

Closed
mrmartineau opened this issue Sep 28, 2015 · 2 comments
Closed

Default value for option assumes option is being used #440

mrmartineau opened this issue Sep 28, 2015 · 2 comments

Comments

@mrmartineau
Copy link

In the commander docs, default values are specified like so:
(I added a default value of foo to the --peppers option to illustrate my point)

var program = require('commander');

program
  .version('0.0.1')
  .option('-p, --peppers', 'Add peppers', 'foo')
  .parse(process.argv);

if (program.peppers) console.log(program.peppers + '  - peppers');

The last line contains if (program.peppers) which will be called if --peppers contains a value, but in this scenario, it always does get executed regardless of if I use the -p or --peppers flag. I would like to be able to provide a default value (like this) and only execute a result if a flag has been used, is that possible? I haven't found the correct way to achieve what I want from the documentation, but I was wondering if there was a more elegant solution than what I've done below:

var program = require('commander');
var userArgs = process.argv.slice(2);

program
  .version('0.0.1')
  .option('-p, --peppers', 'Add peppers', 'foo')
  .parse(process.argv);

if (userArgs.indexOf('-p') !== -1) console.log(program.peppers + '  - peppers');

This works but is there a way to achieve this using commander itself, like if (program.peppers)?

@pal
Copy link

pal commented May 4, 2016

This appears to be working in the current version, are you still having this problem?

@mrmartineau
Copy link
Author

@pal yes, it seems to be working as expected now. Thanks

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