33 * @license MIT
44 */
55
6+ import { COMMON_OPTIONS } from '../constants.ts'
67import { create , resolveBuiltInKey , resolveOptionKey } from '../utils.ts'
78
89import type { ArgOptions , ArgOptionSchema } from 'args-tokens'
910import 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 )
0 commit comments