-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathpositionals.rs
More file actions
160 lines (125 loc) · 4.39 KB
/
Copy pathpositionals.rs
File metadata and controls
160 lines (125 loc) · 4.39 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
use bpaf::*;
#[test]
fn positional_with_help() {
let user = positional::<String>("USER").help("github user\nin two lines");
let api = positional::<String>("API_KEY").help("api key to use");
let parser = construct!(user, api).to_options();
let help = parser.run_inner(&["--help"]).unwrap_err().unwrap_stdout();
let expected_help = "\
Usage: USER API_KEY
Available positional items:
USER github user in two lines
API_KEY api key to use
Available options:
-h, --help Prints help information
";
assert_eq!(expected_help, help);
}
#[test]
fn help_for_positional() {
let c = positional::<String>("C").help("help for\nc");
let d = positional::<String>("DDD").help("help for\nddd");
let parser = construct!(c, d).to_options();
let help = parser.run_inner(&["--help"]).unwrap_err().unwrap_stdout();
let expected_help = "\
Usage: C DDD
Available positional items:
C help for c
DDD help for ddd
Available options:
-h, --help Prints help information
";
assert_eq!(expected_help, help);
}
#[test]
fn dash_is_positional() {
let a = positional::<String>("FILE");
let parser = a.to_options();
assert_eq!("-", parser.run_inner(&["-"]).unwrap());
}
#[test]
fn helpful_error_message() {
let parser = positional::<String>("FOO")
.some("you need to specify at least one FOO")
.to_options();
let err = parser.run_inner(&[]).unwrap_err().unwrap_stderr();
assert_eq!("you need to specify at least one FOO", err);
}
#[test]
fn positional_argument() {
let p = positional::<String>("FILE")
.help("file name")
.group_help("File to process");
let parser = p.to_options();
let help = parser.run_inner(&["--help"]).unwrap_err().unwrap_stdout();
let expected = "\
Usage: FILE
File to process
FILE file name
Available options:
-h, --help Prints help information
";
assert_eq!(expected, help);
}
#[test]
#[should_panic(expected = "bpaf usage BUG: all positional")]
fn positional_help_complain_1() {
let a = positional::<String>("a");
let b = short('b').switch();
let parser = construct!(a, b).to_options();
parser.run_inner(&["--help"]).unwrap_err().unwrap_stderr();
}
#[test]
#[should_panic(expected = "bpaf usage BUG: all positional")]
fn positional_help_complain_2() {
let a = positional::<String>("a");
let b = short('b').switch();
let ba = construct!(b, a);
let c = short('c').switch();
let parser = construct!(ba, c).to_options();
parser.run_inner(&["--help"]).unwrap_err().unwrap_stderr();
}
#[test]
#[should_panic(expected = "bpaf usage BUG: all positional")]
fn positional_help_complain_3() {
let a = positional::<String>("a");
let b = short('b').argument::<String>("B");
let ba = construct!([b, a]);
let c = short('c').switch();
let parser = construct!(ba, c).to_options();
parser.run_inner(&["--help"]).unwrap_err().unwrap_stderr();
}
#[test]
fn positional_help_complain_4() {
let a = positional::<String>("a");
let b = short('b').argument::<String>("B");
let parser = construct!([b, a]).to_options();
parser.run_inner(&["--help"]).unwrap_err().unwrap_stdout();
}
#[test]
fn strictly_positional() {
let parser = positional::<String>("A").strict().to_options();
let r = parser.run_inner(&["--help"]).unwrap_err().unwrap_stdout();
assert_eq!(
r,
"Usage: -- A\n\nAvailable options:\n -h, --help Prints help information\n"
);
let r = parser.run_inner(&["a"]).unwrap_err().unwrap_stderr();
assert_eq!(r, "expected `A` to be on the right side of `--`");
let r = parser.run_inner(&["--", "a"]).unwrap();
assert_eq!(r, "a");
let r = parser.run_inner(&["a", "--"]).unwrap_err().unwrap_stderr();
assert_eq!(r, "expected `A` to be on the right side of `--`");
let r = parser.run_inner(&["--"]).unwrap_err().unwrap_stderr();
assert_eq!(r, "expected `A`, pass `--help` for usage information");
}
#[test]
fn non_strictly_positional() {
let parser = positional::<String>("A").non_strict().to_options();
let r = parser.run_inner(&["a"]).unwrap();
assert_eq!(r, "a");
let r = parser.run_inner(&["--", "a"]).unwrap_err().unwrap_stderr();
assert_eq!(r, "expected `A` to be on the left side of `--`");
let r = parser.run_inner(&["--"]).unwrap_err().unwrap_stderr();
assert_eq!(r, "expected `A`, pass `--help` for usage information");
}