forked from clap-rs/clap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
positionals.rs
203 lines (188 loc) · 8.01 KB
/
positionals.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
extern crate clap;
use clap::{App, AppSettings, Arg, ErrorKind};
#[test]
fn only_pos_follow() {
let r = App::new("onlypos")
.args(&[Arg::from_usage("-f [flag] 'some opt'"),
Arg::from_usage("[arg] 'some arg'")])
.get_matches_from_safe(vec!["", "--", "-f"]);
assert!(r.is_ok());
let m = r.unwrap();
assert!(m.is_present("arg"));
assert!(!m.is_present("f"));
assert_eq!(m.value_of("arg").unwrap(), "-f");
}
#[test]
fn only_pos_follow_multiple() {
let r = App::new("onlypos")
.args(&[Arg::from_usage("-f [flag] 'some opt'"),
Arg::from_usage("[arg]... 'some arg'")])
.get_matches_from_safe(vec!["", "--", "-f", "-g,x"]);
assert!(r.is_ok());
let m = r.unwrap();
assert!(m.is_present("arg"));
assert!(!m.is_present("f"));
assert_eq!(m.values_of("arg").unwrap().collect::<Vec<_>>(), &["-f", "-g,x"]);
}
#[test]
fn positional() {
let m = App::new("positional")
.args(&[
Arg::from_usage("-f, --flag 'some flag'"),
Arg::with_name("positional")
.index(1)
])
.get_matches_from(vec!["", "-f", "test"]);
assert!(m.is_present("positional"));
assert!(m.is_present("flag"));
assert_eq!(m.value_of("positional").unwrap(), "test");
let m = App::new("positional")
.args(&[
Arg::from_usage("-f, --flag 'some flag'"),
Arg::with_name("positional")
.index(1)
])
.get_matches_from(vec!["", "test", "--flag"]);
assert!(m.is_present("positional"));
assert!(m.is_present("flag"));
assert_eq!(m.value_of("positional").unwrap(), "test");
}
#[test]
fn lots_o_vals() {
let r = App::new("opts")
.arg(
Arg::from_usage("[opt]... 'some pos'"),
)
.get_matches_from_safe(vec!["",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
]);
assert!(r.is_ok());
let m = r.unwrap();
assert!(m.is_present("opt"));
assert_eq!(m.values_of("opt").unwrap().collect::<Vec<_>>().len(), 297); // i.e. more than u8
}
#[test]
fn positional_multiple() {
let m = App::new("positional_multiple")
.args(&[
Arg::from_usage("-f, --flag 'some flag'"),
Arg::with_name("positional")
.index(1)
.multiple(true)
])
.get_matches_from(vec!["", "-f", "test1", "test2", "test3"]);
assert!(m.is_present("positional"));
assert!(m.is_present("flag"));
assert_eq!(&*m.values_of("positional").unwrap().collect::<Vec<_>>(), &["test1", "test2", "test3"]);
let m = App::new("positional_multiple")
.args(&[
Arg::from_usage("-f, --flag 'some flag'"),
Arg::with_name("positional")
.index(1)
.multiple(true)
])
.get_matches_from(vec!["", "test1", "test2", "test3", "--flag"]);
assert!(m.is_present("positional"));
assert!(m.is_present("flag"));
assert_eq!(&*m.values_of("positional").unwrap().collect::<Vec<_>>(), &["test1", "test2", "test3"]);
}
#[test]
fn positional_multiple_2() {
let result = App::new("positional_multiple")
.args(&[
Arg::from_usage("-f, --flag 'some flag'"),
Arg::with_name("positional")
.index(1)
])
.get_matches_from_safe(vec!["", "-f", "test1", "test2", "test3"]);
assert!(result.is_err());
let err = result.err().unwrap();
assert_eq!(err.kind, ErrorKind::UnknownArgument);
}
#[test]
fn positional_possible_values() {
let m = App::new("positional_possible_values")
.args(&[
Arg::from_usage("-f, --flag 'some flag'"),
Arg::with_name("positional")
.index(1)
.possible_value("test123")
])
.get_matches_from(vec!["", "-f", "test123"]);
assert!(m.is_present("positional"));
assert!(m.is_present("flag"));
assert_eq!(&*m.values_of("positional").unwrap().collect::<Vec<_>>(), &["test123"]);
}
#[test]
fn create_positional() {
let _ = App::new("test")
.arg(Arg::with_name("test")
.index(1)
.help("testing testing"))
.get_matches();
}
#[test]
fn positional_hyphen_does_not_panic() {
let _ = App::new("test")
.arg(Arg::with_name("dummy"))
.get_matches_from(vec!["test", "-"]);
}
#[test]
fn default_values_default() {
let r = App::new("df")
.arg( Arg::from_usage("[arg] 'some opt'")
.default_value("default"))
.get_matches_from_safe(vec![""]);
assert!(r.is_ok());
let m = r.unwrap();
assert!(m.is_present("arg"));
assert_eq!(m.value_of("arg").unwrap(), "default");
}
#[test]
fn default_values_user_value() {
let r = App::new("df")
.arg( Arg::from_usage("[arg] 'some arg'")
.default_value("default"))
.get_matches_from_safe(vec!["", "value"]);
assert!(r.is_ok());
let m = r.unwrap();
assert!(m.is_present("arg"));
assert_eq!(m.value_of("arg").unwrap(), "value");
}
#[test]
fn positional_trailingvararg() {
let m = App::new("positional")
.setting(AppSettings::TrailingVarArg)
.arg(
Arg::from_usage("[opt]... 'some pos'"),
)
.get_matches_from(vec!["", "test", "--foo", "-Wl,-bar"]);
assert!(m.is_present("opt"));
assert_eq!(m.values_of("opt").unwrap().collect::<Vec<_>>(), &["test", "--foo", "-Wl,-bar"]);
}