1 // Copyright 2018 Guillaume Pinot (@TeXitoi) <texitoi@texitoi.eu>
3 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6 // option. This file may not be copied, modified, or distributed
7 // except according to those terms.
9 use structopt
::StructOpt
;
11 #[derive(StructOpt, PartialEq, Debug)]
13 #[structopt(short, long)]
15 #[structopt(short, long, parse(from_occurrences))]
17 #[structopt(subcommand)]
21 #[derive(StructOpt, PartialEq, Debug)]
27 #[derive(StructOpt, PartialEq, Debug)]
29 #[structopt(short, long)]
31 #[structopt(short, long, parse(from_occurrences))]
33 #[structopt(subcommand)]
39 let result
= Opt
::clap().get_matches_from_safe(&["test"]);
40 assert
!(result
.is_err());
48 Opt2
::from_clap(&Opt2
::clap().get_matches_from(&["test"]))
60 Opt
::from_clap(&Opt
::clap().get_matches_from(&["test", "-vvv", "fetch"]))
68 Opt
::from_clap(&Opt
::clap().get_matches_from(&["test", "--force", "fetch"]))
80 Opt
::from_clap(&Opt
::clap().get_matches_from(&["test", "add"]))
88 Opt
::from_clap(&Opt
::clap().get_matches_from(&["test", "-vv", "add"]))
94 let result
= Opt
::clap().get_matches_from_safe(&["test", "badcmd"]);
95 assert
!(result
.is_err());
96 let result
= Opt
::clap().get_matches_from_safe(&["test", "add", "--verbose"]);
97 assert
!(result
.is_err());
98 let result
= Opt
::clap().get_matches_from_safe(&["test", "--badopt", "add"]);
99 assert
!(result
.is_err());
100 let result
= Opt
::clap().get_matches_from_safe(&["test", "add", "--badopt"]);
101 assert
!(result
.is_err());
104 #[derive(StructOpt, PartialEq, Debug)]
106 #[structopt(short, long)]
108 #[structopt(subcommand)]
112 #[derive(StructOpt, PartialEq, Debug)]
116 #[structopt(subcommand)]
122 #[derive(StructOpt, PartialEq, Debug)]
129 fn test_subsubcommand() {
134 file
: "lib.rs".to_string(),
139 &Opt3
::clap().get_matches_from(&["test", "--all", "foo", "lib.rs", "quux"])
144 #[derive(StructOpt, PartialEq, Debug)]
145 enum SubSubCmdWithOption
{
147 #[structopt(subcommand)]
151 #[structopt(subcommand)]
155 #[derive(StructOpt, PartialEq, Debug)]
157 Add { name: String, url: String }
,
158 Remove { name: String }
,
161 #[derive(StructOpt, PartialEq, Debug)]
168 fn sub_sub_cmd_with_option() {
169 fn make(args
: &[&str]) -> Option
<SubSubCmdWithOption
> {
170 SubSubCmdWithOption
::clap()
171 .get_matches_from_safe(args
)
173 .map(|m
| SubSubCmdWithOption
::from_clap(&m
))
176 Some(SubSubCmdWithOption
::Remote { cmd: None }
),
177 make(&["", "remote"])
180 Some(SubSubCmdWithOption
::Remote
{
181 cmd
: Some(Remote
::Add
{
182 name
: "origin".into(),
186 make(&["", "remote", "add", "origin", "http"])
189 Some(SubSubCmdWithOption
::Stash { cmd: Stash::Save }
),
190 make(&["", "stash", "save"])
192 assert_eq
!(None
, make(&["", "stash"]));