twelf icon indicating copy to clipboard operation
twelf copied to clipboard

Incorrect behavior with struct flattening

Open Shatur opened this issue 11 months ago • 0 comments

Let's consider the following test:

#[test]
fn mixed_clap_flatten_defaults() {
    #[config]
    #[derive(Parser, Debug, Default)]
    #[clap(author, version, about, long_about = None)]
    struct Conf {
        #[clap(flatten)]
        package: Package,
    }

    #[derive(Args, Default, Debug, serde::Serialize, serde::Deserialize)]
    struct Package {
        #[clap(long, required = false)]
        edition: String,
    }

    let matches = Conf::command().get_matches_from(&["test", "--edition=2000"]);

    let prio = vec![Layer::Toml("Cargo.toml".into()), Layer::Clap(matches)];
    let config = Conf::with_layers(&prio).unwrap();

    assert_eq!(config.package.edition, "2000");
}

It panics at assert_eq!(config.package.edition, "2000");. I would expect CLI to override the value from file. But it prints 2021 because due to #[clap(flatten)] it incorrectly merges values from clap.

Shatur avatar Sep 08 '23 08:09 Shatur