Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util: support more config options for parseArgs #53434

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

RedYetiDev
Copy link
Member

@RedYetiDev RedYetiDev commented Jun 12, 2024

Fixes #53427

This pull request introduces the ability for an argument in parseArgs to have the required and placeholder options.

required means that an argument must be supplied.
placeholder is the value for an argument when it is supplied, but no value is supplied with it.

@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 Jun 12, 2024
@RedYetiDev
Copy link
Member Author

I'm all ears to better name options than useDefaultWhenUndefined, if anyone has suggestions.

@cjihrig
Copy link
Contributor

cjihrig commented Jun 12, 2024

Maybe I'm misunderstanding the request here, but would the following work:

  • Add an optional option for individual flags.
  • If optional is false use the current behavior.
  • If optional is true the value is essentially provided value || default value || undefined (yes I realize this is not ideal JavaScript code)
  • optional defaults to false to keep current behavior.

@RedYetiDev
Copy link
Member Author

IMO, "optional" doesn't explain what's this is doing. This flag is meant for if the flag is supplied, but does not contain a value.

@cjihrig
Copy link
Contributor

cjihrig commented Jun 12, 2024

Ah, I was indeed misunderstanding the request. Carry on 😄

Copy link
Member

@tniessen tniessen left a comment

Choose a reason for hiding this comment

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

As I mentioned in #53427 (comment), parseArgs() is supposed to be a minimal implementation. It is not meant for anything beyond the most basic use cases.

More importantly, I don't see how this PR addresses #53427. With this API, how is the application supposed to distinguish between the absence of the option (in which case the option gets the default value) and the presence of the option without a value (in which case the option gets the default value)? In what scenario is useDefaultWhenUndefined useful?

@RedYetiDev
Copy link
Member Author

RedYetiDev commented Jun 12, 2024

With this API, how is the application supposed to distinguish between the absence of the option (in which case the option gets the default value) and the presence of the option without a value (in which case the option gets the default value)? In what scenario is useDefaultWhenUndefined useful?

Before this API, when an argument was supplied without a value (--my-argument), and the type was string, an error would throw. With this API, when an argument is supplied without a value and as a string type, the default value will be used.

An example use case is when you want the user to have the option to enable/customize an option.
If you want to achieve: --color[=green]

./my_script.js --color # Green color
./my_script.js --color=yellow # Yellow color
./my_script.js # Green color

@RedYetiDev RedYetiDev force-pushed the parse-args-default branch 2 times, most recently from 6108de4 to f9d1ef5 Compare June 12, 2024 19:12
@RedYetiDev
Copy link
Member Author

RedYetiDev commented Jun 12, 2024

My latest commit undid itself 🤔

(Fixed)

@tniessen
Copy link
Member

An example use case is when you want the user to have the option to enable/customize an option.
If you want to achieve: --color[=green]

./my_script.js --color # Green color
./my_script.js --color=yellow # Yellow color
./my_script.js # Green color

@RedYetiDev Again, I do not think that's what the feature request is describing. The first and third case in your example should not have the same result, and they do not in the case of git ... --color, which is the reference in #53427.

@RedYetiDev
Copy link
Member Author

RedYetiDev commented Jun 12, 2024

Makes sense, I can adjust the specifics later today

@RedYetiDev RedYetiDev marked this pull request as draft June 12, 2024 20:56
@RedYetiDev RedYetiDev marked this pull request as ready for review June 12, 2024 21:13
@RedYetiDev RedYetiDev requested a review from tniessen June 12, 2024 21:13
@RedYetiDev RedYetiDev changed the title util: support undefined values in parse args util: support more config options for parseArgs Jun 12, 2024
@shadowspawn
Copy link
Member

required was not mentioned in #53427 and has already been proposed, and some concerns raised:

@RedYetiDev
Copy link
Member Author

required was not mentioned in #53427

While it wasn't mentioned, I felt that the addition of a placeholder also warranted the addition of a required, as a user may want to use the placeholder on a required flag.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-ci PRs that need a full CI run. util Issues and PRs related to the built-in util module.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow parseArgs options to have an optional value
5 participants