Skip to content

Commit cc690cf

Browse files
authored
feat: show option value on help usage (#107)
* feat: show option value on help usage * refactor
1 parent 6369ab8 commit cc690cf

File tree

9 files changed

+79
-16
lines changed

9 files changed

+79
-16
lines changed

src/__snapshots__/cli.test.ts.snap

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ exports[`auto generate usage > loosely sub commands > command2 1`] = `
7979
COMMAND command2 <OPTIONS>
8080
8181
OPTIONS:
82-
-b, --bar [bar]
82+
-b, --bar [bar] (default: 42)
8383
-h, --help Display this help message
8484
-v, --version Display this version
8585
"
@@ -109,7 +109,7 @@ USAGE:
109109
COMMAND command2 <OPTIONS>
110110
111111
OPTIONS:
112-
-b, --bar [bar]
112+
-b, --bar [bar] (default: 42)
113113
-h, --help Display this help message
114114
-v, --version Display this version
115115
"
@@ -154,7 +154,7 @@ USAGE:
154154
gunshi command2 <OPTIONS>
155155
156156
OPTIONS:
157-
-b, --bar [bar] The bar option
157+
-b, --bar [bar] The bar option (default: 42)
158158
-h, --help Display this help message
159159
-v, --version Display this version
160160
@@ -225,7 +225,7 @@ USAGE:
225225
gunshi command2 <OPTIONS>
226226
227227
OPTIONS:
228-
-b, --bar [bar] The bar option
228+
-b, --bar [bar] The bar option (default: 42)
229229
-h, --help Display this help message
230230
-v, --version Display this version
231231
@@ -274,7 +274,7 @@ USAGE:
274274
OPTIONS:
275275
-f, --foo <foo> this is foo option
276276
--bar this is bar option
277-
-b, --baz [baz] this is baz option
277+
-b, --baz [baz] this is baz option (default: 42)
278278
-h, --help Display this help message
279279
-v, --version Display this version
280280
"

src/__snapshots__/generator.test.ts.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ USAGE:
66
generator <OPTIONS>
77
88
OPTIONS:
9-
-c, --catalog Display the catalog only
9+
-c, --catalog Display the catalog only (default: false)
1010
-d, --dependency Display the catalogable dependencies only
1111
--no-dependency Negatable of -d, --dependency
1212
-h, --help Display this help message
@@ -26,7 +26,7 @@ USAGE:
2626
generator show <OPTIONS>
2727
2828
OPTIONS:
29-
-c, --catalog Display the catalog only
29+
-c, --catalog Display the catalog only (default: false)
3030
-d, --dependency Display the catalogable dependencies only
3131
--no-dependency Negatable of -d, --dependency
3232
-h, --help Display this help message

src/__snapshots__/renderer.test.ts.snap

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ USAGE:
99
OPTIONS:
1010
-f, --foo <foo> The foo option
1111
--bar The bar option
12-
-b, --baz [baz] The baz option
12+
-b, --baz [baz] The baz option (default: 42)
1313
-q, --qux <qux> The qux option
1414
1515
EXAMPLES:
@@ -29,7 +29,7 @@ USAGE:
2929
OPTIONS:
3030
-f, --foo <foo> [string] The foo option
3131
--bar [boolean] The bar option
32-
-b, --baz [baz] [number] The baz option
32+
-b, --baz [baz] [number] The baz option (default: 42)
3333
-q, --qux <qux> [string] The qux option
3434
3535
EXAMPLES:
@@ -49,7 +49,7 @@ USAGE:
4949
OPTIONS:
5050
-f, --foo <foo> The foo option
5151
--bar The bar option
52-
-b, --baz [baz] The baz option
52+
-b, --baz [baz] The baz option (default: 42)
5353
-q, --qux <qux> The qux option
5454
"
5555
`;
@@ -77,7 +77,7 @@ USAGE:
7777
OPTIONS:
7878
-f, --foo <foo> The foo option
7979
--bar The bar option
80-
-b, --baz [baz] The baz option
80+
-b, --baz [baz] The baz option (default: 42)
8181
"
8282
`;
8383
@@ -99,8 +99,9 @@ For more info, run any command with the \`--help\` flag:
9999
OPTIONS:
100100
-f, --foo <foo> The foo option
101101
--bar The bar option
102-
-b, --baz [baz] The baz option
102+
-b, --baz [baz] The baz option (default: 42)
103103
-q, --qux <qux> The qux option
104+
-l, --log [log] The log option (default: info, choices: debug | info | warn | error)
104105
105106
EXAMPLES:
106107
# Example 1

src/constants.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export const COMMAND_OPTIONS_DEFAULT: CommandOptions<ArgOptions> = {
5555
leftMargin: 2,
5656
middleMargin: 10,
5757
usageOptionType: false,
58+
usageOptionValue: true,
5859
renderHeader: undefined,
5960
renderUsage: undefined,
6061
renderValidationErrors: undefined,
@@ -69,5 +70,7 @@ export const COMMAND_BUILTIN_RESOURCE_KEYS = [
6970
'OPTIONS',
7071
'EXAMPLES',
7172
'FORMORE',
72-
'NEGATABLE'
73+
'NEGATABLE',
74+
'DEFAULT',
75+
'CHOICES'
7376
] as const

src/locales/en-US.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
"EXAMPLES": "EXAMPLES",
88
"FORMORE": "For more info, run any command with the `--help` flag:",
99
"NEGATABLE": "Negatable of",
10+
"DEFAULT": "default",
11+
"CHOICES": "choices",
1012
"help": "Display this help message",
1113
"version": "Display this version"
1214
}

src/locales/ja-JP.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
"EXAMPLES": "",
88
"FORMORE": "詳細は、コマンドと`--help`フラグを実行してください:",
99
"NEGATABLE": "否定可能な",
10+
"DEFAULT": "デフォルト",
11+
"CHOICES": "選択肢",
1012
"help": "このヘルプメッセージを表示",
1113
"version": "このバージョンを表示"
1214
}

src/renderer.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ const SHOW = {
3333
short: 'q',
3434
required: true,
3535
description: 'The qux option'
36+
},
37+
log: {
38+
type: 'enum',
39+
short: 'l',
40+
description: 'The log option',
41+
choices: ['debug', 'info', 'warn', 'error'],
42+
default: 'info'
3643
}
3744
},
3845
name: 'show',

src/renderer/usage.ts

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
* @license MIT
44
*/
55

6+
import { COMMON_OPTIONS } from '../constants.ts'
67
import { create, resolveBuiltInKey, resolveOptionKey } from '../utils.ts'
78

89
import type { ArgOptions, ArgOptionSchema } from 'args-tokens'
910
import type { Command, CommandContext } from '../types.ts'
1011

12+
const COMMON_OPTIONS_KEYS = Object.keys(COMMON_OPTIONS)
13+
1114
/**
1215
* Render the usage.
1316
* @param ctx A {@link CommandContext | command context}
@@ -243,7 +246,7 @@ function getOptionsPairs<Options extends ArgOptions>(
243246
key = value.default ? `${key} [${name}]` : `${key} <${name}>`
244247
}
245248
acc[name] = key
246-
if (value.type === 'boolean' && value.negatable && !(name === 'help' || name === 'version')) {
249+
if (value.type === 'boolean' && value.negatable && !COMMON_OPTIONS_KEYS.includes(name)) {
247250
acc[`no-${name}`] = `--no-${name}`
248251
}
249252
return acc
@@ -259,6 +262,40 @@ function resolveNegatableType<Options extends ArgOptions>(
259262
return ctx.options[key.startsWith('no-') ? resolveNegatableKey(key) : key].type
260263
}
261264

265+
function generateDefaultDisplayValue<Options extends ArgOptions>(
266+
ctx: Readonly<CommandContext<Options>>,
267+
schema: ArgOptionSchema
268+
): string {
269+
return `${ctx.translate(resolveBuiltInKey('DEFAULT'))}: ${schema.default}`
270+
}
271+
272+
function resolveDisplayValue<Options extends ArgOptions>(
273+
ctx: Readonly<CommandContext<Options>>,
274+
key: string
275+
): string {
276+
if (COMMON_OPTIONS_KEYS.includes(key)) {
277+
return ''
278+
}
279+
280+
const schema = ctx.options[key]
281+
if (
282+
(schema.type === 'boolean' || schema.type === 'number' || schema.type === 'string') &&
283+
schema.default !== undefined
284+
) {
285+
return `(${generateDefaultDisplayValue(ctx, schema)})`
286+
}
287+
if (schema.type === 'enum') {
288+
const _default =
289+
schema.default !== undefined // eslint-disable-line unicorn/no-negated-condition
290+
? generateDefaultDisplayValue(ctx, schema)
291+
: ''
292+
const choices = `${ctx.translate(resolveBuiltInKey('CHOICES'))}: ${schema.choices!.join(' | ')}`
293+
return `(${_default ? `${_default}, ${choices}` : choices})`
294+
}
295+
296+
return ''
297+
}
298+
262299
/**
263300
* Generate options usage
264301
* @param ctx A {@link CommandContext | command context}
@@ -289,9 +326,10 @@ async function generateOptionsUsage<Options extends ArgOptions>(
289326
rawDesc = `${ctx.translate(resolveBuiltInKey('NEGATABLE'))} ${optionKey}`
290327
}
291328
const optionsSchema = ctx.env.usageOptionType ? `[${resolveNegatableType(key, ctx)}] ` : ''
329+
const valueDesc = key.startsWith('no-') ? '' : resolveDisplayValue(ctx, key)
292330
// padEnd is used to align the `[]` symbols
293331
const desc = `${optionsSchema ? optionsSchema.padEnd(optionSchemaMaxLength + 3) : ''}${rawDesc}`
294-
const option = `${value.padEnd(optionsMaxLength + ctx.env.middleMargin)}${desc}`
332+
const option = `${value.padEnd(optionsMaxLength + ctx.env.middleMargin)}${desc}${valueDesc ? ` ${valueDesc}` : ''}`
295333
return `${option.padStart(ctx.env.leftMargin + option.length)}`
296334
})
297335
)

src/types.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,16 @@ export interface CommandEnvironment<Options extends ArgOptions = ArgOptions> {
111111
* @see {@link CommandOptions.usageOptionType}
112112
*/
113113
usageOptionType: boolean
114+
/**
115+
* Whether to display the option value.
116+
* @default true
117+
* @see {@link CommandOptions.usageOptionValue}
118+
*/
119+
usageOptionValue: boolean
114120
/**
115121
* Whether to display the command usage.
116122
* @default false
117-
* @see {@link}
123+
* @see {@link CommandOptions.usageSilent}
118124
*/
119125
usageSilent: boolean
120126
/**
@@ -180,6 +186,10 @@ export interface CommandOptions<Options extends ArgOptions = ArgOptions> {
180186
* Whether to display the usage option type.
181187
*/
182188
usageOptionType?: boolean
189+
/**
190+
* Whether to display the option value.
191+
*/
192+
usageOptionValue?: boolean
183193
/**
184194
* Whether to display the command usage.
185195
*/

0 commit comments

Comments
 (0)