Comments (8)
It would also make printing out help text based on the options a bit of a pain
I've added an example of how to hide --no-*
options to the wiki.
The suggestion is that the lib should automatically support negated booleans
The problem is that it's an opinionated domain. Yes, the lib could follow a convention and automatically prepend no-
to option names but, instead of --no-dev
what if users wanted --нет-дев
? Opinions and conventions differ, and change over time. I have withdrawn opinions from this lib in the past.
from command-line-args.
Hi, I need more detail. Could you give me an exampe of the use case you want to support.
from command-line-args.
If you have a Boolean
option with a default true
value, then it's nice to be able to negate it in the standard way, e.g. if you have --dev
with a default of true
, then --no-dev
would generally set it to false. Built in support for this is relatively common in command line argument processors.
from command-line-args.
Having a Boolean
option defined with a defaultValue
of true
effectively makes the option useless.
Take this example:
const commandLineArgs = require('command-line-args')
const optionDefinitions = [
{ name: 'dev', type: Boolean, defaultValue: true }
]
const options = commandLineArgs(optionDefinitions)
console.log(options)
Whether you set --dev
or not makes no difference, effectively making the option pointless.
$ node example.js --dev
{ dev: true }
$ node example.js
{ dev: true }
I'm not sure this is good option design. Would this definition make more sense? It has two meaningful states - true and false, where true
means "prod" and false
means "dev".
const optionDefinitions = [
{ name: 'prod' type: Boolean }
]
from command-line-args.
In the case you do have a Boolean
flag permenantly set to true
, here's an example of how to implement a negating flag:
const commandLineArgs = require('command-line-args')
const optionDefinitions = [
{ name: 'dev', type: Boolean, defaultValue: true },
{ name: 'no-dev', type: Boolean }
]
const options = commandLineArgs(optionDefinitions)
const dev = options.dev && !options['no-dev']
console.log(`dev: ${dev}`)
Usage:
$ node example.js
dev: true
$ node example.js --dev
dev: true
$ node example.js --dev --no-dev
dev: false
$ node example.js --no-dev
dev: false
from command-line-args.
I've added an example and explaination to the wiki. Does this help?
https://github.com/75lb/command-line-args/wiki/How-to-implement-a-negating-flag
from command-line-args.
Having a Boolean option defined with a defaultValue of true effectively makes the option useless.
Yes, that's the exact point of the issue :)
In the case you do have a Boolean flag permenantly set to true, here's an example of how to implement a negating flag:
Yes, that's pretty much how I worked around it too. It would also make printing out help text based on the options a bit of a pain, but I'm not doing that so I don't know for sure.
The suggestion is that the lib should automatically support negated booleans, such that if you define a dev
with a boolean type then it should also automatically handle the no-dev
without having to manually parse it.
Negating boolean flags with a no-
prefix is a common pattern for command line arguments, and also a common pattern to support in command line argument parsing libraries, but this lib is nice and simple, so possibly too out of scope?
from command-line-args.
from command-line-args.
Related Issues (20)
- Throw an exception if both `lazyMultiple` and `multiple` are set HOT 3
- Aliases should be nullable HOT 2
- recommended way to handle help? HOT 1
- Allow configuring arguments to be required HOT 3
- When trying to use a proxy such as jFrog's Artifactory, npm install fails HOT 2
- Missing argument should be error? HOT 1
- How to set a boolean value that defaults to true to false. HOT 1
- Multiple as a single type? HOT 2
- Jumping to [email protected] restrict node to >=14 HOT 2
- double hyphen is parsed as an argument when `defaultOption` is set
- Impossible to validate missing arguments for a flag when `multiple` is set HOT 1
- Parsing error of strings that starts with a digit HOT 1
- name containing dash (e.g. --num-max) supported? HOT 2
- Support concatenated .multiple for string arguments HOT 2
- Unclear behavior with defaultOption and more-than-one multiple args HOT 5
- docs for Usage guide generation is missing HOT 2
- New feature: Add a way to specify boolean value as an argument HOT 2
- What exactly is the purpose of defaultOption in multiple? HOT 1
- Support required options HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from command-line-args.