args icon indicating copy to clipboard operation
args copied to clipboard

Feature: Handle unary boolean option

Open chabou opened this issue 7 years ago • 3 comments

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

chabou avatar Nov 02 '17 23:11 chabou

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.

ntwcklng avatar Nov 04 '17 07:11 ntwcklng

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?

chabou avatar Nov 04 '17 11:11 chabou

Actually, that would be awesome! Yes please.

ntwcklng avatar Nov 04 '17 15:11 ntwcklng