Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mention of .alias() to README. #1833

Merged
merged 1 commit into from
Dec 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,8 @@ Configuration options can be passed with the call to `.command()` and `.addComma
remove the command from the generated help output. Specifying `isDefault: true` will run the subcommand if no other
subcommand is specified ([example](./examples/defaultCommand.js)).

You can add alternative names for a command with `.alias()`. ([example](./examples/alias.js))

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.

### Command-arguments
Expand Down
37 changes: 37 additions & 0 deletions examples/alias.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env node

// This example shows giving alternative names for a command.

// const { Command } = require('commander'); // (normal include)
const { Command } = require('../'); // include commander in git clone of commander repo
const program = new Command();

program
.command('exec')
.argument('<script>')
.alias('ex')
.action((script) => {
console.log(`execute: ${script}`);
});

program
.command('print')
.argument('<file>')
// Multiple aliases is unusual but supported! You can call alias multiple times,
// and/or add multiple aliases at once. Only the first alias is displayed in the help.
.alias('p')
.alias('pr')
.aliases(['display', 'show'])
.action((file) => {
console.log(`print: ${file}`);
});

program.parse();

// Try the following:
// node alias.js --help
// node alias.js exec script
// node alias.js ex script
// node alias.js print file
// node alias.js pr file
// node alias.js show file