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 the ability to add alternative names for subcommand variants with #[clap] attribute #3639

Closed
2 tasks done
mitchmindtree opened this issue Apr 18, 2022 · 2 comments
Closed
2 tasks done
Labels
C-enhancement Category: Raise on the bar on expectations

Comments

@mitchmindtree
Copy link

Please complete the following tasks

Clap Version

3.1.9

Describe your use case

It would be nice if we could add alternative names for subcommands.

E.g. a Build(BuildCmd) command for cargo might offer:

cargo build

It would be nice if we could easily add shorthand aliases for this command. E.g.

cargo b

I believe this is currently possible for options, but I didn't come across a solution for subcommands.

Currently, the only solution I'm aware of for aliasing subcommands is to add further variants, e.g.

    Build(BuildCmd),
    B(BuildCmd),

however I think we run into duplicated help docs here.

Describe the solution you'd like

Perhaps something along the lines of

    #[clap(alias = "b")]
    Build

Or something like:

    #[clap(short)]
    Build

or

    #[clap(short = "b")]

By having a dedicated macro, we could generate dedicated documentation that describes the command is an alias or shorthand for the longer form.

Alternatives, if applicable

No response

Additional Context

No response

@mitchmindtree mitchmindtree added the C-enhancement Category: Raise on the bar on expectations label Apr 18, 2022
@epage
Copy link
Member

epage commented Apr 18, 2022

On the derive-reference, we explain under "Raw attributes" that any Command builder method can be used as an attribute.

If you then go over to Command, you'll see a link for subcommand settings, scroll down, and you'll see we have an alias method.

Both of these combined mean you can do

    #[clap(alias = "b")]
    Build

though that doesn't show up in the help by default. For that, you are looking for

    #[clap(visible_alias = "b")]
    Build

@mitchmindtree
Copy link
Author

Ahh thanks for the quick response, and apologies for the oversight! Hopefully this issue at leasts helps another clap wanderer in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: Raise on the bar on expectations
Projects
None yet
Development

No branches or pull requests

2 participants