Skip to content

Commit d117f7a

Browse files
authored
Merge pull request #6028 from epage/arg
feat(builder): Allow quoted id's for arg macro
2 parents 1036060 + cb8255d commit d117f7a

File tree

2 files changed

+374
-0
lines changed

2 files changed

+374
-0
lines changed

clap_builder/src/macros.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,13 +530,28 @@ macro_rules! arg_impl {
530530
/// [`Arg`]: crate::Arg
531531
#[macro_export]
532532
macro_rules! arg {
533+
( -$($tail:tt)+ ) => {{
534+
let arg = $crate::Arg::default();
535+
let arg = $crate::arg_impl! {
536+
@arg (arg) -$($tail)+
537+
};
538+
debug_assert_ne!(arg.get_id(), "", "Without a value or long flag, the `name:` prefix is required");
539+
arg
540+
}};
533541
( $name:ident: $($tail:tt)+ ) => {{
534542
let arg = $crate::Arg::new($crate::arg_impl! { @string $name });
535543
let arg = $crate::arg_impl! {
536544
@arg (arg) $($tail)+
537545
};
538546
arg
539547
}};
548+
( $name:literal: $($tail:tt)+ ) => {{
549+
let arg = $crate::Arg::new($crate::arg_impl! { @string $name });
550+
let arg = $crate::arg_impl! {
551+
@arg (arg) $($tail)+
552+
};
553+
arg
554+
}};
540555
( $($tail:tt)+ ) => {{
541556
let arg = $crate::Arg::default();
542557
let arg = $crate::arg_impl! {

tests/builder/macros.rs

Lines changed: 359 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,359 @@
1+
use clap::arg;
2+
use snapbox::assert_data_eq;
3+
use snapbox::prelude::*;
4+
use snapbox::str;
5+
6+
#[test]
7+
fn arg_long() {
8+
let arg = arg!(--long);
9+
assert_data_eq!(arg.get_id().to_debug(), str![[r#"
10+
"long"
11+
12+
"#]]);
13+
assert_data_eq!(arg.get_short().to_debug(), str![[r#"
14+
None
15+
16+
"#]]);
17+
assert_data_eq!(arg.get_long().to_debug(), str![[r#"
18+
Some(
19+
"long",
20+
)
21+
22+
"#]]);
23+
assert_data_eq!(arg.get_action().to_debug(), str![[r#"
24+
SetTrue
25+
26+
"#]]);
27+
assert_data_eq!(arg.is_required_set().to_debug(), str![[r#"
28+
false
29+
30+
"#]]);
31+
assert_data_eq!(arg.get_value_names().to_debug(), str![[r#"
32+
None
33+
34+
"#]]);
35+
assert_data_eq!(arg.get_help().to_debug(), str![[r#"
36+
None
37+
38+
"#]]);
39+
}
40+
41+
#[test]
42+
#[should_panic = "Without a value or long flag, the `name:` prefix is required"]
43+
fn arg_short() {
44+
arg!(-s);
45+
}
46+
47+
#[test]
48+
fn arg_long_dashed() {
49+
let arg = arg!(--"long-flag");
50+
assert_data_eq!(arg.get_id().to_debug(), str![[r#"
51+
"long-flag"
52+
53+
"#]]);
54+
assert_data_eq!(arg.get_short().to_debug(), str![[r#"
55+
None
56+
57+
"#]]);
58+
assert_data_eq!(arg.get_long().to_debug(), str![[r#"
59+
Some(
60+
"long-flag",
61+
)
62+
63+
"#]]);
64+
assert_data_eq!(arg.get_action().to_debug(), str![[r#"
65+
SetTrue
66+
67+
"#]]);
68+
assert_data_eq!(arg.is_required_set().to_debug(), str![[r#"
69+
false
70+
71+
"#]]);
72+
assert_data_eq!(arg.get_value_names().to_debug(), str![[r#"
73+
None
74+
75+
"#]]);
76+
assert_data_eq!(arg.get_help().to_debug(), str![[r#"
77+
None
78+
79+
"#]]);
80+
}
81+
82+
#[test]
83+
fn arg_long_optional_value() {
84+
let arg = arg!(--long[VALUE]);
85+
assert_data_eq!(arg.get_id().to_debug(), str![[r#"
86+
"long"
87+
88+
"#]]);
89+
assert_data_eq!(arg.get_short().to_debug(), str![[r#"
90+
None
91+
92+
"#]]);
93+
assert_data_eq!(arg.get_long().to_debug(), str![[r#"
94+
Some(
95+
"long",
96+
)
97+
98+
"#]]);
99+
assert_data_eq!(arg.get_action().to_debug(), str![[r#"
100+
Set
101+
102+
"#]]);
103+
assert_data_eq!(arg.is_required_set().to_debug(), str![[r#"
104+
false
105+
106+
"#]]);
107+
assert_data_eq!(arg.get_value_names().to_debug(), str![[r#"
108+
Some(
109+
[
110+
"VALUE",
111+
],
112+
)
113+
114+
"#]]);
115+
assert_data_eq!(arg.get_help().to_debug(), str![[r#"
116+
None
117+
118+
"#]]);
119+
}
120+
121+
#[test]
122+
fn arg_long_required_value() {
123+
let arg = arg!(--long <VALUE>);
124+
assert_data_eq!(arg.get_id().to_debug(), str![[r#"
125+
"long"
126+
127+
"#]]);
128+
assert_data_eq!(arg.get_short().to_debug(), str![[r#"
129+
None
130+
131+
"#]]);
132+
assert_data_eq!(arg.get_long().to_debug(), str![[r#"
133+
Some(
134+
"long",
135+
)
136+
137+
"#]]);
138+
assert_data_eq!(arg.get_action().to_debug(), str![[r#"
139+
Set
140+
141+
"#]]);
142+
assert_data_eq!(arg.is_required_set().to_debug(), str![[r#"
143+
false
144+
145+
"#]]);
146+
assert_data_eq!(arg.get_value_names().to_debug(), str![[r#"
147+
Some(
148+
[
149+
"VALUE",
150+
],
151+
)
152+
153+
"#]]);
154+
assert_data_eq!(arg.get_help().to_debug(), str![[r#"
155+
None
156+
157+
"#]]);
158+
}
159+
160+
#[test]
161+
#[should_panic = "Multiple values not yet supported"]
162+
fn arg_long_multiple_values() {
163+
arg!(--long <VALUE1> <VALUE2> <VALUE3> [VALUE4] [VALUE5]);
164+
}
165+
166+
#[test]
167+
fn arg_optional_value() {
168+
let arg = arg!([VALUE]);
169+
assert_data_eq!(arg.get_id().to_debug(), str![[r#"
170+
"VALUE"
171+
172+
"#]]);
173+
assert_data_eq!(arg.get_short().to_debug(), str![[r#"
174+
None
175+
176+
"#]]);
177+
assert_data_eq!(arg.get_long().to_debug(), str![[r#"
178+
None
179+
180+
"#]]);
181+
assert_data_eq!(arg.get_action().to_debug(), str![[r#"
182+
Set
183+
184+
"#]]);
185+
assert_data_eq!(arg.is_required_set().to_debug(), str![[r#"
186+
false
187+
188+
"#]]);
189+
assert_data_eq!(arg.get_value_names().to_debug(), str![[r#"
190+
Some(
191+
[
192+
"VALUE",
193+
],
194+
)
195+
196+
"#]]);
197+
assert_data_eq!(arg.get_help().to_debug(), str![[r#"
198+
None
199+
200+
"#]]);
201+
}
202+
203+
#[test]
204+
fn arg_required_value() {
205+
let arg = arg!(<VALUE>);
206+
assert_data_eq!(arg.get_id().to_debug(), str![[r#"
207+
"VALUE"
208+
209+
"#]]);
210+
assert_data_eq!(arg.get_short().to_debug(), str![[r#"
211+
None
212+
213+
"#]]);
214+
assert_data_eq!(arg.get_long().to_debug(), str![[r#"
215+
None
216+
217+
"#]]);
218+
assert_data_eq!(arg.get_action().to_debug(), str![[r#"
219+
Set
220+
221+
"#]]);
222+
assert_data_eq!(arg.is_required_set().to_debug(), str![[r#"
223+
true
224+
225+
"#]]);
226+
assert_data_eq!(arg.get_value_names().to_debug(), str![[r#"
227+
Some(
228+
[
229+
"VALUE",
230+
],
231+
)
232+
233+
"#]]);
234+
assert_data_eq!(arg.get_help().to_debug(), str![[r#"
235+
None
236+
237+
"#]]);
238+
}
239+
240+
#[test]
241+
#[should_panic = "Multiple values not yet supported"]
242+
fn arg_multiple_values() {
243+
arg!(<VALUE1> <VALUE2> <VALUE3> [VALUE4] [VALUE5]);
244+
}
245+
246+
#[test]
247+
fn arg_named_positional() {
248+
let arg = arg!(name: <VALUE>);
249+
assert_data_eq!(arg.get_id().to_debug(), str![[r#"
250+
"name"
251+
252+
"#]]);
253+
assert_data_eq!(arg.get_short().to_debug(), str![[r#"
254+
None
255+
256+
"#]]);
257+
assert_data_eq!(arg.get_long().to_debug(), str![[r#"
258+
None
259+
260+
"#]]);
261+
assert_data_eq!(arg.get_action().to_debug(), str![[r#"
262+
Set
263+
264+
"#]]);
265+
assert_data_eq!(arg.is_required_set().to_debug(), str![[r#"
266+
true
267+
268+
"#]]);
269+
assert_data_eq!(arg.get_value_names().to_debug(), str![[r#"
270+
Some(
271+
[
272+
"VALUE",
273+
],
274+
)
275+
276+
"#]]);
277+
assert_data_eq!(arg.get_help().to_debug(), str![[r#"
278+
None
279+
280+
"#]]);
281+
}
282+
283+
#[test]
284+
fn arg_named_long() {
285+
let arg = arg!(name: --long <VALUE>);
286+
assert_data_eq!(arg.get_id().to_debug(), str![[r#"
287+
"name"
288+
289+
"#]]);
290+
assert_data_eq!(arg.get_short().to_debug(), str![[r#"
291+
None
292+
293+
"#]]);
294+
assert_data_eq!(arg.get_long().to_debug(), str![[r#"
295+
Some(
296+
"long",
297+
)
298+
299+
"#]]);
300+
assert_data_eq!(arg.get_action().to_debug(), str![[r#"
301+
Set
302+
303+
"#]]);
304+
assert_data_eq!(arg.is_required_set().to_debug(), str![[r#"
305+
false
306+
307+
"#]]);
308+
assert_data_eq!(arg.get_value_names().to_debug(), str![[r#"
309+
Some(
310+
[
311+
"VALUE",
312+
],
313+
)
314+
315+
"#]]);
316+
assert_data_eq!(arg.get_help().to_debug(), str![[r#"
317+
None
318+
319+
"#]]);
320+
}
321+
322+
#[test]
323+
fn arg_named_dashed() {
324+
let arg = arg!("named-arg": --long <VALUE>);
325+
assert_data_eq!(arg.get_id().to_debug(), str![[r#"
326+
"named-arg"
327+
328+
"#]]);
329+
assert_data_eq!(arg.get_short().to_debug(), str![[r#"
330+
None
331+
332+
"#]]);
333+
assert_data_eq!(arg.get_long().to_debug(), str![[r#"
334+
Some(
335+
"long",
336+
)
337+
338+
"#]]);
339+
assert_data_eq!(arg.get_action().to_debug(), str![[r#"
340+
Set
341+
342+
"#]]);
343+
assert_data_eq!(arg.is_required_set().to_debug(), str![[r#"
344+
false
345+
346+
"#]]);
347+
assert_data_eq!(arg.get_value_names().to_debug(), str![[r#"
348+
Some(
349+
[
350+
"VALUE",
351+
],
352+
)
353+
354+
"#]]);
355+
assert_data_eq!(arg.get_help().to_debug(), str![[r#"
356+
None
357+
358+
"#]]);
359+
}

0 commit comments

Comments
 (0)