Matthew Maurer | 32e7669 | 2020-06-02 11:15:15 -0700 | [diff] [blame^] | 1 | // https://github.com/TeXitoi/structopt/issues/{NUMBER} |
| 2 | |
| 3 | mod utils; |
| 4 | use utils::*; |
| 5 | |
| 6 | use structopt::StructOpt; |
| 7 | |
| 8 | #[test] |
| 9 | fn issue_151() { |
| 10 | use structopt::{clap::ArgGroup, StructOpt}; |
| 11 | |
| 12 | #[derive(StructOpt, Debug)] |
| 13 | #[structopt(group = ArgGroup::with_name("verb").required(true).multiple(true))] |
| 14 | struct Opt { |
| 15 | #[structopt(long, group = "verb")] |
| 16 | foo: bool, |
| 17 | #[structopt(long, group = "verb")] |
| 18 | bar: bool, |
| 19 | } |
| 20 | |
| 21 | #[derive(Debug, StructOpt)] |
| 22 | struct Cli { |
| 23 | #[structopt(flatten)] |
| 24 | a: Opt, |
| 25 | } |
| 26 | |
| 27 | assert!(Cli::clap().get_matches_from_safe(&["test"]).is_err()); |
| 28 | assert!(Cli::clap() |
| 29 | .get_matches_from_safe(&["test", "--foo"]) |
| 30 | .is_ok()); |
| 31 | assert!(Cli::clap() |
| 32 | .get_matches_from_safe(&["test", "--bar"]) |
| 33 | .is_ok()); |
| 34 | assert!(Cli::clap() |
| 35 | .get_matches_from_safe(&["test", "--zebra"]) |
| 36 | .is_err()); |
| 37 | assert!(Cli::clap() |
| 38 | .get_matches_from_safe(&["test", "--foo", "--bar"]) |
| 39 | .is_ok()); |
| 40 | } |
| 41 | |
| 42 | #[test] |
| 43 | fn issue_289() { |
| 44 | use structopt::{clap::AppSettings, StructOpt}; |
| 45 | |
| 46 | #[derive(StructOpt)] |
| 47 | #[structopt(setting = AppSettings::InferSubcommands)] |
| 48 | enum Args { |
| 49 | SomeCommand(SubSubCommand), |
| 50 | AnotherCommand, |
| 51 | } |
| 52 | |
| 53 | #[derive(StructOpt)] |
| 54 | #[structopt(setting = AppSettings::InferSubcommands)] |
| 55 | enum SubSubCommand { |
| 56 | TestCommand, |
| 57 | } |
| 58 | |
| 59 | assert!(Args::clap() |
| 60 | .get_matches_from_safe(&["test", "some-command", "test-command"]) |
| 61 | .is_ok()); |
| 62 | assert!(Args::clap() |
| 63 | .get_matches_from_safe(&["test", "some", "test-command"]) |
| 64 | .is_ok()); |
| 65 | assert!(Args::clap() |
| 66 | .get_matches_from_safe(&["test", "some-command", "test"]) |
| 67 | .is_ok()); |
| 68 | assert!(Args::clap() |
| 69 | .get_matches_from_safe(&["test", "some", "test"]) |
| 70 | .is_ok()); |
| 71 | } |
| 72 | |
| 73 | #[test] |
| 74 | fn issue_324() { |
| 75 | fn my_version() -> &'static str { |
| 76 | "MY_VERSION" |
| 77 | } |
| 78 | |
| 79 | #[derive(StructOpt)] |
| 80 | #[structopt(version = my_version())] |
| 81 | struct Opt { |
| 82 | #[structopt(subcommand)] |
| 83 | _cmd: Option<SubCommand>, |
| 84 | } |
| 85 | |
| 86 | #[derive(StructOpt)] |
| 87 | enum SubCommand { |
| 88 | Start, |
| 89 | } |
| 90 | |
| 91 | let help = get_long_help::<Opt>(); |
| 92 | assert!(help.contains("MY_VERSION")); |
| 93 | } |
| 94 | |
| 95 | #[test] |
| 96 | fn issue_359() { |
| 97 | #[derive(Debug, PartialEq, StructOpt)] |
| 98 | struct Opt { |
| 99 | #[structopt(subcommand)] |
| 100 | sub: Subcommands, |
| 101 | } |
| 102 | |
| 103 | #[derive(Debug, PartialEq, StructOpt)] |
| 104 | enum Subcommands { |
| 105 | Add, |
| 106 | |
| 107 | #[structopt(external_subcommand)] |
| 108 | Other(Vec<String>), |
| 109 | } |
| 110 | |
| 111 | assert_eq!( |
| 112 | Opt { |
| 113 | sub: Subcommands::Other(vec!["only_one_arg".into()]) |
| 114 | }, |
| 115 | Opt::from_iter(&["test", "only_one_arg"]) |
| 116 | ); |
| 117 | } |