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

Allow for multiple aliases #531

Closed
cbetta opened this issue May 10, 2016 · 13 comments
Closed

Allow for multiple aliases #531

cbetta opened this issue May 10, 2016 · 13 comments

Comments

@cbetta
Copy link

cbetta commented May 10, 2016

I'd love for commander allow to allow for multiple aliases. I've found them to be useful for many reasons varying from default subcommands (records:list and records, record:show and record), preventing typos (record:list vs records:list) and just shortcuts (records:list and rl).

Currently I'm resorting to duplicating my command statements which feels far from effective.

@cbetta
Copy link
Author

cbetta commented May 10, 2016

I'd be happy to add this but would love feedback on the syntax.

Some requirements:

  • Ability to allow for more than 1 alias
  • Allow for selecting what aliases are show in the help screen

Option 1

import commander from 'commander';

commander
  .command('link:create <a> <b>')
  .alias('l', 'lc', 'link')
  .action(...);

Then --help should look something like:

> example --help
   Usage: example [options] [command]

   Commands:

      link:create|l|lc|link <a> <b>                         

Which might get a bit excessive. Instead some aliases might be hidden instead.

Option 2

import commander from 'commander';

commander
  .command('link:create <a> <b>')
  .alias('link')
  .hidden_alias('l', 'lc')
  .action(...);

Which would then result in:

> example --help
   Usage: example [options] [command]

   Commands:
      link:create|link <a> <b>                         

Option 3

The alternative would be to pass an object with the keys of the aliases and the values of the visibility:

import commander from 'commander';

commander
  .command('link:create <a> <b>')
  .alias({ link: true, lc: false, l: false})
  .action(...);

Option 4

Finally the last option would be to define aliases outside of the command scope:

import commander from 'commander';

commander
  .command('link:create <a> <b>')
  .alias('link')
  .action(...);

commander.alias('link:create', {lc: false, l: false}); 

I personally prefer option 2 but I'd love some feedback.

@cbetta cbetta changed the title Allow for multiple aliases? Allow for multiple aliases May 10, 2016
@kodie
Copy link

kodie commented Apr 17, 2018

Would love to see support for multiple aliases.

I'd personally prefer something like this:

import commander from 'commander';

commander
  .command('list')
  .alias('ls')
  .alias('l', false)
  .action(...);

Where alias() can be used multiple times just like option() and accepts a boolean as a second parameter stating if it's visible or not in help (defaults to true)

@tdreid
Copy link

tdreid commented Jul 29, 2018

@kodie 's suggestion is what occurred to me to try naturally. That's how I discovered not yet implemented :)

@kepexx
Copy link

kepexx commented Dec 8, 2018

Agreed with @tdreid and @kodie , it's the most natural in my opinion

@ioxua
Copy link

ioxua commented Dec 23, 2018

Well, Commander already has support for a kind of "aliasing" on options:

import commander from 'commander';

commander
  .option('-f, --foo', 'Foo option')
  .parse(process.argv);

Why not reuse the syntax for commands instead? It would look like this:

import commander from 'commander';

commander
  .command('link, link:create <a> <b>')
  .action(...);

@ganobrega

This comment has been minimized.

@shadowspawn

This comment has been minimized.

@DuckyCrayfish

This comment has been minimized.

@shadowspawn
Copy link
Collaborator

shadowspawn commented Apr 9, 2019

Not consensus, throw in another idea @DuckyCrayfish

I personally lean towards just repeating .alias or allowing array, and not offering ability to hide. Keep it simple.

For the help, an approach I have used in another product is to list the aliases in a separate help section. This avoids cluttering up the main command help with the variations. e.g.

Aliases:
  i: install
  list: ls
  lsr, list-remote: ls-remote
  remove: rm

(The "real" commands are listed on the right.)

@nrgapple

This comment has been minimized.

@shadowspawn
Copy link
Collaborator

shadowspawn commented Mar 28, 2020

I am interested in the approach of allowing repeated .alias(), as no new syntax.

Hiding some aliases was listed in the original requirements and followed through in the examples. I am reluctant to add a plain boolean parameter for a somewhat uncommon feature, as it is not extensible or self documenting. But just listing all the aliases when there are lots will be a bit messy in the current format in the help!

Would listing just the first alias in the built-in help be a usable compromise?

So for @kodie example: #531 (comment)

commander
  .command('list')
  .alias('ls') // visible
  .alias('l') // hidden
  .action(...);
Usage: index [options] [command]

Options:
  -h, --help      display help for command

Commands:
  list|ls
  help [command]  display help for command

shadowspawn added a commit to shadowspawn/commander.js that referenced this issue Apr 2, 2020
shadowspawn added a commit to shadowspawn/commander.js that referenced this issue Apr 2, 2020
@shadowspawn
Copy link
Collaborator

shadowspawn commented Apr 2, 2020

I have written a PR with support for multiple aliases, the first of which is included in the autogenerated help: #1236

Interested readers, is this an adequate implementation for the feature request to allow multiple aliases?

shadowspawn added a commit that referenced this issue Apr 21, 2020
* Allow for multiple aliases #531

* Add tests for multiple alias behaviours

* Add aliases() mainly for getter

* Fix typo
@shadowspawn shadowspawn added the pending release Merged into a branch for a future release, but not released yet label Apr 21, 2020
@shadowspawn shadowspawn self-assigned this Apr 23, 2020
@shadowspawn
Copy link
Collaborator

Support for multiple aliases has been released in Command v5.1.

Thank you for your contributions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants