util: add default value option to parsearg#44631
util: add default value option to parsearg#44631nodejs-github-bot merged 5 commits intonodejs:mainfrom
Conversation
doc/api/util.md
Outdated
| `false`, values for the option are last-wins. **Default:** `false`. | ||
| * `short` {string} A single character alias for the option. | ||
| * `default` {string | boolean | string[] | boolean[]} The default option | ||
| value when it is not set by args. |
There was a problem hiding this comment.
Document that this must match the arg type?
There was a problem hiding this comment.
Done, adding the multiple case too
5196cdd to
0b001a3
Compare
|
After thinking on it for a while, I think Specifically, accepting let options = { endpoint: { type: 'string', default: process.env.MY_PROJECT_ENDPOINT } };
let { values } = parseArgs({ options });which is a convenient pattern, especially since this utility almost certainly doesn't want to handle this sort of default-to-env behavior itself. If let options = { endpoint: { type: 'string' } };
if (process.env.MY_PROJECT_ENDPOINT != null) {
opts.endpoint.default = process.env.MY_PROJECT_ENDPOINT;
}
let { values } = parseArgs({ options });which is much less pleasant all around. |
|
Does this have the same semantics as destructuring with defaults? let { values: { foo = 'default' } } = util.parseArgs({ options: { foo: { type: 'string' } } }) |
The env use case seems pretty reasonable and convenient. I like it.
My comment was in context of the commented code, that I didn't see a need to treat |
Fair usage, I will update this PR 👍🏽
Yes, it does. Doing so would be like moving the "default burden logic" to the caller instead of callee, and this is unwanted in my opinion. |
shadowspawn
left a comment
There was a problem hiding this comment.
LGTM (not a collaborator)
doc/api/util.md
Outdated
| changes: | ||
| - version: REPLACEME | ||
| pr-url: https://github.com/nodejs/node/pull/44631 | ||
| description: add support for default values in input `config`. |
There was a problem hiding this comment.
| description: add support for default values in input `config`. | |
| description: Add support for default values in input `config`. |
| if (optionType === 'string' && !multipleOption) { | ||
| validateString(defaultValue, `options.${longOption}.default`); | ||
| } else if (optionType === 'string' && multipleOption) { | ||
| validateStringArray(defaultValue, `options.${longOption}.default`); | ||
| } else if (optionType === 'boolean' && !multipleOption) { | ||
| validateBoolean(defaultValue, `options.${longOption}.default`); | ||
| } else if (optionType === 'boolean' && multipleOption) { | ||
| validateBooleanArray(defaultValue, `options.${longOption}.default`); | ||
| } |
There was a problem hiding this comment.
nit: we could avoid the code repetition here.
| if (optionType === 'string' && !multipleOption) { | |
| validateString(defaultValue, `options.${longOption}.default`); | |
| } else if (optionType === 'string' && multipleOption) { | |
| validateStringArray(defaultValue, `options.${longOption}.default`); | |
| } else if (optionType === 'boolean' && !multipleOption) { | |
| validateBoolean(defaultValue, `options.${longOption}.default`); | |
| } else if (optionType === 'boolean' && multipleOption) { | |
| validateBooleanArray(defaultValue, `options.${longOption}.default`); | |
| } | |
| let validator; | |
| switch(optionType) { | |
| case 'string': | |
| validator = multipleOption ? validateStringArray : validateString; | |
| break; | |
| case 'boolean': | |
| validator = multipleOption ? validateBooleanArray : validateBoolean; | |
| break; | |
| } | |
| validator(defaultValue, `options.${longOption}.default`); |
|
Completed all the feedbacks Not sure about the failed CI check: should I rebase this branch? |
Users can set a default value for every expected input argument
1a7aae6 to
eb0c843
Compare
|
This PR has landed in v18.11.0
|
|
Node.js 16.x is going into maintenance mode next tuesday (see https://github.com/nodejs/Release#release-schedule), and there are no scheduled release until then, so PRs won't be "automatically" backported anymore. That means someone would need to open a backport PR for this to land on v16.x. |
|
Thanks for info @aduh95 Light question: might adding |
|
It should be, as long as someone opens a backport PR, see https://github.com/nodejs/node/blob/0f9087971c085ea0c93b9fd867e442afe11c0ae0/doc/contributing/backporting-to-release-lines.md for more info. |
|
There doesn't have to be a backport PR. Another option (especially if the commit can be cherry-picked without conflicts) is to add the |
This PR adds to the
parseArgsutility feature a newdefaultinput field option.The developer will be able to define a default value to retrieve when the input arguments are processed.
This PR has a downstream to pkgjs/parseargs#142