forked from clap-rs/clap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
groups.rs
166 lines (141 loc) · 3.79 KB
/
groups.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
use clap::Parser;
use crate::utils::assert_output;
#[test]
fn test_safely_nest_parser() {
#[derive(Parser, Debug, PartialEq)]
struct Opt {
#[command(flatten)]
foo: Foo,
}
#[derive(Parser, Debug, PartialEq)]
struct Foo {
#[arg(long)]
foo: bool,
}
assert_eq!(
Opt {
foo: Foo { foo: true }
},
Opt::try_parse_from(["test", "--foo"]).unwrap()
);
}
#[test]
fn implicit_struct_group() {
#[derive(Parser, Debug)]
struct Opt {
#[arg(short, long, requires = "Source")]
add: bool,
#[command(flatten)]
source: Source,
}
#[derive(clap::Args, Debug)]
struct Source {
crates: Vec<String>,
#[arg(long)]
path: Option<std::path::PathBuf>,
#[arg(long)]
git: Option<String>,
}
const OUTPUT: &str = "\
error: the following required arguments were not provided:
<CRATES|--path <PATH>|--git <GIT>>
Usage: prog --add <CRATES|--path <PATH>|--git <GIT>>
For more information, try '--help'.
";
assert_output::<Opt>("prog --add", OUTPUT, true);
use clap::Args;
assert_eq!(Source::group_id(), Some(clap::Id::from("Source")));
assert_eq!(Opt::group_id(), Some(clap::Id::from("Opt")));
}
#[test]
fn skip_group_avoids_duplicate_ids() {
#[derive(Parser, Debug)]
#[group(skip)]
struct Opt {
#[command(flatten)]
first: Compose<Empty, Empty>,
#[command(flatten)]
second: Compose<Empty, Empty>,
}
#[derive(clap::Args, Debug)]
#[group(skip)]
pub struct Compose<L: clap::Args, R: clap::Args> {
#[clap(flatten)]
pub left: L,
#[clap(flatten)]
pub right: R,
}
#[derive(clap::Args, Clone, Copy, Debug)]
#[group(skip)]
pub struct Empty;
use clap::CommandFactory;
Opt::command().debug_assert();
use clap::Args;
assert_eq!(Empty::group_id(), None);
assert_eq!(Compose::<Empty, Empty>::group_id(), None);
assert_eq!(Opt::group_id(), None);
}
#[test]
#[should_panic = "\
Command clap: Argument group name must be unique
\t'Compose' is already in use (note: `Args` implicitly creates `ArgGroup`s; disable with `#[group(skip)]`"]
fn helpful_panic_on_duplicate_groups() {
#[derive(Parser, Debug)]
struct Opt {
#[command(flatten)]
first: Compose<Empty, Empty>,
#[command(flatten)]
second: Compose<Empty, Empty>,
}
#[derive(clap::Args, Debug)]
pub struct Compose<L: clap::Args, R: clap::Args> {
#[clap(flatten)]
pub left: L,
#[clap(flatten)]
pub right: R,
}
#[derive(clap::Args, Clone, Copy, Debug)]
pub struct Empty;
use clap::CommandFactory;
Opt::command().debug_assert();
}
#[test]
fn required_group() {
#[derive(Parser, Debug)]
struct Opt {
#[command(flatten)]
source: Source,
#[command(flatten)]
dest: Dest,
}
#[derive(clap::Args, Debug)]
#[group(required)]
struct Source {
#[arg(long)]
from_path: Option<std::path::PathBuf>,
#[arg(long)]
from_git: Option<String>,
}
#[derive(clap::Args, Debug)]
#[group(required = true)]
struct Dest {
#[arg(long)]
to_path: Option<std::path::PathBuf>,
#[arg(long)]
to_git: Option<String>,
}
use clap::CommandFactory;
let source_id = clap::Id::from("Source");
let dest_id = clap::Id::from("Dest");
let opt_command = Opt::command();
let source_group = opt_command
.get_groups()
.find(|g| g.get_id() == &source_id)
.unwrap();
let dest_group = opt_command
.get_groups()
.find(|g| g.get_id() == &dest_id)
.unwrap();
assert!(source_group.is_required_set());
assert!(dest_group.is_required_set());
}