use std::collections::HashMap;
use args::SubCommand;
use args::flagarg::FlagArg;
use args::optarg::OptArg;
use args::posarg::PosArg;
pub struct ArgMatches {
pub matches_of: &'static str,
pub flags: HashMap<&'static str, FlagArg>,
pub opts: HashMap<&'static str, OptArg>,
pub positionals: HashMap<&'static str, PosArg>,
pub subcommand: Option<Box<SubCommand>>
}
impl ArgMatches {
pub fn new(name: &'static str) -> ArgMatches {
ArgMatches {
matches_of: name,
flags: HashMap::new(),
opts: HashMap::new(),
positionals: HashMap::new(),
subcommand: None
}
}
pub fn value_of(&self, name: &'static str) -> Option<&str> {
if let Some(ref opt) = self.opts.get(name) {
if !opt.values.is_empty() {
if let Some(ref s) = opt.values.iter().nth(0) {
return Some(&s[..]);
}
}
}
if let Some(ref pos) = self.positionals.get(name) {
return Some(&pos.value[..]);
}
None
}
pub fn values_of(&self, name: &'static str) -> Option<Vec<&str>> {
if let Some(ref opt) = self.opts.get(name) {
if opt.values.is_empty() { return None; }
return Some(opt.values.iter().map(|s| &s[..]).collect::<Vec<_>>());
}
None
}
pub fn is_present(&self, name: &'static str) -> bool {
if let Some(ref sc) = self.subcommand {
if sc.name == name { return true; }
}
if self.flags.contains_key(name) {return true;}
if self.opts.contains_key(name) {return true;}
if self.positionals.contains_key(name) {return true;}
false
}
pub fn occurrences_of(&self, name: &'static str) -> u8 {
if let Some(ref f) = self.flags.get(name) {
return f.occurrences;
}
if let Some(ref o) = self.opts.get(name) {
return o.occurrences;
}
0
}
pub fn subcommand_matches(&self, name: &'static str) -> Option<&ArgMatches> {
if let Some( ref sc) = self.subcommand {
if sc.name != name { return None; }
return Some(&sc.matches);
}
None
}
pub fn subcommand_name(&self) -> Option<&'static str> {
if let Some( ref sc ) = self.subcommand {
return Some(sc.name);
}
None
}
}