Skip to content

Commit e243fe3

Browse files
committed
feat(macros): add ability to get a typed multiple values
1 parent 4b7cd3e commit e243fe3

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

src/macros.rs

+24-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,29 @@ macro_rules! value_t {
4040
Err(_) => Err(format!("{} isn't a valid {}",v,stringify!($t))),
4141
}
4242
},
43-
None => Err(format!("Argument not found"))
43+
None => Err(format!("Argument \"{}\" not found", $v))
44+
}
45+
};
46+
($m:ident.values_of($v:expr), $t:ty) => {
47+
match $m.values_of($v) {
48+
Some(ref v) => {
49+
let mut tmp = Vec::with_capacity(v.len());
50+
let mut err = None;
51+
for pv in v {
52+
match pv.parse::<$t>() {
53+
Ok(rv) => tmp.push(rv),
54+
Err(_) => {
55+
err = Some(format!("{} isn't a valid {}",pv,stringify!($t)));
56+
break
57+
}
58+
}
59+
}
60+
match err {
61+
Some(e) => Err(e),
62+
None => Ok(tmp)
63+
}
64+
},
65+
None => Err(format!("Argument \"{}\" not found", $v))
4466
}
4567
};
4668
}
@@ -60,7 +82,7 @@ macro_rules! value_t_or_exit {
6082
}
6183
},
6284
None => {
63-
println!("Argument not found\n{}\nPlease re-run with --help for more information", $m.usage());
85+
println!("Argument \"{}\" not found or is not valid\n{}\nPlease re-run with --help for more information",$v, $m.usage());
6486
::std::process::exit(1);
6587
}
6688
}

0 commit comments

Comments
 (0)