here:
https://github.com/tj/commander.js/blob/d9abf56c43a93e9584e2e18a9f8fb5af552f738c/index.js#L56-L60
and babel-cli that has --no-babelrc as option leave comment about this too.
https://github.com/babel/babel/blob/master/packages/babel-cli/src/babel/index.js#L246
sorry for my ignorance, but I wanna know the reason to remove --no from option name intentionally.
or I surmise that this code commited about 5 years ago might be legacy a little.
thanks.
Ditto, I'm trying to mirror the --no-verify flag to git push in my CLI tool but commander strips out the no so it doesn't work.
+1. I wanted to do --no-tests on a compile command. Ended up spending half an hour debugging why it wasn't working. For now I'm going to use --skip-tests instead. This should be documented, it isn't apparent from the examples.
It's correct.
If you've set --no-xxx as an option and don't call it (index.js), you will have xxx set to boolean true. And if you call it (index.js --no-xxx) you will have xxx set to boolean false.
I was trying to add a --no-color flag to add the functionality of removing colored output in a cli app when I discovered this and it took me a long time to figure this out.
I am porting the cli app from argparse to commander and would not really like to change the available flags.
This should be at least added in the documentation. The following code sample illustrates this.
program
.option('-b, --bbq-sauce', 'Add bbq sauce')
.option('--no-sauce', 'Remove sauce')
.parse(process.argv);
console.log('you ordered a pizza with:');
if (program.bbqSauce) console.log(' - bbq');
if(program.noSauce) console.log(" - removed sauce.")
This doesn't work currently because of the --noprefix and I don't really see a reason why this was done.
As I said, it's working.
program
.option('-b, --bbq-sauce', 'Add bbq sauce')
.option('--no-sauce', 'Remove sauce')
.parse(process.argv);
console.log('you ordered a pizza with:');
if (program.bbqSauce) console.log(' - bbq');
if (!program.sauce) console.log(' - removed sauce');
If --no-sauce is used, program.sauce will be set to false else true.
@gilles-crealp Yeah, got it, thanks.
However, considering multi-word options are camel-cased, I believe that the first intuition would be to try and access it as program.noSauce for most people and thus this should be added in the documentation to eliminate confusion.
@deepakpathania I totally agree, that must be more documented.
I will fork and submit a PR with the example shared above to be added in the README.
Use this example:
program
.option('--no-sauce', 'Remove sauce')
.parse(process.argv);
console.log('you ordered a pizza');
if (program.sauce) console.log(' with sauce');
else console.log(' without sauce');
@deepakpathania There is already an example here where you can choose a type of cheese or no-cheese.
@gilles-crealp I don't think it is that clear, moreover, this is a common use case and should be mentioned in the main README itself according to me.
@deepakpathania Great!
fixed by #709