Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -400,11 +400,11 @@ interface Options {
}
```

#### command.alias(name)
#### command.alias(...names)

- Type: `(name: string) => Command`
- Type: `(...names: (string | readonly string[])[]) => Command`

Add an alias name to this command, the `name` here can't contain brackets.
Add alias name(s) to this command, the alias names here can't contain brackets.

#### command.allowUnknownOptions()

Expand Down
4 changes: 2 additions & 2 deletions skills/cac/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ try {
- Use `ignoreOptionDefaultValue()` when help should show defaults but parsed output should stay sparse.
- Variadic positional args arrive as arrays.
- `options['--']` is reserved for everything after a double dash.
- `command.alias(name)` accepts plain aliases; use `alias('!')` to mark a named command as the default command.
- `command.alias(...names)` accepts one or more alias strings or arrays of strings; use `alias('!')` to mark a named command as the default command.
- Listen to `command:<name>`, `command:!`, and `command:*` when you need matched/default/unknown-command hooks.

## API Surface
Expand Down Expand Up @@ -171,7 +171,7 @@ cli.matchedCommandName
```ts
command.option(name, description, config?)
command.action(callback)
command.alias(name)
command.alias(...names)
command.allowUnknownOptions()
command.ignoreOptionDefaultValue()
command.example(example)
Expand Down
4 changes: 2 additions & 2 deletions src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ export class Command {
return this
}

alias(name: string): this {
this.aliasNames.push(name)
alias(...names: (string | readonly string[])[]): this {
this.aliasNames.push(...names.flat())
return this
}

Expand Down
82 changes: 82 additions & 0 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,3 +278,85 @@ test('reads process.argv when called, not when imported', () => {
process.argv = original
})
})

describe('command.alias()', () => {
test('multiple aliases via rest arguments', () => {
const cli = cac()
const events: string[] = []
let matchedFiles: string[] | undefined

const cmd = cli
.command('format [...files]', 'Format files')
.alias('fmt', 'fromat', 'foramt')
.action((files: string[]) => {
matchedFiles = files
})

for (const name of ['format', 'fmt', 'fromat', 'foramt']) {
cli.addEventListener(`command:${name}`, () => {
events.push(name)
})
}

expect(cmd.aliasNames).toEqual(['fmt', 'fromat', 'foramt'])

for (const name of ['format', 'fmt', 'fromat', 'foramt']) {
matchedFiles = []
cli.parse(['node', 'bin', name, 'a.ts', 'b.ts'])
expect(cli.matchedCommand).toBe(cmd)
expect(cli.matchedCommandName).toBe(name)
expect(matchedFiles).toEqual(['a.ts', 'b.ts'])
}

expect(events).toEqual(['format', 'fmt', 'fromat', 'foramt'])
})

test('multiple aliases via array and chained calls', () => {
const cli = cac()
let called = 0
const readonlyAliases = ['fromat', 'foramt'] as const

const cmd = cli
.command('format [...files]', 'Format files')
.alias(['fmt'])
.alias(readonlyAliases)
.alias('f')
.action(() => {
called++
})

expect(cmd.aliasNames).toEqual(['fmt', 'fromat', 'foramt', 'f'])

for (const name of ['fmt', 'fromat', 'foramt', 'f']) {
cli.parse(['node', 'bin', name])
expect(cli.matchedCommandName).toBe(name)
}

expect(called).toBe(4)
})

test('default command marker (!) combined with named aliases', () => {
const cli = cac()
const runs: string[] = []

const cmd = cli
.command('format [...files]', 'Format files')
.alias(['fmt', '!'])
.action((files: string[]) => {
runs.push(files.join(','))
})

expect(cmd.isDefaultCommand).toBe(true)

cli.parse(['node', 'bin', 'a.ts'])
expect(cli.matchedCommand).toBe(cmd)
expect(cli.matchedCommandName).toBeUndefined()

cli.unsetMatchedCommand()
cli.parse(['node', 'bin', 'fmt', 'b.ts'])
expect(cli.matchedCommand).toBe(cmd)
expect(cli.matchedCommandName).toBe('fmt')

expect(runs).toEqual(['a.ts', 'b.ts'])
})
})