Skip to content

Commit

Permalink
Merge pull request #1003 from oclif/mdonnalley/multi-arg-help
Browse files Browse the repository at this point in the history
feat: print ellipsis on arg if static is false
  • Loading branch information
shetzel committed Mar 11, 2024
2 parents ed87526 + a509fa4 commit c35e266
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/help/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 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}`
Expand Down
8 changes: 5 additions & 3 deletions src/help/docopts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 || {})
Expand All @@ -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 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() : `[${arg.name.toUpperCase()}]`,
arg.required ? `${arg.name.toUpperCase()}${suffix}` : `[${arg.name.toUpperCase()}${suffix}]`,
) || []
opts.push(...a)
}
Expand Down
19 changes: 19 additions & 0 deletions test/help/format-command.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
strict: 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', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/help/help-test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export async function makeLoadable(command: Command.Class): Promise<Command.Load
}
}

export function makeCommandClass(cmdProps: Partial<Command.Class>): Command.Class {
export function makeCommandClass(cmdProps: Partial<Command.Class & Command.Loadable>): Command.Class {
return class extends Command {
async run(): Promise<void> {
// do nothing
Expand Down

0 comments on commit c35e266

Please sign in to comment.