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

SubCommand enum variant with fields duplicates parent attributes #418

Closed
pfernie opened this issue Jul 31, 2020 · 1 comment · Fixed by #504
Closed

SubCommand enum variant with fields duplicates parent attributes #418

pfernie opened this issue Jul 31, 2020 · 1 comment · Fixed by #504
Labels
bug This is a BUG. The fix may be released in a patch version even if considered breaking

Comments

@pfernie
Copy link

pfernie commented Jul 31, 2020

This example:

use structopt::StructOpt;

#[derive(Debug, StructOpt)]
struct Opts {
    #[structopt(subcommand)]
    /// The command to run
    command: Command,
}

#[derive(Debug, StructOpt)]
enum Command {
    /// Reticulate the splines
    #[structopt(visible_alias = "ret")]
    Reticulate {
        /// How many splines
        num_splines: u8,
    },
    /// Frobnicate the rest
    #[structopt(visible_alias = "frob")]
    Frobnicate,
}

fn main() {
    let opts = Opts::from_args();
    println!("{:?}", opts);
}

Produces this output:

$ cargo run -- -h
so_test 0.1.0

USAGE:
    so_test <SUBCOMMAND>

FLAGS:
    -h, --help       Prints help information
    -V, --version    Prints version information

SUBCOMMANDS:
    frobnicate    Frobnicate the rest [aliases: frob]
    help          Prints this message or the help of the given subcommand(s)
    reticulate    Reticulate the splines [aliases: ret, ret]

Where we see the reticulate command has the ret alias appear twice. Looking at the expanded code:

$ cargo expand
...
        let app = app.subcommand({
           let subcommand = ::structopt::clap::SubCommand::with_name("reticulate");
            let subcommand = {
                let subcommand = subcommand
                    .about("Reticulate the splines")
                    .visible_alias("ret");
                let subcommand = subcommand.arg(
                    ::structopt::clap::Arg::with_name("num-splines")
                        .takes_value(true)
                        .multiple(false)
                        .required(true)
                        .validator(|s| {
                            ::std::str::FromStr::from_str(s.as_str())
                                .map(|_: u8| ())
                                .map_err(|e| e.to_string())
                        })
                        .help("How many splines"),
                );
                subcommand.version("0.1.0")
            };
            subcommand
                .about("Reticulate the splines")
                .visible_alias("ret")
                .version("0.1.0")
        });
...

The about, visible_alias, and version functions on subcommand are called twice. In contrast, the Frobnicate case in the Command enum does not exhibit this behavior, as it does not contain any fields, so does not include a call subcommand.arg:

       let app = app.subcommand({
            let subcommand = ::structopt::clap::SubCommand::with_name("frobnicate");
            let subcommand = subcommand;
            subcommand
                .about("Frobnicate the rest")
                .visible_alias("frob")
                .version("0.1.0")
        });
        app.version("0.1.0")

I believe this duplication arises from this or this code, but I am not sure if simply removing would break other cases or not.

@0xazure
Copy link

0xazure commented Aug 16, 2020

I'm able to confirm the duplicate visible_alias in the help output. I'm upgrading from v0.2.15, and some quick tests show that the alias duplication starts appearing from v0.3.0 so I would consider this a regression.

@TeXitoi TeXitoi added the bug This is a BUG. The fix may be released in a patch version even if considered breaking label Sep 25, 2020
0xazure added a commit to 0xazure/sidecar that referenced this issue Nov 24, 2020
structopt currently has a bug [0] where it duplicates visible aliases in
the help output, so we remove command aliases until this bug is fixed.

[0]: TeXitoi/structopt#418
michiel-de-muynck added a commit to michiel-de-muynck/structopt that referenced this issue Oct 13, 2021
This commit fixes the issue where all top level methods were duplicated
for subcommands with fields (see issue TeXitoi#418). For some attributes, such
as aliases, this resulted in duplicate output.
TeXitoi pushed a commit that referenced this issue Oct 18, 2021
This commit fixes the issue where all top level methods were duplicated
for subcommands with fields (see issue #418). For some attributes, such
as aliases, this resulted in duplicate output.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This is a BUG. The fix may be released in a patch version even if considered breaking
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants