Skip to content

util: add default value option to parsearg#44631

Merged
nodejs-github-bot merged 5 commits intonodejs:mainfrom
Eomm:parseargs-default-value
Oct 7, 2022
Merged

util: add default value option to parsearg#44631
nodejs-github-bot merged 5 commits intonodejs:mainfrom
Eomm:parseargs-default-value

Conversation

@Eomm
Copy link
Member

@Eomm Eomm commented Sep 13, 2022

This PR adds to the parseArgs utility feature a new default input 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

@nodejs-github-bot nodejs-github-bot added needs-ci PRs that need a full CI run. util Issues and PRs related to the built-in util module. labels Sep 13, 2022
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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Document that this must match the arg type?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, adding the multiple case too

@Eomm Eomm force-pushed the parseargs-default-value branch from 5196cdd to 0b001a3 Compare September 14, 2022 05:58
Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@lpinca lpinca added the semver-minor PRs that contain new features and should be released in the next minor version. label Sep 14, 2022
@bakkot
Copy link
Contributor

bakkot commented Sep 22, 2022

After thinking on it for a while, I think undefined should be a legal value for default, and should be treated exactly like default being missing. (So, in particular, setting default: undefined should not cause the key to appear in the values object.) cc @shadowspawn since you expressed the opposite opinion above.

Specifically, accepting undefined makes it easier to do stuff like

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 undefined isn't allowed, then you would have to write something like

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.

@tniessen
Copy link
Member

Does this have the same semantics as destructuring with defaults?

let { values: { foo = 'default' } } = util.parseArgs({ options: { foo: { type: 'string' } } })

@shadowspawn
Copy link
Member

After thinking on it for a while, I think undefined should be a legal value for default, and should be treated exactly like default being missing. (So, in particular, setting default: undefined should not cause the key to appear in the values object.)

The env use case seems pretty reasonable and convenient. I like it.

cc @shadowspawn since you expressed the opposite opinion [above (https://github.com//pull/44631#discussion_r971695523).

My comment was in context of the commented code, that I didn't see a need to treat undefined as a valid value for output values. So not in opposition to your suggestion that is useful to allow for input config.

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@Eomm
Copy link
Member Author

Eomm commented Sep 24, 2022

If undefined isn't allowed, then you would have to write something like

Fair usage, I will update this PR 👍🏽

Does this have the same semantics as destructuring with defaults?

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.

Copy link
Member

@shadowspawn shadowspawn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
description: add support for default values in input `config`.
description: Add support for default values in input `config`.

Comment on lines +336 to +345
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`);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: we could avoid the code repetition here.

Suggested change
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`);

@Eomm
Copy link
Member Author

Eomm commented Sep 26, 2022

Completed all the feedbacks

Not sure about the failed CI check: should I rebase this branch?

@Eomm Eomm force-pushed the parseargs-default-value branch from 1a7aae6 to eb0c843 Compare September 29, 2022 19:37
@mcollina mcollina added the request-ci Add this label to start a Jenkins CI on a PR. label Sep 30, 2022
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Sep 30, 2022
@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@mcollina mcollina added commit-queue Add this label to land a pull request using GitHub Actions. commit-queue-squash Add this label to instruct the Commit Queue to squash all the PR commits into the first one. and removed needs-ci PRs that need a full CI run. labels Oct 7, 2022
@shadowspawn
Copy link
Member

This PR has landed in v18.11.0

parseArgs has also been included in Node.js v16. Is there any additional work require to land this PR in v16, so default is also available there, or will it happen automatically?

@aduh95
Copy link
Contributor

aduh95 commented Oct 15, 2022

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.

@shadowspawn
Copy link
Member

shadowspawn commented Oct 15, 2022

Thanks for info @aduh95

Light question: might adding default support be acceptable as a back port at this late stage? (i.e. v16.x not in maintenance mode yet, but will be very soon!)

@aduh95
Copy link
Contributor

aduh95 commented Oct 16, 2022

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.

@targos
Copy link
Member

targos commented Oct 16, 2022

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 lts-watch-v16.x label.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author ready PRs that have at least one approval, no pending requests for changes, and a CI started. commit-queue-squash Add this label to instruct the Commit Queue to squash all the PR commits into the first one. semver-minor PRs that contain new features and should be released in the next minor version. util Issues and PRs related to the built-in util module.