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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
pub mod errors;
|
2015-05-28 09:19:19 -04:00
|
|
|
pub mod lexer;
|
2015-04-28 01:10:37 -04:00
|
|
|
pub mod matches;
|
|
|
|
mod opts;
|
|
|
|
|
|
|
|
use std::env::Args;
|
|
|
|
|
2015-04-28 18:40:22 +00:00
|
|
|
pub use errors::{Error, ErrorKind};
|
2015-04-28 01:10:37 -04:00
|
|
|
pub use matches::Matches;
|
2015-05-28 21:04:00 +00:00
|
|
|
pub use lexer::{analyze, collect, Token};
|
2015-06-04 15:37:59 -04:00
|
|
|
use opts::{opts, Opts};
|
2015-04-28 01:10:37 -04:00
|
|
|
|
2015-04-30 15:14:34 +00:00
|
|
|
pub fn parse(mut args: Args, options: &[&'static str]) -> Result<Matches, Error> {
|
2015-04-28 01:10:37 -04:00
|
|
|
let mut matches: Matches = Matches::new();
|
|
|
|
|
2015-05-28 09:19:19 -04:00
|
|
|
let tokens = match lexer::collect(options) {
|
|
|
|
Err(why) => return Err(why),
|
|
|
|
Ok(t) => t
|
|
|
|
};
|
|
|
|
matches.tokens = tokens.clone();
|
|
|
|
let mut opts: Opts = opts(&tokens);
|
2015-04-28 01:10:37 -04:00
|
|
|
|
|
|
|
args.next(); // Remove the program name from the list of program arguments
|
|
|
|
|
|
|
|
let mut next_arg = args.next();
|
|
|
|
while next_arg.is_some() {
|
|
|
|
let mut current_arg = next_arg.unwrap();
|
2015-06-04 15:37:59 -04:00
|
|
|
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 opts; e.g. tar -xzf
|
|
|
|
for c in current_arg[1..].chars() {
|
|
|
|
let mut s = String::new();
|
|
|
|
s.push(c);
|
|
|
|
arg_vec.push(s);
|
|
|
|
}
|
2015-04-28 01:10:37 -04:00
|
|
|
}
|
|
|
|
|
2015-06-04 15:37:59 -04:00
|
|
|
for arg in arg_vec.iter() {
|
|
|
|
if opts.contains_opt(&arg) {
|
|
|
|
let has_arg: bool = *opts.get_opt(&arg).unwrap();
|
|
|
|
|
|
|
|
if has_arg {
|
|
|
|
// NOTE: The corresponding arg MUST be immediately following
|
|
|
|
current_arg = match args.next() {
|
|
|
|
None => {
|
|
|
|
let arg_ = (*arg).clone();
|
|
|
|
return Err(Error::new(ErrorKind::MissingArgument, arg_));
|
|
|
|
},
|
|
|
|
Some(a) => a
|
|
|
|
};
|
|
|
|
|
|
|
|
matches.insert(&arg, ¤t_arg);
|
|
|
|
} else {
|
|
|
|
matches.insert(&arg, "");
|
|
|
|
}
|
2015-04-28 01:10:37 -04:00
|
|
|
} else {
|
2015-06-04 15:37:59 -04:00
|
|
|
let arg_ = (*arg).clone();
|
|
|
|
return Err(Error::new(ErrorKind::InvalidArgument, arg_));
|
2015-04-28 01:10:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else { // Probably a required arg
|
|
|
|
let arg_name: String = opts.get_arg().unwrap();
|
|
|
|
matches.insert(&arg_name, ¤t_arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
next_arg = args.next();
|
|
|
|
}
|
|
|
|
|
2015-04-30 15:14:34 +00:00
|
|
|
match opts.arg_len() {
|
|
|
|
0 => Ok(matches),
|
|
|
|
_ => Err(Error::new(ErrorKind::MissingArgument, opts.get_arg().unwrap())),
|
|
|
|
}
|
2015-04-28 01:10:37 -04:00
|
|
|
}
|
|
|
|
|
2015-06-04 15:37:59 -04:00
|
|
|
pub fn help(tokens: &Vec<lexer::Token>, program_name: &str, program_desc: &str) {
|
|
|
|
println!("{} - {}", program_name, program_desc);
|
2015-04-28 01:10:37 -04:00
|
|
|
|
2015-06-04 15:37:59 -04:00
|
|
|
for token in tokens.iter() {
|
|
|
|
println!("{}", token);
|
2015-04-28 01:10:37 -04:00
|
|
|
}
|
|
|
|
}
|