mirror of
https://github.com/clap-rs/clap
synced 2024-12-13 22:32:33 +00:00
Closes #17
This commit is contained in:
parent
96d28df880
commit
01e3fc6ddd
3 changed files with 55 additions and 44 deletions
85
src/app.rs
85
src/app.rs
|
@ -223,7 +223,7 @@ impl App {
|
|||
help: a.help,
|
||||
requires: a.requires,
|
||||
required: a.required,
|
||||
values: None
|
||||
values: vec![]
|
||||
});
|
||||
} else {
|
||||
if let Some(ref l) = a.long {
|
||||
|
@ -451,12 +451,12 @@ impl App {
|
|||
self.print_version(true);
|
||||
}
|
||||
|
||||
let mut arg_val: Option<Vec<String>> = None;
|
||||
let mut arg_val: Option<String> = None;
|
||||
|
||||
if arg.contains("=") {
|
||||
let arg_vec: Vec<&str> = arg.split("=").collect();
|
||||
arg = arg_vec[0];
|
||||
arg_val = Some(vec![arg_vec[1].to_string()]);
|
||||
arg_val = Some(arg_vec[1].to_string());
|
||||
}
|
||||
|
||||
for (k, v) in self.opts.iter() {
|
||||
|
@ -466,11 +466,7 @@ 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,
|
||||
|
@ -480,9 +476,13 @@ impl App {
|
|||
blacklist: None,
|
||||
multiple: v.multiple,
|
||||
requires: None,
|
||||
values: arg_val
|
||||
values: if arg_val.is_some() { vec![arg_val.clone().unwrap()]} else {vec![]}
|
||||
});
|
||||
|
||||
match arg_val {
|
||||
None => { return Some(v.name); },
|
||||
_ => { return None; }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -559,27 +559,29 @@ impl App {
|
|||
true, true);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Short flag or opt
|
||||
let arg_c = arg.chars().nth(0).unwrap();
|
||||
self.check_for_help_and_version(arg_c);
|
||||
return None;
|
||||
}
|
||||
// Short flag or opt
|
||||
let arg_c = arg.chars().nth(0).unwrap();
|
||||
|
||||
if ! self.parse_single_short_flag(matches, arg_c) {
|
||||
for (k, v) in self.opts.iter() {
|
||||
if let Some(s) = v.short {
|
||||
if s == arg_c {
|
||||
return Some(k)
|
||||
}
|
||||
}
|
||||
}
|
||||
// Ensure the arg in question isn't a help or version flag
|
||||
self.check_for_help_and_version(arg_c);
|
||||
|
||||
self.report_error(
|
||||
format!("Argument -{} isn't valid",arg_c),
|
||||
true, true);
|
||||
// Check for a matching flag, and return none if found
|
||||
if self.parse_single_short_flag(matches, arg_c) { return None; }
|
||||
|
||||
// Check for matching short in options, and return the name
|
||||
// (only ones with shorts, of course)
|
||||
for v in self.opts.values().filter(|&v| v.short.is_some()) {
|
||||
if v.short.unwrap() == arg_c {
|
||||
return Some(v.name)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
None
|
||||
// Didn't match a flag or option, must be invalid
|
||||
self.report_error( format!("Argument -{} isn't valid",arg_c), true, true);
|
||||
|
||||
unreachable!();
|
||||
}
|
||||
|
||||
fn parse_single_short_flag(&mut self, matches: &mut ArgMatches, arg: char) -> bool {
|
||||
|
@ -723,17 +725,26 @@ impl App {
|
|||
format!("-{}",opt.short.unwrap())
|
||||
}),true, true);
|
||||
}
|
||||
matches.opts.insert(nvo, OptArg{
|
||||
name: opt.name,
|
||||
short: opt.short,
|
||||
long: opt.long,
|
||||
help: opt.help,
|
||||
requires: None,
|
||||
blacklist: None,
|
||||
multiple: opt.multiple,
|
||||
required: opt.required,
|
||||
values: Some(vec![arg.clone()])
|
||||
});
|
||||
let mut done = false;
|
||||
if opt.multiple {
|
||||
if let Some(ref mut o) = matches.opts.get_mut(opt.name) {
|
||||
done = true;
|
||||
o.values.push(arg.clone());
|
||||
}
|
||||
}
|
||||
if ! done {
|
||||
matches.opts.insert(nvo, OptArg{
|
||||
name: opt.name,
|
||||
short: opt.short,
|
||||
long: opt.long,
|
||||
help: opt.help,
|
||||
requires: None,
|
||||
blacklist: None,
|
||||
multiple: opt.multiple,
|
||||
required: opt.required,
|
||||
values: vec![arg.clone()]
|
||||
});
|
||||
}
|
||||
if let Some(ref bl) = opt.blacklist {
|
||||
if ! bl.is_empty() {
|
||||
for name in bl.iter() {
|
||||
|
|
|
@ -102,8 +102,8 @@ 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.values {
|
||||
if let Some(ref s) = v.iter().nth(0) {
|
||||
if ! opt.values.is_empty() {
|
||||
if let Some(ref s) = opt.values.iter().nth(0) {
|
||||
return Some(s);
|
||||
}
|
||||
}
|
||||
|
@ -136,9 +136,9 @@ impl ArgMatches {
|
|||
/// ```
|
||||
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<_>>());
|
||||
}
|
||||
if opt.values.is_empty() { return None; }
|
||||
|
||||
return Some(opt.values.iter().map(|s| &s[..]).collect::<Vec<_>>());
|
||||
}
|
||||
None
|
||||
}
|
||||
|
|
|
@ -31,8 +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
|
||||
/// Allow multiple occurrences of an option argument such as "-c some -c other"
|
||||
pub multiple: bool,
|
||||
/// The value provided to the argument by the user
|
||||
pub values: Option<Vec<String>>
|
||||
pub values: Vec<String>
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue