From 73940fe4b1c040f9548710b3b85a0f86676dca2f Mon Sep 17 00:00:00 2001 From: ychampion <68075205+ychampion@users.noreply.github.com> Date: Sat, 14 Nov 2026 16:15:18 +0530 Subject: [PATCH] feat(command): support multiple aliases in alias() Allow command.alias() to register multiple aliases via rest arguments or string arrays while preserving single-string and chained calls. Fixes #57 --- README.md | 6 ++-- skills/cac/SKILL.md | 4 +-- src/command.ts | 4 +-- tests/index.test.ts | 82 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 89 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index f5ad0e89..76269991 100644 --- a/README.md +++ b/README.md @@ -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() diff --git a/skills/cac/SKILL.md b/skills/cac/SKILL.md index 274001fa..569fc40b 100644 --- a/skills/cac/SKILL.md +++ b/skills/cac/SKILL.md @@ -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:`, `command:!`, and `command:*` when you need matched/default/unknown-command hooks. ## API Surface @@ -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) diff --git a/src/command.ts b/src/command.ts index a76d143e..9f92dd26 100644 --- a/src/command.ts +++ b/src/command.ts @@ -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 } diff --git a/tests/index.test.ts b/tests/index.test.ts index 6927d442..e4c02a8f 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -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']) + }) +})