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

Feature: Handle unary boolean option #112

Open
chabou opened this issue Nov 2, 2017 · 3 comments
Open

Feature: Handle unary boolean option #112

chabou opened this issue Nov 2, 2017 · 3 comments

Comments

@chabou
Copy link
Contributor

chabou commented Nov 2, 2017

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
@ntwcklng
Copy link
Collaborator

ntwcklng commented Nov 4, 2017

Hey @chabou try this: #110 (comment)
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 ntwcklng closed this as completed Nov 4, 2017
@chabou
Copy link
Contributor Author

chabou commented Nov 4, 2017

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?

@ntwcklng
Copy link
Collaborator

ntwcklng commented Nov 4, 2017

Actually, that would be awesome! Yes please.

@ntwcklng ntwcklng reopened this Nov 4, 2017
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