args
args copied to clipboard
Feature: Handle unary boolean option
In case of boolean option, it would be useful to have this option with true value if it is passed as argument with no value.
Bonus: it should be compatible with --no-<option>
to set it false.
Example:
args.option(['v', 'verbose'], 'Enable verbose mode', false)
.option(['sound'], 'Enable sound', true);
# Should give /tmp as args.sub
$ myscript -v /tmp
$ myscript --no-sound /tmp
Hey @chabou try this: https://github.com/leo/args/issues/110#issuecomment-338704466
It is necessary to tell mri
or minimist
your booleans with the default value of true
.
Then the --no-sound
flag gets automatically generated.
args
.option(['v', 'verbose'], 'Enable verbose mode', false)
.option(['s', 'sound'], 'Enable sound', true)
const flags = args.parse(process.argv, {
mri: {
boolean: ['v', 's']
},
version: false
})
console.log('flags: ', flags)
console.log('sub: ', args.sub)
$ node args.js --v /tmp
flags: { v: true, verbose: true, s: true, sound: true }
sub: [ '/tmp' ]
$ node args.js --no-sound /tmp
flags: { v: false, verbose: false, s: false, sound: false }
sub: [ '/tmp' ]
Note: in order to get the verbose
option working with -v
& --verbose
, you have to disable the version
command in the options.
Thank you for this tip 😍 I will try!
But are you interested to have a PR to manage this directly in args
without the need to add a mri
section?
Actually, that would be awesome! Yes please.