-
-
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
Open
miguelmarcondesf
wants to merge
21
commits into
nodejs:main
Choose a base branch
from
miguelmarcondesf:support-help-parseargs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+314
−2
Open
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
86083c3
lib: add help text support for options in parse_args
miguelmarcondesf 1e2dda3
lib: improve readability for help text formatting method
miguelmarcondesf 7178c4c
lib: add support for help text in parseArgs
miguelmarcondesf e5013a1
lib: add enableHelpPrinting option
miguelmarcondesf 40f4e67
lib: enhance help printing logic to handle empty help text
miguelmarcondesf cee8d9d
lib: refactor help printing test to improve output handling and missi…
miguelmarcondesf 07b8498
doc: add support for help text in options and enableHelpPrinting conf…
miguelmarcondesf 13aed88
doc: update pull request URL
miguelmarcondesf 707ebd0
lib: checks for help flag
miguelmarcondesf eadccbb
doc: update printUsage return type
miguelmarcondesf 2be1787
lib: remove enableHelpPrinting option
miguelmarcondesf 6c363f5
lib: refactor formatHelpTextForPrint and simplify printUsage logic
miguelmarcondesf 213c061
test: improve tests description
miguelmarcondesf e6a219f
test: update return structure and improve tests descriptions
miguelmarcondesf c8923dd
test: fix lint
miguelmarcondesf 40256fc
Update doc/api/util.md
miguelmarcondesf 15b44d2
lib: improve formatting in formatHelpTextForPrint using StringPrototy…
miguelmarcondesf 55b29d5
lib: add support for auto-injecting help option and return help text …
miguelmarcondesf f8d9140
test: enhance help option tests
miguelmarcondesf caf2840
doc: update util to enhance parseArgs help option functionality
miguelmarcondesf f6b6199
lib: removing returnHelpText and addHelpOption
miguelmarcondesf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ const { | |
| ObjectPrototypeHasOwnProperty: ObjectHasOwn, | ||
| StringPrototypeCharAt, | ||
| StringPrototypeIndexOf, | ||
| StringPrototypePadEnd, | ||
| StringPrototypeSlice, | ||
| StringPrototypeStartsWith, | ||
| } = primordials; | ||
|
|
@@ -305,13 +306,63 @@ function argsToTokens(args, options) { | |
| return tokens; | ||
| } | ||
|
|
||
| /** | ||
| * Format help text for printing. | ||
| * @param {string} longOption - long option name e.g. 'foo' | ||
| * @param {object} optionConfig - option config from parseArgs({ options }) | ||
| * @returns {string} formatted help text for printing | ||
| * @example | ||
| * formatHelpTextForPrint('foo', { type: 'string', help: 'help text' }) | ||
| * // returns '--foo <arg> help text' | ||
| */ | ||
| function formatHelpTextForPrint(longOption, optionConfig) { | ||
| const layoutSpacing = 30; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
|
|
||
| const shortOption = objectGetOwn(optionConfig, 'short'); | ||
| const type = objectGetOwn(optionConfig, 'type'); | ||
| const help = objectGetOwn(optionConfig, 'help'); | ||
|
|
||
| let helpTextForPrint = ''; | ||
| if (shortOption) { | ||
| helpTextForPrint += `-${shortOption}, `; | ||
| } | ||
| helpTextForPrint += `--${longOption}`; | ||
| if (type === 'string') { | ||
| helpTextForPrint += ' <arg>'; | ||
| } | ||
|
|
||
| if (help) { | ||
ljharb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (helpTextForPrint.length > layoutSpacing) { | ||
| helpTextForPrint += `\n${StringPrototypePadEnd('', layoutSpacing)}${help}`; | ||
| } else { | ||
| helpTextForPrint = `${StringPrototypePadEnd(helpTextForPrint, layoutSpacing)}${help}`; | ||
| } | ||
| } | ||
|
|
||
| return helpTextForPrint; | ||
| } | ||
|
|
||
| const parseArgs = (config = kEmptyObject) => { | ||
| const args = objectGetOwn(config, 'args') ?? getMainArgs(); | ||
| const strict = objectGetOwn(config, 'strict') ?? true; | ||
| const allowPositionals = objectGetOwn(config, 'allowPositionals') ?? !strict; | ||
| const returnTokens = objectGetOwn(config, 'tokens') ?? false; | ||
| const allowNegative = objectGetOwn(config, 'allowNegative') ?? false; | ||
| const options = objectGetOwn(config, 'options') ?? { __proto__: null }; | ||
| let options = objectGetOwn(config, 'options') ?? { __proto__: null }; | ||
| const help = objectGetOwn(config, 'help') ?? ''; | ||
|
|
||
| const hasGenerateHelp = help.length > 0; | ||
| if (hasGenerateHelp && !ObjectHasOwn(options, 'help')) { | ||
| options = { | ||
| ...options, | ||
| __proto__: null, | ||
| help: { | ||
| type: 'boolean', | ||
| short: 'h', | ||
| help: 'Show help', | ||
| }, | ||
| }; | ||
| } | ||
| // Bundle these up for passing to strict-mode checks. | ||
| const parseConfig = { args, strict, options, allowPositionals, allowNegative }; | ||
|
|
||
|
|
@@ -322,11 +373,11 @@ const parseArgs = (config = kEmptyObject) => { | |
| validateBoolean(returnTokens, 'tokens'); | ||
| validateBoolean(allowNegative, 'allowNegative'); | ||
| validateObject(options, 'options'); | ||
| validateString(help, 'help'); | ||
| ArrayPrototypeForEach( | ||
| ObjectEntries(options), | ||
| ({ 0: longOption, 1: optionConfig }) => { | ||
| validateObject(optionConfig, `options.${longOption}`); | ||
|
|
||
| // type is required | ||
| const optionType = objectGetOwn(optionConfig, 'type'); | ||
| validateUnion(optionType, `options.${longOption}.type`, ['string', 'boolean']); | ||
|
|
@@ -362,6 +413,11 @@ const parseArgs = (config = kEmptyObject) => { | |
| } | ||
| validator(defaultValue, `options.${longOption}.default`); | ||
| } | ||
|
|
||
| const helpOption = objectGetOwn(optionConfig, 'help'); | ||
| if (ObjectHasOwn(optionConfig, 'help')) { | ||
| validateString(helpOption, `options.${longOption}.help`); | ||
| } | ||
| }, | ||
| ); | ||
|
|
||
|
|
@@ -404,6 +460,23 @@ const parseArgs = (config = kEmptyObject) => { | |
| } | ||
| }); | ||
|
|
||
| // Phase 4: generate print usage for each option | ||
| let printUsage = ''; | ||
| if (help) { | ||
| printUsage += help; | ||
| } | ||
| ArrayPrototypeForEach(ObjectEntries(options), ({ 0: longOption, 1: optionConfig }) => { | ||
| const helpTextForPrint = formatHelpTextForPrint(longOption, optionConfig); | ||
|
|
||
| if (printUsage.length > 0) { | ||
| printUsage += '\n'; | ||
| } | ||
| printUsage += helpTextForPrint; | ||
| }); | ||
|
|
||
| if (help && printUsage.length > 0) { | ||
| result.helpText = printUsage; | ||
| } | ||
|
|
||
| return result; | ||
| }; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
helpgeneral and eachhelpoption would be impacted, considering some kind of translation.If it's only related to injecting
--helpwhen 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.