From 43d2f87d6d7736f96d7d60f1d251b2d11f7f6b07 Mon Sep 17 00:00:00 2001 From: Wee Bit Date: Tue, 8 Aug 2023 03:46:56 +0300 Subject: [PATCH] Add user-friendly option help for strictOptionalOptionArguments --- lib/help.js | 15 +++++++++------ lib/option.js | 15 +++++++++++++++ typings/index.d.ts | 2 +- typings/index.test-d.ts | 1 + 4 files changed, 26 insertions(+), 7 deletions(-) diff --git a/lib/help.js b/lib/help.js index 14e0fb9f3..597f384ea 100644 --- a/lib/help.js +++ b/lib/help.js @@ -153,11 +153,14 @@ class Help { * Get the option term to show in the list of options. * * @param {Option} option + * @param {Command} [cmd] * @returns {string} */ - optionTerm(option) { - return option.flags; + optionTerm(option, cmd) { + return cmd?._strictOptionalOptionArguments && option.optional + ? option.strictOptionalHelpTerm + : option.flags; } /** @@ -195,7 +198,7 @@ class Help { longestOptionTermLength(cmd, helper) { return helper.visibleOptions(cmd).reduce((max, option) => { - return Math.max(max, helper.optionTerm(option).length); + return Math.max(max, helper.optionTerm(option, cmd).length); }, 0); } @@ -209,7 +212,7 @@ class Help { longestGlobalOptionTermLength(cmd, helper) { return helper.visibleGlobalOptions(cmd).reduce((max, option) => { - return Math.max(max, helper.optionTerm(option).length); + return Math.max(max, helper.optionTerm(option, cmd).length); }, 0); } @@ -380,7 +383,7 @@ class Help { // Options const optionList = helper.visibleOptions(cmd).map((option) => { - return formatItem(helper.optionTerm(option), helper.optionDescription(option)); + return formatItem(helper.optionTerm(option, cmd), helper.optionDescription(option)); }); if (optionList.length > 0) { output = output.concat(['Options:', formatList(optionList), '']); @@ -388,7 +391,7 @@ class Help { if (this.showGlobalOptions) { const globalOptionList = helper.visibleGlobalOptions(cmd).map((option) => { - return formatItem(helper.optionTerm(option), helper.optionDescription(option)); + return formatItem(helper.optionTerm(option, cmd), helper.optionDescription(option)); }); if (globalOptionList.length > 0) { output = output.concat(['Global Options:', formatList(globalOptionList), '']); diff --git a/lib/option.js b/lib/option.js index d61fc5f2f..cb8dff6f6 100644 --- a/lib/option.js +++ b/lib/option.js @@ -22,6 +22,21 @@ class Option { const optionFlags = splitOptionFlags(flags); this.short = optionFlags.shortFlag; this.long = optionFlags.longFlag; + this.strictOptionalHelpTerm = null; + if (this.optional) { + const { 0: argument, index } = flags.match(/\[.*\]/); + this.strictOptionalHelpTerm = ( + flags.slice(0, index) + flags.slice(index + argument.length) + ); + if (this.short) { + this.strictOptionalHelpTerm = this.strictOptionalHelpTerm + .replace(this.short, `${this.short}${argument}`); + } + if (this.long) { + this.strictOptionalHelpTerm = this.strictOptionalHelpTerm + .replace(this.long, `${this.long}=${argument}`); + } + } this.negate = false; if (this.long) { this.negate = this.long.startsWith('--no-'); diff --git a/typings/index.d.ts b/typings/index.d.ts index 12b2d63d6..f3be9eba1 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -207,7 +207,7 @@ export class Help { /** Get the command summary to show in the list of subcommands. */ subcommandDescription(cmd: Command): string; /** Get the option term to show in the list of options. */ - optionTerm(option: Option): string; + optionTerm(option: Option, cmd?: Command): string; /** Get the option description to show in the list of options. */ optionDescription(option: Option): string; /** Get the argument term to show in the list of arguments. */ diff --git a/typings/index.test-d.ts b/typings/index.test-d.ts index 734036fad..233c6e01b 100644 --- a/typings/index.test-d.ts +++ b/typings/index.test-d.ts @@ -377,6 +377,7 @@ expectType(helper.commandUsage(helperCommand)); expectType(helper.commandDescription(helperCommand)); expectType(helper.subcommandDescription(helperCommand)); expectType(helper.optionTerm(helperOption)); +expectType(helper.optionTerm(helperOption, helperCommand)); expectType(helper.optionDescription(helperOption)); expectType(helper.argumentTerm(helperArgument)); expectType(helper.argumentDescription(helperArgument));