Closed stephenctw closed 2 years ago
That's normal behavior: the options of the other struct are not recognized by the others, and thus there is usage error.
What you can do is regrouping all your struct in one that group everything, and use flatten on each.
use structopt::StructOpt;
#[derive(StructOpt, Debug)]
struct AConfig {
#[structopt(short, long, env)]
pub ab: Option<String>,
#[structopt(short, long, env)]
pub cb: Option<String>,
}
#[derive(StructOpt, Debug)]
struct BConfig {
#[structopt(short, long, env)]
pub ba: Option<String>,
#[structopt(short, long, env)]
pub db: Option<String>,
}
#[derive(StructOpt, Debug)]
struct Opt {
#[structopt(flatten)]
a: AConfig,
#[structopt(flatten)]
b: BConfig,
}
fn main() {
let opt = Opt::from_args();
let b_config = opt.b;
let a_config = opt.a;
println!("{:?}", a_config);
println!("{:?}", b_config);
}
Thanks I'll give it a try. Is this behavior documented somewhere? I had a hard time looking for it in documentation or issues. Also I think throwing an explicit error can be very helpful.
Your dedicated use case is not explicitly documented, but flatten is already documented, yes.
I have a program that needs several structs that derive from
StructOpt
, yet they are in parallel not subcommand to each other. I want to achieve this with codeI was expecting this to work just fine, giving me all options in
help
, and accept them with no problem. But what I am seeing is only the first struct that callsfrom_args
is shown inhelp
, and none of the options actually work.The only thing I find working in this setup is the
env
. It works for bothAConfig
andBConfig