Skip to content

Commit 9ade2cd

Browse files
committed
feat(YAML): allows using lists or single values with arg declarations
One can now use a list or single value for certain Arg YAML declarations such as possible_values, etc. Prior to this commit, if only a single value was desired one would have to use the format: ```yaml possible_values: - value ``` But now once can use ```yaml possible_values: value ``` Closes #614 Closes #613
1 parent 37cc3c3 commit 9ade2cd

File tree

1 file changed

+27
-47
lines changed

1 file changed

+27
-47
lines changed

src/args/arg.rs

+27-47
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,25 @@ impl<'a, 'b> Arg<'a, 'b> {
145145
let mut a = Arg::with_name(name_str);
146146
let arg_settings = y.get(name_yml).unwrap().as_hash().unwrap();
147147

148+
macro_rules! vec_or_str {
149+
($v:ident, $a:ident, $c:ident) => {{
150+
let maybe_vec = $v.as_vec();
151+
if let Some(vec) = maybe_vec {
152+
for ys in vec {
153+
if let Some(s) = ys.as_str() {
154+
$a = $a.$c(s);
155+
}
156+
}
157+
} else {
158+
if let Some(s) = $v.as_str() {
159+
$a = $a.$c(s);
160+
}
161+
}
162+
$a
163+
}
164+
};
165+
}
166+
148167
for (k, v) in arg_settings.iter() {
149168
a = match k.as_str().unwrap() {
150169
"short" => a.short(v.as_str().unwrap()),
@@ -169,67 +188,28 @@ impl<'a, 'b> Arg<'a, 'b> {
169188
"display_order" => a.display_order(v.as_i64().unwrap() as usize),
170189
"default_value" => a.default_value(v.as_str().unwrap()),
171190
"value_names" => {
172-
for ys in v.as_vec().unwrap() {
173-
if let Some(s) = ys.as_str() {
174-
a = a.value_name(s);
175-
}
176-
}
177-
a
191+
vec_or_str!(v, a, value_name)
178192
}
179193
"groups" => {
180-
for ys in v.as_vec().unwrap() {
181-
if let Some(s) = ys.as_str() {
182-
a = a.group(s);
183-
}
184-
}
185-
a
194+
vec_or_str!(v, a, group)
186195
}
187196
"requires" => {
188-
for ys in v.as_vec().unwrap() {
189-
if let Some(s) = ys.as_str() {
190-
a = a.requires(s);
191-
}
192-
}
193-
a
197+
vec_or_str!(v, a, requires)
194198
}
195199
"conflicts_with" => {
196-
for ys in v.as_vec().unwrap() {
197-
if let Some(s) = ys.as_str() {
198-
a = a.conflicts_with(s);
199-
}
200-
}
201-
a
200+
vec_or_str!(v, a, conflicts_with)
202201
}
203202
"overrides_with" => {
204-
for ys in v.as_vec().unwrap() {
205-
if let Some(s) = ys.as_str() {
206-
a = a.overrides_with(s);
207-
}
208-
}
209-
a
203+
vec_or_str!(v, a, overrides_with)
210204
}
211205
"possible_values" => {
212-
for ys in v.as_vec().unwrap() {
213-
if let Some(s) = ys.as_str() {
214-
a = a.possible_value(s);
215-
}
216-
}
217-
a
206+
vec_or_str!(v, a, possible_value)
218207
}
219208
"required_unless_one" => {
220-
for ys in v.as_vec().unwrap() {
221-
if let Some(s) = ys.as_str() {
222-
a = a.required_unless(s);
223-
}
224-
}
225-
a
209+
vec_or_str!(v, a, required_unless)
226210
}
227211
"required_unless_all" => {
228-
for ys in v.as_vec().unwrap() {
229-
if let Some(s) = ys.as_str() {
230-
a = a.required_unless(s);
231-
}
232-
}
212+
a = vec_or_str!(v, a, required_unless);
233213
a.setb(ArgSettings::RequiredUnlessAll);
234214
a
235215
}

0 commit comments

Comments
 (0)