-
Notifications
You must be signed in to change notification settings - Fork 152
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
Option::unwrap on nested flatten/subcommand #388
Comments
What is the expected behavior? I can't find anything really meaning full from this example. |
I would have thought structopt should never just call unwrap on a None, and that even on unexpected StructOpt derivations should at least give some error message. In this case, I expect the behaviour would be equivalent to This example was boiled down from a larger example which broke when I tried to insert a few additional |
Hm I would actually expect this to work or, at least, show a meaningful panic message. I'll see what can be done in... a number of days. Although, I have one concern about flattening structs that contain subcommands. There must be at most one field marked with enum Sub {}
// You CAN'T do that
struct Opts {
#[structopt(subcommand)]
s1: Sub,
#[structopt(subcommand)]
s2: Sub,
} Structopt can check this case and abort with a compile-time error, that's cool. But it gets really complicated once flattening is in play:
Structopt can't hold you hand here because it doesn't know what's at the other end of the flatten at compile time. This kind of invariant is up to user to keep upheld. It gets only worse with a possibility of transitive flatten... |
So, TLDR, it should fail at compile time because we can handle only one subcommand in the type tree, but that's complicated to implement with flatten. You validate @CreepySkeleton ? |
Yes. The only way to use it correctly is struct Opt {
#[flatten]
flatten: Flat
}
struct Flat {
#[subcommand]
sub: Sub,
opt: String
} Which is no different from: struct Opt {
#[flatten]
flatten: Flat,
#[subcommand]
sub: Sub,
}
struct Flat {
opt: String
} I doubt we can detect this at compile time, but I think I'll be able to improve the panic message. |
Using
structopt-0.3.14
and Rustnightly-2020-04-20
the following program when run withcargo run -- command foo
gives a panicOption::unwrap() on a None
.RUST_BACKTRACE=1
points to the generatedfrom_clap
as containing the error.Note that changing
Enum3
tostruct Enum3 {args: Vec<String>}
also seems to behave unexpectedly as it always displays the help instead of returning a valid result.The text was updated successfully, but these errors were encountered: