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

为什么我本地测试的结果--no-foo会覆盖事先定义的--foo #1287

Closed
ghost opened this issue Jun 22, 2020 · 3 comments
Closed

为什么我本地测试的结果--no-foo会覆盖事先定义的--foo #1287

ghost opened this issue Jun 22, 2020 · 3 comments

Comments

@ghost
Copy link

ghost commented Jun 22, 2020

直接copy文档上的例子。本地测试结果如下:
--no-cheese和--cheese(有默认值),如果后定义--cheese,打印program.cheese,那么结果是默认值。如果后定义--no-cheese,打印program.cheese,那么结果是true
如果只设置了--cheese(无默认值),执行node index pizza-options打印program.cheese,结果是undefined

@shadowspawn
Copy link
Collaborator

shadowspawn commented Jun 22, 2020

What is your code, and what are you trying? I tried this:

const { program } = require('commander');

program
  .option('--cheese <flavour>', 'cheese flavour', 'mozzarella')
  .option('--no-cheese', 'plain with no cheese');
  
program.parse();

console.log(program.cheese);
% node index.js 
mozzarella
% node index.js --cheese blue
blue
% node index.js --no-cheese  
false
% node index.js --cheese blue --no-cheese
false
% node index.js --no-cheese --cheese=blue
blue

@ghost
Copy link
Author

ghost commented Jun 23, 2020

sorry,I just now found the cause of the error。
my project's commander package version is 2.20.0 and resulting in incorrect default values。
when i update it,the result is corret。
Thank you for answering my childish question

@shadowspawn
Copy link
Collaborator

shadowspawn commented Jun 23, 2020

Glad you worked it out. Thanks for explaining.

There were good improvements released in Commander v3 from #795, collected items in #939.

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

1 participant