Skip to content

Commit

Permalink
Add user-friendly option help for strictOptionalOptionArguments
Browse files Browse the repository at this point in the history
  • Loading branch information
aweebit committed Aug 8, 2023
1 parent 93f8723 commit 43d2f87
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
15 changes: 9 additions & 6 deletions lib/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand Down Expand Up @@ -380,15 +383,15 @@ 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), '']);
}

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), '']);
Expand Down
15 changes: 15 additions & 0 deletions lib/option.js
Original file line number Diff line number Diff line change
Expand Up @@ -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-');
Expand Down
2 changes: 1 addition & 1 deletion typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
1 change: 1 addition & 0 deletions typings/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@ expectType<string>(helper.commandUsage(helperCommand));
expectType<string>(helper.commandDescription(helperCommand));
expectType<string>(helper.subcommandDescription(helperCommand));
expectType<string>(helper.optionTerm(helperOption));
expectType<string>(helper.optionTerm(helperOption, helperCommand));
expectType<string>(helper.optionDescription(helperOption));
expectType<string>(helper.argumentTerm(helperArgument));
expectType<string>(helper.argumentDescription(helperArgument));
Expand Down

0 comments on commit 43d2f87

Please sign in to comment.