command-line-api icon indicating copy to clipboard operation
command-line-api copied to clipboard

Allow adding aliases to command using object initializer

Open CCRcmcpe opened this issue 4 years ago • 4 comments

This is a simple example:

var rootCommand = new RootCommand
{
    new Command("do-something")
    {
        new Command("abcdefg") // How to add aliases here?
        new Command("qwerty")
    }
};

In this case, you have to give up using the object initializer and manually add the commands. This supposed example demonstrates an easy way to build commands.

var rootCommand = new RootCommand
{
    new Command("do-something")
    {
        new Command("abcdefg") { Aliases = { "c", "w" } } // In reality, this will raise a compiler error, because Aliases is a IReadOnlyList<string>
        new Command("qwerty")
    }
};

CCRcmcpe avatar Apr 27 '21 05:04 CCRcmcpe

Just an additional note: If the public API of Command is being updated/changed to allow such functionality, please also make it consistent with Option/Option<T>. Currently, Option/Option<T> allows specifying aliases through constructor arguments and an AddAlias method, whereas a Command exposes the same feature only through an AddAlias method.

elgonzo avatar Apr 27 '21 10:04 elgonzo

Just an additional note: If the public API of Command is being updated/changed to allow such functionality, please also make it consistent with Option/Option<T>. Currently, Option/Option<T> allows specifying aliases through constructor arguments and an AddAlias method, whereas a Command exposes the same feature only through an AddAlias method.

Agreed. More generally, the whole construction of the command structure should be able to be done in one go by the object initializer.

CCRcmcpe avatar Apr 27 '21 14:04 CCRcmcpe

The whole pack, Option/Argument/Command should probably be changed to records so this all comes mostly for free :)

kzu avatar Apr 28 '21 18:04 kzu

Would also be great if we can use the .AddAlias() in a fluent way. new Command() .AddAlias() .AddAlias();

DavidVTurley avatar Oct 24 '24 08:10 DavidVTurley