From 7a71524c88906d2fbbfd7ede8dd16175560c9461 Mon Sep 17 00:00:00 2001 From: Mike Donnalley Date: Tue, 5 Mar 2024 14:34:49 -0700 Subject: [PATCH 1/2] feat: print ellipsis on arg if static is false --- src/help/command.ts | 3 ++- src/help/docopts.ts | 8 +++++--- test/help/format-command.test.ts | 19 +++++++++++++++++++ test/help/help-test-utils.ts | 2 +- 4 files changed, 27 insertions(+), 5 deletions(-) diff --git a/src/help/command.ts b/src/help/command.ts index 8ddbef8cd..a045a2141 100644 --- a/src/help/command.ts +++ b/src/help/command.ts @@ -59,7 +59,8 @@ export class CommandHelp extends HelpFormatter { if (args.filter((a) => a.description).length === 0) return return args.map((a) => { - const name = a.name.toUpperCase() + // Add ellipsis to indicate that the argument takes multiple values if static is false + const name = this.command.static === false ? `${a.name.toUpperCase()}...` : a.name.toUpperCase() let description = a.description || '' if (a.default) description = `${colorize(this.config?.theme?.flagDefaultValue, `[default: ${a.default}]`)} ${description}` diff --git a/src/help/docopts.ts b/src/help/docopts.ts index 5ae005cac..defcff0e8 100644 --- a/src/help/docopts.ts +++ b/src/help/docopts.ts @@ -61,7 +61,7 @@ export class DocOpts { private flagMap: {[index: string]: Command.Flag.Any} - public constructor(private cmd: Command.Cached | Command.Class | Command.Loadable) { + public constructor(private cmd: Command.Loadable) { // Create a new map with references to the flags that we can manipulate. this.flagMap = {} this.flagList = Object.entries(cmd.flags || {}) @@ -72,16 +72,18 @@ export class DocOpts { }) } - public static generate(cmd: Command.Cached | Command.Class | Command.Loadable): string { + public static generate(cmd: Command.Loadable): string { return new DocOpts(cmd).toString() } public toString(): string { const opts = ['<%= command.id %>'] if (this.cmd.args) { + // If static is false, add ellipsis to indicate that the argument takes multiple values + const suffix = this.cmd.static === false ? '...' : '' const a = Object.values(ensureArgObject(this.cmd.args)).map((arg) => - arg.required ? arg.name.toUpperCase() : `[${arg.name.toUpperCase()}]`, + arg.required ? `${arg.name.toUpperCase()}${suffix}` : `[${arg.name.toUpperCase()}${suffix}]`, ) || [] opts.push(...a) } diff --git a/test/help/format-command.test.ts b/test/help/format-command.test.ts index ed035e129..4d964ad57 100644 --- a/test/help/format-command.test.ts +++ b/test/help/format-command.test.ts @@ -517,6 +517,25 @@ FLAG DESCRIPTIONS ARGUMENTS ARG1 (option1|option2) Show the options`) }) + + it('should output arg with ... if static is false', async () => { + const cmd = await makeLoadable( + makeCommandClass({ + id: 'apps:create', + static: false, + args: { + arg1: Args.string({description: 'Show the options'}), + }, + }), + ) + + const output = help.formatCommand(cmd) + expect(output).to.equal(`USAGE + $ oclif apps:create [ARG1...] + +ARGUMENTS + ARG1... Show the options`) + }) }) describe('usage', () => { diff --git a/test/help/help-test-utils.ts b/test/help/help-test-utils.ts index 3461695a3..8da2d08d2 100644 --- a/test/help/help-test-utils.ts +++ b/test/help/help-test-utils.ts @@ -59,7 +59,7 @@ export async function makeLoadable(command: Command.Class): Promise): Command.Class { +export function makeCommandClass(cmdProps: Partial): Command.Class { return class extends Command { async run(): Promise { // do nothing From a509fa4f999511f767c3eb0afb1f37476da64213 Mon Sep 17 00:00:00 2001 From: Mike Donnalley Date: Mon, 11 Mar 2024 12:18:08 -0600 Subject: [PATCH 2/2] fix: use correct prop name --- src/help/command.ts | 4 ++-- src/help/docopts.ts | 4 ++-- test/help/format-command.test.ts | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/help/command.ts b/src/help/command.ts index a045a2141..2a090876c 100644 --- a/src/help/command.ts +++ b/src/help/command.ts @@ -59,8 +59,8 @@ export class CommandHelp extends HelpFormatter { if (args.filter((a) => a.description).length === 0) return return args.map((a) => { - // Add ellipsis to indicate that the argument takes multiple values if static is false - const name = this.command.static === false ? `${a.name.toUpperCase()}...` : a.name.toUpperCase() + // Add ellipsis to indicate that the argument takes multiple values if strict is false + const name = this.command.strict === false ? `${a.name.toUpperCase()}...` : a.name.toUpperCase() let description = a.description || '' if (a.default) description = `${colorize(this.config?.theme?.flagDefaultValue, `[default: ${a.default}]`)} ${description}` diff --git a/src/help/docopts.ts b/src/help/docopts.ts index defcff0e8..1cd80b45d 100644 --- a/src/help/docopts.ts +++ b/src/help/docopts.ts @@ -79,8 +79,8 @@ export class DocOpts { public toString(): string { const opts = ['<%= command.id %>'] if (this.cmd.args) { - // If static is false, add ellipsis to indicate that the argument takes multiple values - const suffix = this.cmd.static === false ? '...' : '' + // If strict is false, add ellipsis to indicate that the argument takes multiple values + const suffix = this.cmd.strict === false ? '...' : '' const a = Object.values(ensureArgObject(this.cmd.args)).map((arg) => arg.required ? `${arg.name.toUpperCase()}${suffix}` : `[${arg.name.toUpperCase()}${suffix}]`, diff --git a/test/help/format-command.test.ts b/test/help/format-command.test.ts index 4d964ad57..5d0cb5568 100644 --- a/test/help/format-command.test.ts +++ b/test/help/format-command.test.ts @@ -522,7 +522,7 @@ ARGUMENTS const cmd = await makeLoadable( makeCommandClass({ id: 'apps:create', - static: false, + strict: false, args: { arg1: Args.string({description: 'Show the options'}), },