Skip to content

Commit 55b29d5

Browse files
lib: add support for auto-injecting help option and return help text in parseArgs
1 parent 15b44d2 commit 55b29d5

File tree

1 file changed

+35
-14
lines changed

1 file changed

+35
-14
lines changed

lib/internal/util/parse_args/parse_args.js

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -348,8 +348,25 @@ const parseArgs = (config = kEmptyObject) => {
348348
const allowPositionals = objectGetOwn(config, 'allowPositionals') ?? !strict;
349349
const returnTokens = objectGetOwn(config, 'tokens') ?? false;
350350
const allowNegative = objectGetOwn(config, 'allowNegative') ?? false;
351-
const options = objectGetOwn(config, 'options') ?? { __proto__: null };
351+
let options = objectGetOwn(config, 'options') ?? { __proto__: null };
352352
const help = objectGetOwn(config, 'help') ?? '';
353+
354+
const hasGenerateHelp = help.length > 0;
355+
const addHelpOption = objectGetOwn(config, 'addHelpOption') ?? hasGenerateHelp;
356+
const returnHelpText = objectGetOwn(config, 'returnHelpText') ?? (hasGenerateHelp || addHelpOption);
357+
358+
// Auto-inject help option if requested and not already present
359+
if (addHelpOption && !ObjectHasOwn(options, 'help')) {
360+
options = {
361+
...options,
362+
__proto__: null,
363+
help: {
364+
type: 'boolean',
365+
short: 'h',
366+
help: 'Show help',
367+
},
368+
};
369+
}
353370
// Bundle these up for passing to strict-mode checks.
354371
const parseConfig = { args, strict, options, allowPositionals, allowNegative };
355372

@@ -361,6 +378,8 @@ const parseArgs = (config = kEmptyObject) => {
361378
validateBoolean(allowNegative, 'allowNegative');
362379
validateObject(options, 'options');
363380
validateString(help, 'help');
381+
validateBoolean(addHelpOption, 'addHelpOption');
382+
validateBoolean(returnHelpText, 'returnHelpText');
364383
ArrayPrototypeForEach(
365384
ObjectEntries(options),
366385
({ 0: longOption, 1: optionConfig }) => {
@@ -447,22 +466,24 @@ const parseArgs = (config = kEmptyObject) => {
447466
}
448467
});
449468

450-
// Phase 4: generate print usage for each option
451-
let printUsage = '';
452-
if (help) {
453-
printUsage += help;
454-
}
455-
ArrayPrototypeForEach(ObjectEntries(options), ({ 0: longOption, 1: optionConfig }) => {
456-
const helpTextForPrint = formatHelpTextForPrint(longOption, optionConfig);
469+
// Phase 4: generate print usage for each option if requested
470+
if (returnHelpText) {
471+
let printUsage = '';
472+
if (help) {
473+
printUsage += help;
474+
}
475+
ArrayPrototypeForEach(ObjectEntries(options), ({ 0: longOption, 1: optionConfig }) => {
476+
const helpTextForPrint = formatHelpTextForPrint(longOption, optionConfig);
477+
478+
if (printUsage.length > 0) {
479+
printUsage += '\n';
480+
}
481+
printUsage += helpTextForPrint;
482+
});
457483

458484
if (printUsage.length > 0) {
459-
printUsage += '\n';
485+
result.helpText = printUsage;
460486
}
461-
printUsage += helpTextForPrint;
462-
});
463-
464-
if (help && printUsage.length > 0) {
465-
result.values.help = printUsage;
466487
}
467488

468489
return result;

0 commit comments

Comments
 (0)