Skip to content

Commit 91bfa8d

Browse files
authored
Add mention of .alias() to README. (#1833)
1 parent 5a201ec commit 91bfa8d

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

Readme.md

+2
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,8 @@ Configuration options can be passed with the call to `.command()` and `.addComma
543543
remove the command from the generated help output. Specifying `isDefault: true` will run the subcommand if no other
544544
subcommand is specified ([example](./examples/defaultCommand.js)).
545545

546+
You can add alternative names for a command with `.alias()`. ([example](./examples/alias.js))
547+
546548
For safety, `.addCommand()` does not automatically copy the inherited settings from the parent command. There is a helper routine `.copyInheritedSettings()` for copying the settings when they are wanted.
547549

548550
### Command-arguments

examples/alias.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env node
2+
3+
// This example shows giving alternative names for a command.
4+
5+
// const { Command } = require('commander'); // (normal include)
6+
const { Command } = require('../'); // include commander in git clone of commander repo
7+
const program = new Command();
8+
9+
program
10+
.command('exec')
11+
.argument('<script>')
12+
.alias('ex')
13+
.action((script) => {
14+
console.log(`execute: ${script}`);
15+
});
16+
17+
program
18+
.command('print')
19+
.argument('<file>')
20+
// Multiple aliases is unusual but supported! You can call alias multiple times,
21+
// and/or add multiple aliases at once. Only the first alias is displayed in the help.
22+
.alias('p')
23+
.alias('pr')
24+
.aliases(['display', 'show'])
25+
.action((file) => {
26+
console.log(`print: ${file}`);
27+
});
28+
29+
program.parse();
30+
31+
// Try the following:
32+
// node alias.js --help
33+
// node alias.js exec script
34+
// node alias.js ex script
35+
// node alias.js print file
36+
// node alias.js pr file
37+
// node alias.js show file

0 commit comments

Comments
 (0)