util: support negative options for parseArgs#53107
util: support negative options for parseArgs#53107nodejs-github-bot merged 1 commit intonodejs:mainfrom
parseArgs#53107Conversation
|
IMO there should be a flag, something like Tip I am not a core collaborator, and this is only a suggestion. |
|
Turning this on by default would be a breaking change. Might make more sense to make it opt-in per option, as in Also I'm not sure it makes sense to support this for
That's definitely wrong. |
mcollina
left a comment
There was a problem hiding this comment.
I don't think it should be set to the opposite of the default value, but to false.
Thanks for the advise!, I believe a flag like |
Thanks for the reminder! I have misunderstanding it before and thought
I believe add a new flag in the
I think that the |
Thanks for the reminder. I have misunderstanding it before and thought --no- prefix should just opposite the default value. |
--no- prefix for argument with boolean type for parseArgsparseArgs
doc/api/util.md
Outdated
| * `allowPositionals` {boolean} Whether this command accepts positional | ||
| arguments. | ||
| **Default:** `false` if `strict` is `true`, otherwise `true`. | ||
| * `allowNegative` {boolean} Whether allow negative options. |
There was a problem hiding this comment.
| * `allowNegative` {boolean} Whether allow negative options. | |
| * `allowNegative` {boolean} Whether allow negative options prefixed with `--no-`. |
There was a problem hiding this comment.
"Negative options" is a bit unclear. Maybe something more explicit?
| * `allowNegative` {boolean} Whether allow negative options. | |
| * `allowNegative` {boolean} If `true`, allows explicitly setting boolean options to `false` by prefixing the option name with `--no-`. |
|
If landing this, it would be good to also update the docs which use this precise case as an example of the tokens array:
This example should either be replaced or should at least mention that you can do this automatically with the
|
|
I was thinking about that too. The example in the docs support
|
| if (allowNegative && StringPrototypeStartsWith(longOption, 'no-')) { | ||
| // Boolean option negation: --no-foo | ||
| const longOptionWithoutPrefixNo = StringPrototypeSlice(longOption, 3); | ||
| if (optionsGetOwn(options, longOptionWithoutPrefixNo, 'type') !== 'string') { |
There was a problem hiding this comment.
The test in checkOptionUsage is against 'boolean' so I think probably clearer and more consistent to test against 'boolean' here?
| if (optionsGetOwn(options, longOptionWithoutPrefixNo, 'type') !== 'string') { | |
| if (optionsGetOwn(options, longOptionWithoutPrefixNo, 'type') === 'boolean') { |
There was a problem hiding this comment.
The test in
checkOptionUsageis against'boolean'so I think probably clearer and more consistent to test against'boolean'here?
here I have considered if options come from CLI, just like node index.js --no-foo, in this case, (optionsGetOwn(options, longOptionWithoutPrefixNo, 'type') will be undefined, so it also should be added when using if (... === 'boolean'), which is better for consistent testing.
There was a problem hiding this comment.
Ah, so strict:false and allowNegation means --no-foo returns foo:false. Right, I missed that and agree your code is correct and my suggestion wrong. 👍
This is very edge case, but what about strict:false and --no-foo=x? I think to be consistent with general behaviour of --x=y in strict:false should probably return no-foo:x?
The high level intention is when end-user does something unexpected, preserve information and leave it to author to sort out. (See for example the code a few lines down that only sets newValue totrue only if there is not an option value supplied.)
(Edit: added suggestion to cover --no-foo=x in separate comment.)
This comment was marked as duplicate.
This comment was marked as duplicate.
Sorry, something went wrong.
|
For interest, I was wondering about a short option for negation, and current state of PR does allow this: % node index.js
{ values: [Object: null prototype] {}, positionals: [] }
% node index.js --no-boolean
{
values: [Object: null prototype] { boolean: false },
positionals: []
}
% node index.js -B
{
values: [Object: null prototype] { boolean: false },
positionals: []
}
|
I'm curious if the example of |
I have referred the usage of negative options in |
To be clear, I think the current PR behaviour is fine. I tried that configuration because it is natural in Commander, which has separate options for the positive and negative configurations, so obvious that can have a short option for the negative. I wanted to see if I could do it in |
I think it is a simpler and more predictable behaviour for As for whether we need a new example... |
|
There are some other example uses for tokens on the |
test/parallel/test-parse-args.mjs
Outdated
| }); | ||
|
|
||
| test('allow negative options and passed multiple arguments', () => { | ||
| const args = ['--alpha', '--no-alpha', '--alpha']; |
There was a problem hiding this comment.
Suggest removing first option so can tell that last-one-wins.
| const args = ['--alpha', '--no-alpha', '--alpha']; | |
| const args = ['--no-alpha', '--alpha']; |
|
Landed in 4a72b2f |
PR-URL: nodejs#53107 Refs: nodejs#53095 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
PR-URL: nodejs#53107 Refs: nodejs#53095 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Notable changes: deps,lib,src: * (SEMVER-MINOR) add experimental web storage (Colin Ihrig) #52435 doc: * move `node --run` stability to rc (Yagiz Nizipli) #53433 * mark WebSocket as stable (Matthew Aitken) #53352 * mark --heap-prof and related flags stable (Joyee Cheung) #53343 * mark --cpu-prof and related flags stable (Joyee Cheung) #53343 * doc-only deprecate OpenSSL engine-based APIs (Richard Lau) #53329 inspector: * fix disable async hooks on Debugger.setAsyncCallStackDepth (Joyee Cheung) #53473 lib: * (SEMVER-MINOR) add diagnostics_channel events to module loading (RafaelGSS) #44340 util: * (SEMVER-MINOR) support `--no-` for argument with boolean type for parseArgs (Zhenwei Jin) #53107 PR-URL: #53583
Notable changes: deps,lib,src: * (SEMVER-MINOR) add experimental web storage (Colin Ihrig) #52435 doc: * move `node --run` stability to rc (Yagiz Nizipli) #53433 * mark WebSocket as stable (Matthew Aitken) #53352 * mark --heap-prof and related flags stable (Joyee Cheung) #53343 * mark --cpu-prof and related flags stable (Joyee Cheung) #53343 * doc-only deprecate OpenSSL engine-based APIs (Richard Lau) #53329 inspector: * fix disable async hooks on Debugger.setAsyncCallStackDepth (Joyee Cheung) #53473 lib: * (SEMVER-MINOR) add diagnostics_channel events to module loading (RafaelGSS) #44340 util: * (SEMVER-MINOR) support `--no-` for argument with boolean type for parseArgs (Zhenwei Jin) #53107 PR-URL: #53583
Notable changes: deps,lib,src: * (SEMVER-MINOR) add experimental web storage (Colin Ihrig) #52435 doc: * move `node --run` stability to rc (Yagiz Nizipli) #53433 * mark WebSocket as stable (Matthew Aitken) #53352 * mark --heap-prof and related flags stable (Joyee Cheung) #53343 * mark --cpu-prof and related flags stable (Joyee Cheung) #53343 * doc-only deprecate OpenSSL engine-based APIs (Richard Lau) #53329 inspector: * fix disable async hooks on Debugger.setAsyncCallStackDepth (Joyee Cheung) #53473 lib: * (SEMVER-MINOR) add diagnostics_channel events to module loading (RafaelGSS) #44340 util: * (SEMVER-MINOR) support `--no-` for argument with boolean type for parseArgs (Zhenwei Jin) #53107 PR-URL: #53583
Notable changes: deps,lib,src: * (SEMVER-MINOR) add experimental web storage (Colin Ihrig) #52435 doc: * move `node --run` stability to rc (Yagiz Nizipli) #53433 * mark WebSocket as stable (Matthew Aitken) #53352 * mark --heap-prof and related flags stable (Joyee Cheung) #53343 * mark --cpu-prof and related flags stable (Joyee Cheung) #53343 * doc-only deprecate OpenSSL engine-based APIs (Richard Lau) #53329 inspector: * fix disable async hooks on Debugger.setAsyncCallStackDepth (Joyee Cheung) #53473 lib: * (SEMVER-MINOR) add diagnostics_channel events to module loading (RafaelGSS) #44340 util: * (SEMVER-MINOR) support `--no-` for argument with boolean type for parseArgs (Zhenwei Jin) #53107 PR-URL: #53583
Notable changes: buffer: * (SEMVER-MINOR) add .bytes() method to Blob (Matthew Aitken) #53221 doc: * doc-only deprecate OpenSSL engine-based APIs (Richard Lau) #53329 * (SEMVER-MINOR) add context.assert docs (Colin Ihrig) #53169 * (SEMVER-MINOR) improve explanation about built-in modules (Joyee Cheung) #52762 * add StefanStojanovic to collaborators (StefanStojanovic) #53118 * add Marco Ippolito to TSC (Rafael Gonzaga) #53008 inspector: * fix disable async hooks on Debugger.setAsyncCallStackDepth (Joyee Cheung) #53473 net: * (SEMVER-MINOR) add new net.server.listen tracing channel (Paolo Insogna) #53136 process: * (SEMVER-MINOR) add process.getBuiltinModule(id) (Joyee Cheung) #52762 src,permission: * (SEMVER-MINOR) --allow-wasi & prevent WASI exec (Rafael Gonzaga) #53124 test_runner: * (SEMVER-MINOR) add context.fullName (Colin Ihrig) #53169 util: * (SEMVER-MINOR) support `--no-` for argument with boolean type for parseArgs (Zhenwei Jin) #53107 PR-URL: TODO
Notable changes: buffer: * (SEMVER-MINOR) add .bytes() method to Blob (Matthew Aitken) #53221 doc: * doc-only deprecate OpenSSL engine-based APIs (Richard Lau) #53329 * (SEMVER-MINOR) add context.assert docs (Colin Ihrig) #53169 * (SEMVER-MINOR) improve explanation about built-in modules (Joyee Cheung) #52762 * add StefanStojanovic to collaborators (StefanStojanovic) #53118 * add Marco Ippolito to TSC (Rafael Gonzaga) #53008 inspector: * fix disable async hooks on Debugger.setAsyncCallStackDepth (Joyee Cheung) #53473 net: * (SEMVER-MINOR) add new net.server.listen tracing channel (Paolo Insogna) #53136 process: * (SEMVER-MINOR) add process.getBuiltinModule(id) (Joyee Cheung) #52762 src,permission: * (SEMVER-MINOR) --allow-wasi & prevent WASI exec (Rafael Gonzaga) #53124 test_runner: * (SEMVER-MINOR) add context.fullName (Colin Ihrig) #53169 util: * (SEMVER-MINOR) support `--no-` for argument with boolean type for parseArgs (Zhenwei Jin) #53107 PR-URL: #53945
Notable changes: buffer: * (SEMVER-MINOR) add .bytes() method to Blob (Matthew Aitken) #53221 doc: * doc-only deprecate OpenSSL engine-based APIs (Richard Lau) #53329 * (SEMVER-MINOR) add context.assert docs (Colin Ihrig) #53169 * (SEMVER-MINOR) improve explanation about built-in modules (Joyee Cheung) #52762 * add StefanStojanovic to collaborators (StefanStojanovic) #53118 * add Marco Ippolito to TSC (Rafael Gonzaga) #53008 inspector: * fix disable async hooks on Debugger.setAsyncCallStackDepth (Joyee Cheung) #53473 net: * (SEMVER-MINOR) add new net.server.listen tracing channel (Paolo Insogna) #53136 process: * (SEMVER-MINOR) add process.getBuiltinModule(id) (Joyee Cheung) #52762 src,permission: * (SEMVER-MINOR) --allow-wasi & prevent WASI exec (Rafael Gonzaga) #53124 test_runner: * (SEMVER-MINOR) add context.fullName (Colin Ihrig) #53169 util: * (SEMVER-MINOR) support `--no-` for argument with boolean type for parseArgs (Zhenwei Jin) #53107 PR-URL: #53945
Notable changes: buffer: * (SEMVER-MINOR) add .bytes() method to Blob (Matthew Aitken) #53221 doc: * doc-only deprecate OpenSSL engine-based APIs (Richard Lau) #53329 * (SEMVER-MINOR) add context.assert docs (Colin Ihrig) #53169 * (SEMVER-MINOR) improve explanation about built-in modules (Joyee Cheung) #52762 * add StefanStojanovic to collaborators (StefanStojanovic) #53118 * add Marco Ippolito to TSC (Rafael Gonzaga) #53008 inspector: * fix disable async hooks on Debugger.setAsyncCallStackDepth (Joyee Cheung) #53473 net: * (SEMVER-MINOR) add new net.server.listen tracing channel (Paolo Insogna) #53136 process: * (SEMVER-MINOR) add process.getBuiltinModule(id) (Joyee Cheung) #52762 src,permission: * (SEMVER-MINOR) --allow-wasi & prevent WASI exec (Rafael Gonzaga) #53124 test_runner: * (SEMVER-MINOR) add context.fullName (Colin Ihrig) #53169 util: * (SEMVER-MINOR) support `--no-` for argument with boolean type for parseArgs (Zhenwei Jin) #53107 PR-URL: #53945
Notable changes: buffer: * (SEMVER-MINOR) add .bytes() method to Blob (Matthew Aitken) #53221 doc: * doc-only deprecate OpenSSL engine-based APIs (Richard Lau) #53329 * (SEMVER-MINOR) add context.assert docs (Colin Ihrig) #53169 * (SEMVER-MINOR) improve explanation about built-in modules (Joyee Cheung) #52762 * add StefanStojanovic to collaborators (StefanStojanovic) #53118 * add Marco Ippolito to TSC (Rafael Gonzaga) #53008 inspector: * fix disable async hooks on Debugger.setAsyncCallStackDepth (Joyee Cheung) #53473 net: * (SEMVER-MINOR) add new net.server.listen tracing channel (Paolo Insogna) #53136 process: * (SEMVER-MINOR) add process.getBuiltinModule(id) (Joyee Cheung) #52762 src,permission: * (SEMVER-MINOR) --allow-wasi & prevent WASI exec (Rafael Gonzaga) #53124 test_runner: * (SEMVER-MINOR) add context.fullName (Colin Ihrig) #53169 util: * (SEMVER-MINOR) support `--no-` for argument with boolean type for parseArgs (Zhenwei Jin) #53107 PR-URL: #53945
This PR tries to support negative options like the format
--no-fooforparseArgsby adding a flagallowNegativein theconfigofparseArgs. It works for general CLI flag andoptionspassed toparseArgs.By default,
allowNegativeisfalsein order to bring a breaking change.Refs: #53095