use std::collections::HashMap;
use args::{ FlagArg, OptArg, PosArg };
use subcommand::SubCommand;
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<(&'static str, 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<&String> {
if let Some(ref opt) = self.opts.get(name) {
if let Some(ref v) = opt.value {
return Some(v);
}
}
if let Some(ref pos) = self.positionals.get(name) {
if let Some(ref v) = pos.value {
return Some(v);
}
}
None
}
pub fn is_present(&self, name: &'static str) -> bool {
if let Some((sc_name, _ )) = self.subcommand {
if sc_name == name { return true; }
}
if self.flags.contains_key(name) ||
self.opts.contains_key(name) ||
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;
}
0
}
pub fn subcommand_matches(&self, name: &'static str) -> Option<&ArgMatches> {
if let Some( ( sc_name, 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((name, _)) = self.subcommand {
return Some(name);
}
None
}
}