Commander.js: Allow for multiple aliases

Created on 10 May 2016  路  13Comments  路  Source: tj/commander.js

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.

enhancement

Most helpful comment

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.

All 13 comments

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.

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)

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

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

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(...);

This is already being supported?

@ganobrega Current status is:

  • commands support a single alias
  • options do not support an alias as such

Was a consensus ever reached on the option to support? If not I have yet another suggestion that is a bit of a middle-ground.

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.)

Any update on this?

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: https://github.com/tj/commander.js/issues/531#issuecomment-382184517

  .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

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?

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

Thank you for your contributions.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

winys picture winys  路  5Comments

mtrabelsi picture mtrabelsi  路  3Comments

youurayy picture youurayy  路  5Comments

aminch picture aminch  路  3Comments

san-templates picture san-templates  路  5Comments