1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
use std::ffi::OsStr; use std::collections::hash_map::{Entry, Iter}; use std::ops::Deref; use vec_map::VecMap; use args::{ArgMatches, MatchedArg, SubCommand}; use args::settings::ArgSettings; use args::AnyArg; #[doc(hidden)] #[allow(missing_debug_implementations)] pub struct ArgMatcher<'a>(pub ArgMatches<'a>); impl<'a> Default for ArgMatcher<'a> { fn default() -> Self { ArgMatcher(ArgMatches::default()) } } impl<'a> ArgMatcher<'a> { pub fn new() -> Self { ArgMatcher::default() } pub fn get_mut(&mut self, arg: &str) -> Option<&mut MatchedArg> { self.0.args.get_mut(arg) } pub fn get(&self, arg: &str) -> Option<&MatchedArg> { self.0.args.get(arg) } pub fn remove(&mut self, arg: &str) { self.0.args.remove(arg); } pub fn remove_all(&mut self, args: &[&str]) { for &arg in args { self.0.args.remove(arg); } } pub fn insert(&mut self, name: &'a str) { self.0.args.insert(name, MatchedArg::new()); } pub fn contains(&self, arg: &str) -> bool { self.0.args.contains_key(arg) } pub fn is_empty(&self) -> bool { self.0.args.is_empty() } pub fn usage(&mut self, usage: String) { self.0.usage = Some(usage); } pub fn arg_names(&'a self) -> Vec<&'a str> { self.0.args.keys().map(Deref::deref).collect() } pub fn entry(&mut self, arg: &'a str) -> Entry<&'a str, MatchedArg> { self.0.args.entry(arg) } pub fn subcommand(&mut self, sc: SubCommand<'a>) { self.0.subcommand = Some(Box::new(sc)); } pub fn subcommand_name(&self) -> Option<&str> { self.0.subcommand_name() } pub fn iter(&self) -> Iter<&str, MatchedArg> { self.0.args.iter() } pub fn inc_occurrence_of(&mut self, arg: &'a str) { if let Some(a) = self.get_mut(arg) { debugln!("+1 to {}'s occurrences", arg); a.occurs += 1; return; } self.insert(arg); } pub fn inc_occurrences_of(&mut self, args: &[&'a str]) { for arg in args { self.inc_occurrence_of(arg); } } pub fn add_val_to(&mut self, arg: &'a str, val: &OsStr) { let ma = self.entry(arg).or_insert(MatchedArg { occurs: 0, vals: VecMap::new(), }); let len = ma.vals.len() + 1; ma.vals.insert(len, val.to_owned()); } pub fn needs_more_vals<'b, A>(&self, o: &A) -> bool where A: AnyArg<'a, 'b> { if let Some(ma) = self.get(o.name()) { if let Some(num) = o.num_vals() { return if o.is_set(ArgSettings::Multiple) { ((ma.vals.len() as u64) % num) != 0 } else { num != (ma.vals.len() as u64) }; } else if let Some(num) = o.max_vals() { return !((ma.vals.len() as u64) > num); } else if o.min_vals().is_some() { return true; } return o.is_set(ArgSettings::Multiple); } true } } impl<'a> Into<ArgMatches<'a>> for ArgMatcher<'a> { fn into(self) -> ArgMatches<'a> { self.0 } }