-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathanywhere.rs
More file actions
185 lines (144 loc) · 5.67 KB
/
Copy pathanywhere.rs
File metadata and controls
185 lines (144 loc) · 5.67 KB
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
use bpaf::*;
#[test]
fn parse_anywhere_positional() {
let a = any("X", |h| if h != "--help" { Some(h) } else { None })
.help("all the things")
.anywhere();
let b = short('b').help("batch mode").switch();
let parser: OptionParser<(String, bool)> = construct!(a, b).to_options();
let r = parser.run_inner(&["--help"]).unwrap_err().unwrap_stdout();
let expected = "\
Usage: X [-b]
Available options:
X all the things
-b batch mode
-h, --help Prints help information
";
assert_eq!(r, expected);
// this should be allowed because "anywhere" prevents anything inside from being positional
parser.check_invariants(true);
}
#[test]
fn parse_anywhere_no_catch() {
let a = short('a').req_flag(());
let b = positional::<usize>("X");
let ab = construct!(a, b).adjacent();
let c = short('c').switch();
let parser = construct!(ab, c).to_options();
// Usage: -a <x> [-c],
let r = parser.run_inner(&["3", "-a"]).unwrap_err().unwrap_stderr();
assert_eq!(r, "expected `X`, pass `--help` for usage information");
let r = parser.run_inner(&["-a"]).unwrap_err().unwrap_stderr();
assert_eq!(r, "expected `X`, pass `--help` for usage information");
let r = parser
.run_inner(&["-a", "221b"])
.unwrap_err()
.unwrap_stderr();
assert_eq!(r, "couldn't parse `221b`: invalid digit found in string");
let r = parser.run_inner(&["-c", "-a"]).unwrap_err().unwrap_stderr();
assert_eq!(r, "expected `X`, pass `--help` for usage information");
let r = parser
.run_inner(&["-c", "-a", "221b"])
.unwrap_err()
.unwrap_stderr();
assert_eq!(r, "couldn't parse `221b`: invalid digit found in string");
let r = parser.run_inner(&["-a", "-c"]).unwrap_err().unwrap_stderr();
assert_eq!(r, "expected `X`, pass `--help` for usage information");
let r = parser
.run_inner(&["-a", "221b", "-c"])
.unwrap_err()
.unwrap_stderr();
assert_eq!(r, "couldn't parse `221b`: invalid digit found in string");
}
#[test]
fn anywhere_catch_optional() {
let a = short('a').req_flag(());
let b = positional::<usize>("x");
let ab = construct!(a, b).adjacent().optional().catch();
let bc = short('a').switch();
let parser = construct!(ab, bc).to_options();
let r = parser.run_inner(&["-a", "10"]).unwrap();
assert_eq!(r, (Some(((), 10)), false));
let r = parser.run_inner(&["-a"]).unwrap();
assert_eq!(r, (None, true));
let r = parser.run_inner(&[]).unwrap();
assert_eq!(r, (None, false));
}
#[test]
fn anywhere_catch_many() {
let a = short('a').req_flag(());
let b = positional::<usize>("x");
let ab = construct!(a, b).adjacent().many().catch();
let bc = short('a').switch();
let parser = construct!(ab, bc).to_options();
let r = parser.run_inner(&["-a"]).unwrap();
assert_eq!(r, (vec![], true));
let r = parser.run_inner(&["-a", "10"]).unwrap();
assert_eq!(r, (vec![((), 10)], false));
let r = parser.run_inner(&[]).unwrap();
assert_eq!(r, (Vec::new(), false));
}
#[test]
fn anywhere_catch_fallback() {
let a = short('a').req_flag(());
let b = positional::<usize>("x");
let ab = construct!(a, b).adjacent().fallback(((), 10));
let bc = short('a').switch();
let parser = construct!(ab, bc).to_options();
let r = parser.run_inner(&["-a", "12"]).unwrap();
assert_eq!(r, (((), 12), false));
let r = parser.run_inner(&["-a"]).unwrap();
assert_eq!(r, (((), 10), true));
let r = parser.run_inner(&[]).unwrap();
assert_eq!(r, (((), 10), false));
}
#[test]
fn parse_anywhere_catch_optional() {
let a = short('a').req_flag(());
let b = positional::<usize>("x");
// optional + catch makes it so parser succeeds without consuming anything
// usually leaving `-a` untouched to be consumed by something else
let ab = construct!(a, b).adjacent().optional().catch();
let c = short('c').switch();
let parser = construct!(ab, c).to_options();
let r = parser
.run_inner(&["-a", "221b"])
.unwrap_err()
.unwrap_stderr();
assert_eq!(r, "`-a` is not expected in this context");
let r = parser.run_inner(&["3", "-a"]).unwrap_err().unwrap_stderr();
assert_eq!(r, "`3` is not expected in this context");
let r = parser.run_inner(&["-a"]).unwrap_err().unwrap_stderr();
assert_eq!(r, "`-a` is not expected in this context");
let r = parser.run_inner(&["-c", "-a"]).unwrap_err().unwrap_stderr();
assert_eq!(r, "`-a` is not expected in this context");
let r = parser
.run_inner(&["-c", "-a", "221b"])
.unwrap_err()
.unwrap_stderr();
assert_eq!(r, "`-a` is not expected in this context");
let r = parser.run_inner(&["-a", "-c"]).unwrap_err().unwrap_stderr();
assert_eq!(r, "`-a` is not expected in this context");
let r = parser
.run_inner(&["-a", "221b", "-c"])
.unwrap_err()
.unwrap_stderr();
assert_eq!(r, "`-a` is not expected in this context");
}
#[test]
fn anywhere_literal() {
let tag = any(
"-mode",
|x: String| if x == "-mode" { Some(()) } else { None },
);
let mode = positional::<usize>("value");
let a = construct!(tag, mode).adjacent().many().catch();
let b = short('b').switch();
let parser: OptionParser<(Vec<((), usize)>, bool)> = construct!(a, b).to_options();
let r = parser.run_inner(&["-b", "-mode", "12"]).unwrap();
assert_eq!(r, (vec![((), 12)], true));
let r = parser.run_inner(&["-mode", "12", "-b"]).unwrap();
assert_eq!(r, (vec![((), 12)], true));
let r = parser.run_inner(&["-mode", "12"]).unwrap();
assert_eq!(r, (vec![((), 12)], false));
}