Merge pull request #11 from kbknapp/onlyposfollows

Closes #10 (Adds support for "--" where only positional arguments follow)
This commit is contained in:
Kevin K. 2015-03-17 20:57:51 -04:00
commit e6ca4cfc1b

View file

@ -703,66 +703,71 @@ impl App {
fn get_matches_from(&mut self, matches: &mut ArgMatches, it: &mut IntoIter<String>) { fn get_matches_from(&mut self, matches: &mut ArgMatches, it: &mut IntoIter<String>) {
self.create_help_and_version(); self.create_help_and_version();
// let mut needs_val = false; let mut pos_only = false;
let mut subcmd_name: Option<&'static str> = None; let mut subcmd_name: Option<&'static str> = None;
let mut needs_val_of: Option<&'static str> = None; let mut needs_val_of: Option<&'static str> = None;
let mut pos_counter = 1; let mut pos_counter = 1;
while let Some(arg) = it.next() { while let Some(arg) = it.next() {
let arg_slice = arg.as_slice(); let arg_slice = arg.as_slice();
let mut skip = false; let mut skip = false;
if let Some(nvo) = needs_val_of { if ! pos_only {
if let Some(ref opt) = self.opts.get(nvo) { if let Some(nvo) = needs_val_of {
if self.blacklist.contains(opt.name) { if let Some(ref opt) = self.opts.get(nvo) {
self.report_error( if self.blacklist.contains(opt.name) {
format!("The argument {} is mutually exclusive with one or more other arguments", self.report_error(
if let Some(long) = opt.long { format!("The argument {} is mutually exclusive with one or more other arguments",
format!("--{}",long) if let Some(long) = opt.long {
}else{ format!("--{}",long)
format!("-{}",opt.short.unwrap()) }else{
}),true, true); format!("-{}",opt.short.unwrap())
} }),true, true);
matches.opts.insert(nvo, OptArg{ }
name: opt.name, matches.opts.insert(nvo, OptArg{
short: opt.short, name: opt.name,
long: opt.long, short: opt.short,
help: opt.help, long: opt.long,
requires: None, help: opt.help,
blacklist: None, requires: None,
required: opt.required, blacklist: None,
value: Some(arg.clone()) required: opt.required,
}); value: Some(arg.clone())
if let Some(ref bl) = opt.blacklist { });
if ! bl.is_empty() { if let Some(ref bl) = opt.blacklist {
for name in bl.iter() { if ! bl.is_empty() {
self.blacklist.insert(name); for name in bl.iter() {
self.blacklist.insert(name);
}
} }
} }
} if self.required.contains(opt.name) {
if self.required.contains(opt.name) { self.required.remove(opt.name);
self.required.remove(opt.name); }
} if let Some(ref reqs) = opt.requires {
if let Some(ref reqs) = opt.requires { if ! reqs.is_empty() {
if ! reqs.is_empty() { for n in reqs.iter() {
for n in reqs.iter() { if matches.opts.contains_key(n) { continue; }
if matches.opts.contains_key(n) { continue; } if matches.flags.contains_key(n) { continue; }
if matches.flags.contains_key(n) { continue; } if matches.positionals.contains_key(n) { continue; }
if matches.positionals.contains_key(n) { continue; } self.required.insert(n);
self.required.insert(n); }
} }
} }
skip = true;
} }
skip = true;
} }
} }
if skip { if skip {
needs_val_of = None; needs_val_of = None;
continue; continue;
} }
if arg_slice.starts_with("--") { if arg_slice.starts_with("--") && ! pos_only {
if arg_slice.len() == 2 {
pos_only = true;
continue;
}
// Single flag, or option long version // Single flag, or option long version
needs_val_of = self.parse_long_arg(matches, &arg); needs_val_of = self.parse_long_arg(matches, &arg);
} else if arg_slice.starts_with("-") && arg_slice.len() != 1 && ! pos_only {
} else if arg_slice.starts_with("-") && arg_slice.len() != 1 {
needs_val_of = self.parse_short_arg(matches, &arg); needs_val_of = self.parse_short_arg(matches, &arg);
} else { } else {
// Positional or Subcommand // Positional or Subcommand