TeXitoi / structopt

Parse command line arguments by defining a struct.
Other
2.71k stars 151 forks source link

Registering a cargo subcommand alias? #458

Closed k0pernicus closed 3 years ago

k0pernicus commented 3 years ago

Hi,
We are working on cargo-generate, a cargo subcommand to generate easily a Rust project based on a template.

We have an issue reported by one of our users here as, after a cargo install, the subcommand alias (gen) is not recognized.

As explained in the issue, cargo run -- gen is working, but the issue is reported only after installing the binary using cargo install.

Our current implementation:

#[derive(StructOpt)]
#[structopt(bin_name = "cargo")]
pub enum Cli {
    #[structopt(name = "generate", visible_alias = "gen")]
    Generate(Args),
}

Using this code, cargo run -- generate and cargo run -- gen works.
However, after a cargo install, cargo generate works but not cargo gen:

➜  cargo-generate git:(975ea2c) cargo gen --version   
error: no such subcommand: `gen`

        Did you mean `new`?

I checked quickly the documentation of cargo and did not found something "interesting" about registering the aliases, except maybe registering manually the alias in the ~/.cargo/config...

I am not sure that structopt is doing something at this level, as the cargo documentation explained that they are checking if a binary is a cargo subcommand based on the name of the binary, but I wanted to ask the question just in case.

Thanks in advance!

TeXitoi commented 3 years ago

Nothing to do with clap/structopt, that's cargo that has ni way to know that it must forward gen to the generate binary.

k0pernicus commented 3 years ago

Thanks - I suspected it, but I preferred to check before :)