Skip to content

Commit ece5b81

Browse files
boneskullclaude
andauthored
feat: add command alias support (#24)
Co-authored-by: Claude <noreply@anthropic.com>
1 parent 3a0b0cc commit ece5b81

8 files changed

Lines changed: 355 additions & 54 deletions

File tree

examples/nested-commands.ts

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,25 @@
44
*
55
* A git-like CLI that demonstrates:
66
*
7-
* - Nested command groups (e.g., `git remote add`)
7+
* - Nested command groups with aliases (e.g., `git r add` for `git remote add`)
88
* - Factory pattern for full type inference of parent globals
99
* - Unlimited nesting depth
1010
* - Parent globals flowing to nested handlers
1111
* - Default subcommands
12+
* - Command aliases at both parent and nested levels
1213
*
13-
* Usage: npx tsx examples/nested-commands.ts remote add origin
14-
* https://github.com/... npx tsx examples/nested-commands.ts remote remove
15-
* origin npx tsx examples/nested-commands.ts config get user.name npx tsx
16-
* examples/nested-commands.ts --verbose remote add origin https://... npx tsx
17-
* examples/nested-commands.ts --help npx tsx examples/nested-commands.ts remote
18-
* --help
14+
* @example
15+
*
16+
* ```sh
17+
* npx tsx examples/nested-commands.ts remote add origin https://github.com/...
18+
* npx tsx examples/nested-commands.ts r add origin https://... # 'r' alias
19+
* npx tsx examples/nested-commands.ts remote rm origin # 'rm' alias
20+
* npx tsx examples/nested-commands.ts config get user.name
21+
* npx tsx examples/nested-commands.ts cfg get user.name # 'cfg' alias
22+
* npx tsx examples/nested-commands.ts --verbose remote add origin https://...
23+
* npx tsx examples/nested-commands.ts --help
24+
* npx tsx examples/nested-commands.ts remote --help
25+
* ```
1926
*/
2027
import { bargs, opt, pos } from '../src/index.js';
2128

@@ -53,6 +60,7 @@ await bargs('git-like', {
5360
// ─────────────────────────────────────────────────────────────────────────────
5461
// FACTORY PATTERN: Full type inference for parent globals!
5562
// The factory receives a builder that already has parent globals typed.
63+
// Nested command groups can have aliases too (e.g., 'r' for 'remote')
5664
// ─────────────────────────────────────────────────────────────────────────────
5765
.command(
5866
'remote',
@@ -95,7 +103,8 @@ await bargs('git-like', {
95103
console.log(`Removed remote '${name}'`);
96104
}
97105
},
98-
'Remove a remote',
106+
// Subcommands can have aliases too
107+
{ aliases: ['rm', 'del'], description: 'Remove a remote' },
99108
)
100109
.command(
101110
'list',
@@ -114,10 +123,11 @@ await bargs('git-like', {
114123
}
115124
}
116125
},
117-
'List remotes',
126+
{ aliases: ['ls'], description: 'List remotes' },
118127
)
119128
.defaultCommand('list'),
120-
'Manage remotes',
129+
// Parent command group has aliases
130+
{ aliases: ['r'], description: 'Manage remotes' },
121131
)
122132

123133
// ─────────────────────────────────────────────────────────────────────────────
@@ -139,7 +149,7 @@ await bargs('git-like', {
139149
}
140150
console.log(value);
141151
},
142-
'Get a config value',
152+
{ aliases: ['g'], description: 'Get a config value' },
143153
)
144154
.command(
145155
'set',
@@ -155,9 +165,9 @@ await bargs('git-like', {
155165
console.log(`Set ${key} = ${value}`);
156166
}
157167
},
158-
'Set a config value',
168+
{ aliases: ['s'], description: 'Set a config value' },
159169
),
160-
'Manage configuration',
170+
{ aliases: ['cfg', 'c'], description: 'Manage configuration' },
161171
)
162172

163173
// ─────────────────────────────────────────────────────────────────────────────
@@ -174,7 +184,7 @@ await bargs('git-like', {
174184
console.log(`Config entries: ${config.size}`);
175185
}
176186
},
177-
'Show status',
187+
{ aliases: ['st', 's'], description: 'Show status' },
178188
)
179189
.defaultCommand('status')
180190
.parseAsync();

examples/tasks.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,17 @@
44
*
55
* A task manager that demonstrates:
66
*
7-
* - Multiple commands (add, list, done)
7+
* - Multiple commands with aliases (add/a/new, list/ls, done/complete/x)
88
* - Global options (--verbose, --file)
99
* - Command-specific options (--priority for add)
1010
* - Command positionals (task text)
1111
* - Full type inference with the (Parser, handler) API
1212
*
1313
* Usage: npx tsx examples/tasks.ts add "Buy groceries" --priority high npx tsx
14-
* examples/tasks.ts list npx tsx examples/tasks.ts done 1 npx tsx
15-
* examples/tasks.ts --help npx tsx examples/tasks.ts add --help
14+
* examples/tasks.ts a "Buy groceries" # same as 'add' npx tsx examples/tasks.ts
15+
* list npx tsx examples/tasks.ts ls # same as 'list' npx tsx examples/tasks.ts
16+
* done 1 npx tsx examples/tasks.ts x 1 # same as 'done' npx tsx
17+
* examples/tasks.ts --help
1618
*/
1719
import { bargs, opt, pos } from '../src/index.js';
1820

@@ -77,6 +79,7 @@ await bargs('tasks', {
7779
})
7880
.globals(globalOptions)
7981
// The handler receives merged global + command types
82+
// Use { description, aliases } for command aliases
8083
.command(
8184
'add',
8285
addParser,
@@ -98,7 +101,7 @@ await bargs('tasks', {
98101
console.log(`Added task #${task.id}: ${text}`);
99102
}
100103
},
101-
'Add a new task',
104+
{ aliases: ['a', 'new'], description: 'Add a new task' },
102105
)
103106
.command(
104107
'list',
@@ -131,7 +134,7 @@ await bargs('tasks', {
131134
}
132135
}
133136
},
134-
'List all tasks',
137+
{ aliases: ['ls'], description: 'List all tasks' },
135138
)
136139
.command(
137140
'done',
@@ -156,7 +159,7 @@ await bargs('tasks', {
156159
console.log(`Completed task #${id}: ${task.text}`);
157160
}
158161
},
159-
'Mark a task as complete',
162+
{ aliases: ['complete', 'x'], description: 'Mark a task as complete' },
160163
)
161164
.defaultCommand('list')
162165
.parseAsync();

0 commit comments

Comments
 (0)