TeXitoi / structopt

Parse command line arguments by defining a struct.
Other
2.7k stars 149 forks source link

Description of flattened struct overwrites app description #539

Closed palant closed 4 months ago

palant commented 4 months ago

Consider the following application:

use structopt::StructOpt;

/// Inner description
#[derive(Debug, StructOpt)]
struct OptInner {}

/// Outer description
#[derive(Debug, StructOpt)]
struct OptOuter {
    #[structopt(flatten)]
    inner: OptInner
}

fn main() {
    OptOuter::from_args();
}

Running cargo run -- -h produces the following help text:

test 0.1.0
Inner description

USAGE:
    test

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

Note that the application description belongs to OptInner, not OptOuter as expected. Running cargo expand shows the issue:

let app = app.about("Outer description");
let app = <OptInner as ::structopt::StructOptInternal>::augment_clap(app);

So this first applies the attributes of the outer structure and calls augment_clap() of the inner structure then which will overwrite them.

The equivalent clap code does that in reverse order, producing the correct result:

let __clap_app = <OptInner as clap::Args>::augment_args(__clap_app);
__clap_app.about("Outer description").long_about(None)

For reference: https://github.com/clap-rs/clap/issues/2527

TeXitoi commented 4 months ago

Duplicate of #333