Skip to content

Commit 50d88de

Browse files
committed
Add user-friendly option help for strictOptionalOptionArguments
1 parent 733d9cd commit 50d88de

4 files changed

Lines changed: 26 additions & 7 deletions

File tree

lib/help.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,14 @@ class Help {
153153
* Get the option term to show in the list of options.
154154
*
155155
* @param {Option} option
156+
* @param {Command} [cmd]
156157
* @returns {string}
157158
*/
158159

159-
optionTerm(option) {
160-
return option.flags;
160+
optionTerm(option, cmd) {
161+
return cmd?._strictOptionalOptionArguments && option.optional
162+
? option.strictOptionalHelpTerm
163+
: option.flags;
161164
}
162165

163166
/**
@@ -195,7 +198,7 @@ class Help {
195198

196199
longestOptionTermLength(cmd, helper) {
197200
return helper.visibleOptions(cmd).reduce((max, option) => {
198-
return Math.max(max, helper.optionTerm(option).length);
201+
return Math.max(max, helper.optionTerm(option, cmd).length);
199202
}, 0);
200203
}
201204

@@ -209,7 +212,7 @@ class Help {
209212

210213
longestGlobalOptionTermLength(cmd, helper) {
211214
return helper.visibleGlobalOptions(cmd).reduce((max, option) => {
212-
return Math.max(max, helper.optionTerm(option).length);
215+
return Math.max(max, helper.optionTerm(option, cmd).length);
213216
}, 0);
214217
}
215218

@@ -380,15 +383,15 @@ class Help {
380383

381384
// Options
382385
const optionList = helper.visibleOptions(cmd).map((option) => {
383-
return formatItem(helper.optionTerm(option), helper.optionDescription(option));
386+
return formatItem(helper.optionTerm(option, cmd), helper.optionDescription(option));
384387
});
385388
if (optionList.length > 0) {
386389
output = output.concat(['Options:', formatList(optionList), '']);
387390
}
388391

389392
if (this.showGlobalOptions) {
390393
const globalOptionList = helper.visibleGlobalOptions(cmd).map((option) => {
391-
return formatItem(helper.optionTerm(option), helper.optionDescription(option));
394+
return formatItem(helper.optionTerm(option, cmd), helper.optionDescription(option));
392395
});
393396
if (globalOptionList.length > 0) {
394397
output = output.concat(['Global Options:', formatList(globalOptionList), '']);

lib/option.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,21 @@ class Option {
2222
const optionFlags = splitOptionFlags(flags);
2323
this.short = optionFlags.shortFlag;
2424
this.long = optionFlags.longFlag;
25+
this.strictOptionalHelpTerm = null;
26+
if (this.optional) {
27+
const { 0: argument, index } = flags.match(/\[.*\]/);
28+
this.strictOptionalHelpTerm = (
29+
flags.slice(0, index) + flags.slice(index + argument.length)
30+
);
31+
if (this.short) {
32+
this.strictOptionalHelpTerm = this.strictOptionalHelpTerm
33+
.replace(this.short, `${this.short}${argument}`);
34+
}
35+
if (this.long) {
36+
this.strictOptionalHelpTerm = this.strictOptionalHelpTerm
37+
.replace(this.long, `${this.long}=${argument}`);
38+
}
39+
}
2540
this.negate = false;
2641
if (this.long) {
2742
this.negate = this.long.startsWith('--no-');

typings/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ export class Help {
207207
/** Get the command summary to show in the list of subcommands. */
208208
subcommandDescription(cmd: Command): string;
209209
/** Get the option term to show in the list of options. */
210-
optionTerm(option: Option): string;
210+
optionTerm(option: Option, cmd?: Command): string;
211211
/** Get the option description to show in the list of options. */
212212
optionDescription(option: Option): string;
213213
/** Get the argument term to show in the list of arguments. */

typings/index.test-d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@ expectType<string>(helper.commandUsage(helperCommand));
377377
expectType<string>(helper.commandDescription(helperCommand));
378378
expectType<string>(helper.subcommandDescription(helperCommand));
379379
expectType<string>(helper.optionTerm(helperOption));
380+
expectType<string>(helper.optionTerm(helperOption, helperCommand));
380381
expectType<string>(helper.optionDescription(helperOption));
381382
expectType<string>(helper.argumentTerm(helperArgument));
382383
expectType<string>(helper.argumentDescription(helperArgument));

0 commit comments

Comments
 (0)