mirror of
https://github.com/clap-rs/clap
synced 2024-12-12 22:02:35 +00:00
Initial commit for multiple option arguments
This commit is contained in:
parent
d9244b6879
commit
96d28df880
3 changed files with 52 additions and 13 deletions
23
src/app.rs
23
src/app.rs
|
@ -218,11 +218,12 @@ impl App {
|
|||
name: a.name,
|
||||
short: a.short,
|
||||
long: a.long,
|
||||
multiple: a.multiple,
|
||||
blacklist: a.blacklist,
|
||||
help: a.help,
|
||||
requires: a.requires,
|
||||
required: a.required,
|
||||
value: None
|
||||
values: None
|
||||
});
|
||||
} else {
|
||||
if let Some(ref l) = a.long {
|
||||
|
@ -450,12 +451,12 @@ impl App {
|
|||
self.print_version(true);
|
||||
}
|
||||
|
||||
let mut arg_val: Option<String> = None;
|
||||
let mut arg_val: Option<Vec<String>> = None;
|
||||
|
||||
if arg.contains("=") {
|
||||
let arg_vec: Vec<&str> = arg.split("=").collect();
|
||||
arg = arg_vec[0];
|
||||
arg_val = Some(arg_vec[1].to_string());
|
||||
arg_val = Some(vec![arg_vec[1].to_string()]);
|
||||
}
|
||||
|
||||
for (k, v) in self.opts.iter() {
|
||||
|
@ -465,6 +466,11 @@ impl App {
|
|||
self.report_error(format!("The argument --{} is mutually exclusive with one or more other arguments", arg),
|
||||
true, true);
|
||||
}
|
||||
let mut name_to_return = Option<
|
||||
match arg_val {
|
||||
None => { return Some(v.name); },
|
||||
_ => { return None; }
|
||||
}
|
||||
matches.opts.insert(k, OptArg{
|
||||
name: v.name,
|
||||
short: v.short,
|
||||
|
@ -472,13 +478,11 @@ impl App {
|
|||
help: v.help,
|
||||
required: v.required,
|
||||
blacklist: None,
|
||||
multiple: v.multiple,
|
||||
requires: None,
|
||||
value: arg_val.clone()
|
||||
values: arg_val
|
||||
});
|
||||
match arg_val {
|
||||
None => { return Some(v.name); },
|
||||
_ => { return None; }
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -726,8 +730,9 @@ impl App {
|
|||
help: opt.help,
|
||||
requires: None,
|
||||
blacklist: None,
|
||||
multiple: opt.multiple,
|
||||
required: opt.required,
|
||||
value: Some(arg.clone())
|
||||
values: Some(vec![arg.clone()])
|
||||
});
|
||||
if let Some(ref bl) = opt.blacklist {
|
||||
if ! bl.is_empty() {
|
||||
|
|
|
@ -86,7 +86,10 @@ impl ArgMatches {
|
|||
|
||||
/// Gets the value of a specific option or positional argument (i.e. an argument that takes
|
||||
/// an additional value at runtime). If the option wasn't present at runtime
|
||||
/// it returns `None`
|
||||
/// it returns `None`.
|
||||
///
|
||||
/// *NOTE:* If getting a value for an option argument that allows multiples, prefer `values_of()`
|
||||
/// as `value_of()` will only return the _*first*_ value.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
|
@ -99,8 +102,10 @@ impl ArgMatches {
|
|||
/// ```
|
||||
pub fn value_of(&self, name: &'static str) -> Option<&String> {
|
||||
if let Some(ref opt) = self.opts.get(name) {
|
||||
if let Some(ref v) = opt.value {
|
||||
return Some(v);
|
||||
if let Some(ref v) = opt.values {
|
||||
if let Some(ref s) = v.iter().nth(0) {
|
||||
return Some(s);
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Some(ref pos) = self.positionals.get(name) {
|
||||
|
@ -111,6 +116,33 @@ impl ArgMatches {
|
|||
None
|
||||
}
|
||||
|
||||
/// Gets the values of a specific option in a vector (i.e. an argument that takes
|
||||
/// an additional value at runtime). If the option wasn't present at runtime
|
||||
/// it returns `None`
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```no_run
|
||||
/// # use clap::{App, Arg};
|
||||
/// # let matches = App::new("myapp").arg(Arg::new("output").takes_value(true)).get_matches();
|
||||
/// // If the program had option "-c" that took a value and was run
|
||||
/// // via "myapp -o some -o other -o file"
|
||||
/// // values_of() would return a [&str; 3] ("some", "other", "file")
|
||||
/// if let Some(os) = matches.values_of("output") {
|
||||
/// for o in os {
|
||||
/// println!("A value for output: {}", o);
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
pub fn values_of(&self, name: &'static str) -> Option<Vec<&str>> {
|
||||
if let Some(ref opt) = self.opts.get(name) {
|
||||
if let Some(ref v) = opt.values {
|
||||
return Some(v.iter().map(|s| &s[..]).collect::<Vec<_>>());
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// Checks if a flag was argument was supplied at runtime. **DOES NOT** work for
|
||||
/// option or positional arguments (use `.value_of()` instead)
|
||||
///
|
||||
|
|
|
@ -31,6 +31,8 @@ pub struct OptArg {
|
|||
pub requires: Option<Vec<&'static str>>,
|
||||
/// A list of names for other arguments that *may not* be used with this flag
|
||||
pub blacklist: Option<Vec<&'static str>>,
|
||||
/// Allow multiple values of an option argument
|
||||
pub multiple: bool,
|
||||
/// The value provided to the argument by the user
|
||||
pub value: Option<String>
|
||||
pub values: Option<Vec<String>>
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue