75lb / command-line-args

A mature, feature-complete library to parse command-line options.
MIT License
679 stars 107 forks source link

New feature: Add a way to specify boolean value as an argument #127

Closed jihu closed 4 days ago

jihu commented 2 years ago

It would be very useful if one was able to configure command-line-args so that a Boolean option can take it's value as an argument, just like a String option. Currently it is impossible using a single parameter, to change it from a default value true, to the value false. That's like having a GUI where it's impossible to uncheck a checkbox that is checked by default. It simply doesn't make any sense.

Not only would it make it more consistent to allow this (compared to String options etc), but it would also make it possible to change a default true value to false, without having to use the imho very ugly "--no-xyz" extra parameter workaround. It would also work regardless of the language the parameter is written in, since there is only one option, not two seemingly unrelated options ("--xyz" and "--no-xyz", or "--дев" and "--нет-дев").

The example in issue 113 is spot on, but that issue didn't ask for a new feature. This here is a feature request, not a documentation question. I know how it works. I'm simply suggesting how to make an already great product even better :)

jihu commented 2 years ago

This feature would also make it possible to have a Boolean option to be required. That is currently impossible.

The use case for having a required boolean could be that one want's to force the user to make an active decision, ie yes/no, true/false. But since the user can't actively choose 'false' then this can't be done currently.

75lb commented 1 year ago

Hi, could you post an example so I can more accurately visualise what you're trying to achieve? Something like this?

$ example file1.txt file2.txt --terms-accepted true

It would be very useful if one was able to configure command-line-args so that a Boolean option can take it's value as an argument, just like a String option.

If a boolean flag option requires an argument then it's no longer a boolean flag option. Could you define an option which accepts an input argument and then process the result with a custom function? Something like..

const commandLineArgs = require('command-line-args')

function activeDecision (userInput) {
  return userInput === 'true' || userInput === 'yes'
}

const optionDefinitions = [
  { name: 'accept-terms', type: activeDecision }
]

const options = commandLineArgs(optionDefinitions, { camelCase: true })
console.log('activeDecision result:', options.acceptTerms)

if (options.acceptTerms) {
  console.log('Thanks for accepting the terms.')
} else {
  console.log('Please accept the terms to continue.')
}

Example usage:

$ node active-decision.js
activeDecision result: undefined
Please accept the terms to continue.

$ node active-decision.js --accept-terms
activeDecision result: null
Please accept the terms to continue.

$ node active-decision.js --accept-terms no
activeDecision result: false
Please accept the terms to continue.

$ node active-decision.js --accept-terms yes
activeDecision result: true
Thanks for accepting the terms.

$ node active-decision.js --accept-terms true
activeDecision result: true
Thanks for accepting the terms.