vilicvane / clime

⌨ The command-line interface framework for TypeScript.
252 stars 10 forks source link

Enable setting the alias via @command() decorator. #34

Open x3rAx opened 7 years ago

x3rAx commented 7 years ago

I think it would be nice to be able to set the alias inside the subcommand file using the @command() decorator.

It is currently only possible to defined aliases in the parent command by exporting a SubcommandDefinition array:

import { SubcommandDefinition } from 'clime'

export const subcommands: SubcommandDefinition[] = [
    {
        name: 'my-really-annoying-long-but-descriptive-command',
        alias: 'do-it',
    }
]

This way, I will have to maintain the alias in a separate file from the actual subcommand definition file. And it is very easy to forget changing the command definition in the parent command when for example renaming the subcommand.

In this case one would end up with the new command name (without an alias) and the old one (with alias but not working as it is not defined anymore).

SUBCOMMANDS

  my-really-annoying-long-but-descriptive-command [do-it]
  my-long-but-descriptive-command                         - Isn't that a long command?

When setting the alias in the @command() decorator, this would not happen.

Drawbacks

Using this method, every subcommand has to be loaded when executing a subcommand using its alias because clime does not know which command has the given alias. This might produce some unwanted IO, especially in larger applications with many subcommands. But I don't know if this would really be a problem

Solution 1

This behavior could be configurable while bootstrapping clime:

let cli = new CLI(
    Path.basename(process.argv[1]),  // App name
    Path.join(__dirname, 'commands') // Path to commands
    {
        aliasInSubcommand: false // I would prefer it to be `true` by default
    }
);

Solution 2

Don't allow this at all if there are too may concerns but at least let the developer know that something is wrong. (See #33)

vilicvane commented 7 years ago

@x3rAx Hi, thanks for your suggestions, though I would prefer the way #33 suggests.

Actually I would agree that it might not be a real performance issue most of the time, not to mention we could have an option for that. However, it's not necessary either as the subcommands entry would be seldom edited. 😂