Skip to content

Commit

Permalink
Flattening a subcommand now panics with a meaningful error message
Browse files Browse the repository at this point in the history
  • Loading branch information
CreepySkeleton committed Jun 12, 2020
1 parent caa1455 commit 776fe37
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
3 changes: 2 additions & 1 deletion structopt-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,8 @@ fn gen_from_clap_enum(name: &Ident) -> TokenStream {
quote! {
fn from_clap(matches: &::structopt::clap::ArgMatches) -> Self {
<#name as ::structopt::StructOptInternal>::from_subcommand(matches.subcommand())
.unwrap()
.expect("structopt misuse: You likely tried to #[flatten] a struct \
that contains #[subcommand]. This is forbidden.")
}
}
}
Expand Down
24 changes: 24 additions & 0 deletions tests/flatten.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,27 @@ fn merge_subcommands_with_flatten() {
Opt::from_iter(&["test", "command2", "43"])
);
}

#[test]
#[should_panic = "structopt misuse: You likely tried to #[flatten] a struct \
that contains #[subcommand]. This is forbidden."]
fn subcommand_in_flatten() {
#[derive(Debug, StructOpt)]
pub enum Struct1 {
#[structopt(flatten)]
Struct1(Struct2),
}

#[derive(Debug, StructOpt)]
pub struct Struct2 {
#[structopt(subcommand)]
command_type: Enum3,
}

#[derive(Debug, StructOpt)]
pub enum Enum3 {
Command { args: Vec<String> },
}

Struct1::from_iter(&["test", "command", "foo"]);
}

0 comments on commit 776fe37

Please sign in to comment.