-
-
Notifications
You must be signed in to change notification settings - Fork 34.2k
Add support to print help/usage of util.parseArgs #58875
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
base: main
Are you sure you want to change the base?
Add support to print help/usage of util.parseArgs #58875
Conversation
| * // returns '--foo <arg> help text' | ||
| */ | ||
| function formatHelpTextForPrint(longOption, optionConfig) { | ||
| const layoutSpacing = 30; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found print has some logic related to layout spacing, maybe we can use something similar here.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #58875 +/- ##
==========================================
- Coverage 88.55% 88.55% -0.01%
==========================================
Files 704 704
Lines 207824 207916 +92
Branches 40040 40058 +18
==========================================
+ Hits 184046 184117 +71
- Misses 15805 15847 +42
+ Partials 7973 7952 -21
🚀 New features to boost your workflow:
|
|
In general, what I'd expect is that |
|
@ljharb I'm not sure about printing the full help on validation failures. Once you have even three or four options that ends up being a lot of text, almost all of which is irrelevant to the actual error. Compare the output of I might be supportive of an option which makes validation failures just print the error and exit, instead of throwing a noisy error, but I don't think it makes sense to dump the full help text. |
|
That's a solid argument for allowing, but not defaulting, that behavior :-) |
|
I think that having commands print their full help text on argument validation failure is sufficiently unusual that it doesn't make sense to support it; I can't actually find any commands on my system that have that behavior, after trying a bunch at random. |
|
Interesting, I've seen that a lot in CLI tools on npm, albeit not in shell/OS ones. As long as the ability to trigger the help text exists, it can be worked around, so that's fine. |
6110e02 to
98c0d7e
Compare
98c0d7e to
a565569
Compare
doc/api/util.md
Outdated
| * `positionals` {string\[]} Positional arguments. | ||
| * `tokens` {Object\[] | undefined} See [parseArgs tokens](#parseargs-tokens) | ||
| section. Only returned if `config` includes `tokens: true`. | ||
| * `printUsage` {string | undefined} Formatted help text for all options provided. Only included if general `help` text |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In some of the original discussion, printUsage was a function and hence the name. It was suggested that simpler to just return a string, which is what you have done here. So I think the name could be changed.
| * `printUsage` {string | undefined} Formatted help text for all options provided. Only included if general `help` text | |
| * `usage` {string | undefined} Formatted help text for all options provided. Only included if general `help` text |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I personally think a function is better, because then it allows the implementation to defer formatting of the text if it wishes to.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Returning a function in the parse results? I find the idea of returning a function a bit surprising, I am expecting a plain old javascript object (POJO). And the function is presumably going to have to hold onto the passed arguments to process later.
Are there other APIs in node that behave like this and return mixed data and functions? (Not counting classes...)
Side note: if there is a function, it should return the string not output the string.
Although I'd just always return a usage when there's any help options specified, instead of returning a
printUsage function (so that you can print it some other way, e.g. as part of a larger message about invalid
arguments, or with a footer or something. It's not like it's expensive to compute the string up front).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(I wouldn't be surprised by a separate function to call to get the formatted help, but that isn't as integrated.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree the function would return the string, yes. If it's actually not expensive to compute the string then it's fine to just return a string.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I might worry more about deferring the cost of assembling the string in other contexts, but here it's really only going to happen once per script launch, so I wouldn't expect it to matter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey, I've updated values.help to return the string as it was.
To be honest, I think in general we would want to values return a value and not a behavior function 🤔 on the other hand, we will only have computation when being called, like @bakkot mentioned, but I think it is a cheap cost.
I would stick with the string value until we can find maybe future formatting parameters for that.
Let me know your thoughts @shadowspawn @ljharb @bakkot
We are giving them one place to configure the help option. It's the same place as all the other options - there is nothing special about an option which happens to be spelled |
|
Hey @bakkot @ljharb @shadowspawn
Let me know what we think, and thanks in advance! |
Hey everyone @bakkot @ljharb @shadowspawn |
Yes. I suggested those, and they didn't work out! I'm happy to do a PR to remove those on your PR it you like. |
Co-authored-by: John Gee <john@ruru.gen.nz>
Co-authored-by: Jordan Harband <ljharb@gmail.com>
3650308 to
f6b6199
Compare
@shadowspawn No worries, just pushed that change! |
| help: { | ||
| type: 'boolean', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if the user wants their custom option to be called "ayuda", how would that disable the auto-added --help?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ljharb Good point, but in that case, everything we're using in relation to the help general and each help option would be impacted, considering some kind of translation.
If it's only related to injecting --help when the general help option isn't available, we can simply remove that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove what? I'm confused.
Personally, I think the help option should only ever be named "--help", and be a boolean, but others argued upthread that it should be customizable. However, now we don't have any way to know if the user is passing a "help" option or not, because it could be named anything.
That's why I suggested type: "help", which can only be a boolean, because then we only inject our own help option when no type help options are provided by the user.
Issue: 56184
Add support to print help/usage of
util.parseArgsThis PR adds help text functionality to
util.parseArgs, allowing developers to easily generate and display help information for their CLI applications.Checklist
printUsageadded to return object