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
#[macro_export]
macro_rules! clap_dispatch {
($matches:expr; { $( $name:ident $match_arm_args:tt => $callback:expr ),* }) => {
match $matches.subcommand_name() {
$(
Some(stringify!($name)) => {
let matches = $matches.subcommand_matches(stringify!($name)).unwrap();
clap_dispatch!(MATCH_ARM_ARGS, matches, $match_arm_args);
$callback;
}
)*
Some(x) => {
panic!("Internal error: Command not covered: {}", x);
},
None => {
println!("Subcommand required. See --help for help.");
::std::process::exit(1);
}
}
};
(MATCH_ARM_ARGS, $matches:ident, ( $matches_name:pat, $($arg_name:ident as $varname:ident),* )) => {
$(
let $varname = $matches.value_of(stringify!($arg_name)).unwrap();
)*
let $matches_name = $matches;
};
(MATCH_ARM_ARGS, $matches:ident, ( $matches_name:pat )) => { clap_dispatch!(MATCH_ARM_ARGS, $matches, ($matches_name,)) };
(MATCH_ARM_ARGS, $matches:ident, ()) => { clap_dispatch!(MATCH_ARM_ARGS, $matches, (_,)) };
}