2015-04-28 01:10:37 -04:00
|
|
|
/* Pirate - A command-line arrrrguments parser, written in Rust.
|
|
|
|
* Copyright (C) 2015 Zachary Dziura
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::collections::hash_map::Keys;
|
2015-06-09 16:50:16 -04:00
|
|
|
|
|
|
|
use errors::{Error, ErrorKind};
|
|
|
|
use vars::Vars;
|
2015-04-28 01:10:37 -04:00
|
|
|
|
|
|
|
pub struct Matches {
|
2015-06-16 14:28:14 -04:00
|
|
|
matches: HashMap<String, String>
|
2015-04-28 01:10:37 -04:00
|
|
|
}
|
|
|
|
|
2015-06-16 14:28:14 -04:00
|
|
|
pub fn matches(vars: &mut Vars, env_args: &[String]) -> Result<Matches, Error> {
|
|
|
|
let mut matches: HashMap<String, String> = HashMap::new();
|
|
|
|
let mut args = env_args.iter();
|
|
|
|
|
|
|
|
args.next(); // Remove the program name
|
|
|
|
|
|
|
|
let mut next_arg = args.next();
|
|
|
|
while next_arg.is_some() {
|
|
|
|
let mut current_arg = next_arg.unwrap();
|
|
|
|
let mut arg_vec: Vec<String> = Vec::new();
|
|
|
|
|
|
|
|
// Determine if current opt is in short, long, or arg form
|
|
|
|
if ¤t_arg[..1] == "-" {
|
|
|
|
if ¤t_arg[..2] == "--" { // Long form opt
|
|
|
|
arg_vec.push(String::from(¤t_arg[2..]));
|
|
|
|
} else { // Short form opt
|
|
|
|
// Assuming it's a group of short-form vars; e.g. tar -xzf
|
|
|
|
for c in current_arg[1..].chars() {
|
|
|
|
let mut s = String::new();
|
|
|
|
s.push(c);
|
|
|
|
arg_vec.push(s);
|
2015-06-09 16:50:16 -04:00
|
|
|
}
|
2015-06-16 14:28:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for arg in arg_vec.iter() {
|
|
|
|
if vars.contains_opt(arg) {
|
|
|
|
let token = vars.get_opt(arg).unwrap();
|
|
|
|
|
|
|
|
if token.has_arg {
|
|
|
|
// NOTE: The corresponding arg MUST be immediately following
|
|
|
|
current_arg = match args.next() {
|
|
|
|
None => return Err(Error::new(ErrorKind::MissingArgument, arg.clone())),
|
|
|
|
Some(a) => a
|
|
|
|
};
|
2015-06-09 16:50:16 -04:00
|
|
|
|
2015-06-16 14:28:14 -04:00
|
|
|
matches.insert(token.name(), current_arg.clone());
|
2015-06-09 16:50:16 -04:00
|
|
|
} else {
|
2015-06-16 14:28:14 -04:00
|
|
|
matches.insert(token.name(), String::new());
|
2015-06-09 16:50:16 -04:00
|
|
|
}
|
2015-06-16 14:28:14 -04:00
|
|
|
} else {
|
|
|
|
return Err(Error::new(ErrorKind::InvalidArgument, arg.clone()));
|
2015-06-09 16:50:16 -04:00
|
|
|
}
|
|
|
|
}
|
2015-06-16 14:28:14 -04:00
|
|
|
} else { // Probably a required arg
|
|
|
|
let arg = vars.get_arg().unwrap();
|
|
|
|
matches.insert(arg.name(), current_arg.clone());
|
2015-04-28 01:10:37 -04:00
|
|
|
}
|
|
|
|
|
2015-06-16 14:28:14 -04:00
|
|
|
next_arg = args.next();
|
2015-04-28 01:10:37 -04:00
|
|
|
}
|
|
|
|
|
2015-06-16 14:28:14 -04:00
|
|
|
match vars.arg_len() {
|
|
|
|
0 => Ok(Matches { matches: matches }),
|
|
|
|
_ => Err(Error::new(ErrorKind::MissingArgument, vars.get_arg().unwrap().name())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Matches {
|
2015-04-28 01:10:37 -04:00
|
|
|
pub fn get(&self, arg: &str) -> Option<&String> {
|
|
|
|
self.matches.get(arg)
|
|
|
|
}
|
|
|
|
|
2015-06-16 14:28:14 -04:00
|
|
|
pub fn has_match(&self, arg: &str) -> bool {
|
2015-04-28 01:10:37 -04:00
|
|
|
let arg = String::from(arg);
|
|
|
|
self.matches.contains_key(&arg)
|
|
|
|
}
|
|
|
|
|
2015-06-16 14:28:14 -04:00
|
|
|
pub fn matches(&self) -> Keys<String, String> {
|
2015-04-28 01:10:37 -04:00
|
|
|
self.matches.keys()
|
|
|
|
}
|
|
|
|
}
|
2015-06-16 14:28:14 -04:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::Matches;
|
|
|
|
use super::super::errors::{Error, ErrorKind};
|
|
|
|
use super::super::vars::Vars;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_matches() {
|
|
|
|
let opts = vec!["o/opt(An option)", "a(Argument):"];
|
|
|
|
let env_args = vec![String::from("test"), String::from("-a"), String::from("Test")];
|
|
|
|
let mut vars = Vars::new("Test", &opts).unwrap();
|
|
|
|
let matches = match Matches::new(&mut vars, &env_args) {
|
|
|
|
Ok(m) => m,
|
|
|
|
Err(why) => panic!("An error occurred: {}", why)
|
|
|
|
};
|
|
|
|
|
|
|
|
assert_eq!(matches.get("opt").unwrap(), &String::from("opt"));
|
|
|
|
}
|
|
|
|
}
|