Skip to content

Commit 376b989

Browse files
committed
show defaults in descriptions
1 parent 5be80b9 commit 376b989

1 file changed

Lines changed: 32 additions & 21 deletions

File tree

src/index.ts

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ export class Massarg<Options extends OptionsBase = OptionsBase> {
4040
this._options.push({
4141
name: "help",
4242
aliases: ["h"],
43-
defaultValue: false,
4443
description: "Display help information",
4544
parse: Boolean,
4645
})
@@ -294,7 +293,9 @@ export class Massarg<Options extends OptionsBase = OptionsBase> {
294293
}
295294
}
296295

297-
private _getWrappedLines(list: Array<{ name: string; description?: string }>): string[] {
296+
private _getWrappedLines(
297+
list: Array<{ name: string; description?: string; additionalColorCount?: number }>
298+
): string[] {
298299
const { normalColors, highlightColors } = this._help
299300
const lines: string[] = []
300301

@@ -321,7 +322,13 @@ export class Massarg<Options extends OptionsBase = OptionsBase> {
321322

322323
for (const line of wrap(cmdName + cmdDescr, {
323324
indent: nameFullSize + INDENT_LEN,
324-
colorCount: this._help.useColors ? colorCount(normalColors, highlightColors) : 0,
325+
colorCount: this._help.useColors
326+
? colorCount(
327+
normalColors,
328+
highlightColors,
329+
item.additionalColorCount ? new Array({ length: item.additionalColorCount }) : []
330+
)
331+
: 0,
325332
firstLineIndent: INDENT_LEN,
326333
printWidth: this._help.printWidth,
327334
})) {
@@ -335,15 +342,9 @@ export class Massarg<Options extends OptionsBase = OptionsBase> {
335342
}
336343

337344
private _printCommands(): string[] {
338-
const lines: string[] = []
339-
for (const line of this._getWrappedLines(
345+
return this._getWrappedLines(
340346
this._commands.map((c) => ({ name: this._fullCmdName(c), description: c.description }))
341-
)) {
342-
if (line.trim().length) {
343-
lines.push(line)
344-
}
345-
}
346-
return lines
347+
)
347348
}
348349

349350
private _printOptions(): string[] {
@@ -357,12 +358,16 @@ export class Massarg<Options extends OptionsBase = OptionsBase> {
357358
const opts = this._commandOptions(cmd)
358359
if (opts.length) {
359360
commandOpts.push(this.color(subtitleColors, `${cmd.name}:`))
361+
commandOpts.push("")
362+
360363
for (const line of this._getWrappedLines(
361-
opts.map((c) => ({ name: this._fullOptName(c), description: c.description }))
364+
opts.map((c) => ({
365+
name: this._fullOptName(c),
366+
description: this._optionDescription(c),
367+
additionalColorCount: c.defaultValue !== undefined ? 1 : 0,
368+
}))
362369
)) {
363-
if (line.trim().length) {
364-
commandOpts.push(line)
365-
}
370+
commandOpts.push(line)
366371
}
367372
}
368373
}
@@ -372,7 +377,6 @@ export class Massarg<Options extends OptionsBase = OptionsBase> {
372377

373378
for (const line of commandOpts) {
374379
lines.push(line)
375-
lines.push("")
376380
}
377381

378382
const globalOpts = this._globalOptions()
@@ -382,17 +386,24 @@ export class Massarg<Options extends OptionsBase = OptionsBase> {
382386
lines.push("")
383387
}
384388
for (const line of this._getWrappedLines(
385-
globalOpts.map((c) => ({ name: this._fullOptName(c), description: c.description }))
389+
globalOpts.map((c) => ({ name: this._fullOptName(c), description: this._optionDescription(c) }))
386390
)) {
387-
if (line.trim().length) {
388-
lines.push(line)
389-
lines.push("")
390-
}
391+
lines.push(line)
391392
}
392393
}
393394
return lines
394395
}
395396

397+
private _optionDescription(c: OptionDef<Options, any>): string | undefined {
398+
if (c.defaultValue === undefined || !this._help.includeDefaults) {
399+
return c.description
400+
}
401+
402+
return [c.description!, this.color(this._help.bodyColors, `(default: ${c.defaultValue.toString().trim()})`)]
403+
.filter(Boolean)
404+
.join(" ")
405+
}
406+
396407
private _fullCmdName(cmd: CommandDef<Options>) {
397408
return [cmd.name, ...(cmd.aliases ?? [])].join(this._help.commandNameSeparator)
398409
}

0 commit comments

Comments
 (0)