Skip to content

Commit

Permalink
Handle inter-expansion top-level args properly
Browse files Browse the repository at this point in the history
  • Loading branch information
CreepySkeleton committed Dec 2, 2019
1 parent d1a50bf commit 3413004
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# [Upcoming]

* Top level `#structopt[...]` raw methods on inner atructs/enums now work as expected
([#151](https://github.com/TeXitoi/structopt/issues/151)) ([#289](https://github.com/TeXitoi/structopt/issues/289)).

# v0.3.5 (2019-11-22)

* `try_from_str` functions are now called with a `&str` instead of a `&String` ([#282](https://github.com/TeXitoi/structopt/pull/282))
Expand Down
10 changes: 6 additions & 4 deletions structopt-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,9 @@ fn gen_augmentation(
}
});

let app_methods = parent_attribute.top_level_methods();
quote! {{
let #app_var = #app_var#app_methods;
#( #args )*
#subcmd
#app_var
Expand Down Expand Up @@ -361,9 +363,7 @@ fn gen_clap(attrs: &[Attribute]) -> GenOutput {
);
let tokens = {
let name = attrs.cased_name();
let methods = attrs.top_level_methods();

quote!(::structopt::clap::App::new(#name)#methods)
quote!(::structopt::clap::App::new(#name))
};

GenOutput { tokens, attrs }
Expand Down Expand Up @@ -463,11 +463,13 @@ fn gen_augment_clap_enum(
}
});

let app_methods = parent_attribute.top_level_methods();

quote! {
pub fn augment_clap<'a, 'b>(
app: ::structopt::clap::App<'a, 'b>
) -> ::structopt::clap::App<'a, 'b> {
app #( #subcommands )*
app #app_methods #( #subcommands )*
}
}
}
Expand Down
49 changes: 49 additions & 0 deletions tests/issues.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// https://github.com/TeXitoi/structopt/issues/151
// https://github.com/TeXitoi/structopt/issues/289

#[test]
fn issue_151() {
use structopt::{clap::ArgGroup, StructOpt};

#[derive(StructOpt, Debug)]
#[structopt(group = ArgGroup::with_name("verb").required(true).multiple(true))]
struct Opt {
#[structopt(long, group = "verb")] foo: bool,
#[structopt(long, group = "verb")] bar: bool,
}

#[derive(Debug, StructOpt)]
struct Cli {
#[structopt(flatten)]
a: Opt
}

assert!(Cli::clap().get_matches_from_safe(&["test"]).is_err());
assert!(Cli::clap().get_matches_from_safe(&["test", "--foo"]).is_ok());
assert!(Cli::clap().get_matches_from_safe(&["test", "--bar"]).is_ok());
assert!(Cli::clap().get_matches_from_safe(&["test", "--zebra"]).is_err());
}

#[test]
fn issue_289() {

use structopt::{StructOpt, clap::AppSettings};

#[derive(StructOpt)]
#[structopt(setting = AppSettings::InferSubcommands)]
enum Args {
SomeCommand(SubSubCommand),
AnotherCommand,
}

#[derive(StructOpt)]
#[structopt(setting = AppSettings::InferSubcommands)]
enum SubSubCommand {
TestCommand,
}

assert!(Args::clap().get_matches_from_safe(&["test", "some-command", "test-command"]).is_ok());
assert!(Args::clap().get_matches_from_safe(&["test", "some", "test-command"]).is_ok());
assert!(Args::clap().get_matches_from_safe(&["test", "some-command", "test"]).is_ok());
assert!(Args::clap().get_matches_from_safe(&["test", "some", "test"]).is_ok());
}

0 comments on commit 3413004

Please sign in to comment.