use std::env;
use std::collections::HashMap;
use super::parameter::Parameter;
use super::args::Args;
#[cfg(test)]
mod tests {
use super::{Parser, Parameter};
#[test]
fn can_create_parser() {
let _parser = Parser::new("");
}
#[test]
fn can_help() {
let mut parser = Parser::new("Test Parser 0");
parser.add_parameter(Parameter::param("--test", "A test parameter").alias("-t"));
parser.help();
}
#[test]
fn can_parse() {
let mut parser = Parser::new("Test Parser 0");
parser.add_parameter(Parameter::param("--test", "A test parameter").alias("-t"));
println!("{:?}", parser.parse_args());
}
}
#[derive(Debug)]
struct ParamInfo {
takes_value: bool,
description: String,
}
impl ParamInfo {
pub fn new(takes_value: bool, description: String) -> ParamInfo {
return ParamInfo{takes_value: takes_value, description: description};
}
}
#[derive(Debug)]
pub struct Parser {
description: String,
parameters: HashMap<String, ParamInfo>,
aliases: HashMap<String, String>,
default_args: Args,
}
impl Parser {
pub fn new(description: &str) -> Parser {
return Parser{description: String::from(description), parameters: HashMap::new(), aliases: HashMap::new(), default_args: Args::new()};
}
pub fn add_parameter(&mut self, param: Parameter) {
if let Some(alias) = param.alias {
self.aliases.insert(alias, param.name.clone());
}
if param.takes_value {
self.default_args.arguments.insert(param.name.clone(), param.default);
}
self.parameters.insert(param.name, ParamInfo::new(param.takes_value, param.description));
}
pub fn help(&self) {
let mut parameter_aliases = HashMap::new();
for (key, value) in &self.aliases {
parameter_aliases.insert(value, key);
}
let mut usage = format!("{}\nUsage: {}", self.description, env::current_exe().unwrap().to_str().unwrap());
for (mut param, _) in &self.parameters {
let metavar = String::from(param.to_uppercase().trim_start_matches("-"));
if let Some(short) = parameter_aliases.get(param) {
param = short;
}
usage += &format!(" [{} {}]", param, metavar);
}
println!("{}", usage);
for (param, info) in &self.parameters {
let mut print_arg = String::from("\t");
if let Some(short) = parameter_aliases.get(param) {
print_arg += &format!("{},", short);
}
print_arg += &format!("\t{}", param);
println!("{}\t\t{}", print_arg, info.description);
}
}
pub fn parse_args(&self) -> Args {
fn fail(parser: &Parser) -> ! {
parser.help();
std::process::exit(1);
}
let mut args = self.default_args.clone();
let mut args_iter = env::args().into_iter().skip(1);
while let Some(arg) = args_iter.next() {
let mut full_arg = arg.clone();
if let Some(full_name) = self.aliases.get(&arg) {
full_arg = full_name.clone();
}
if let Some(info) = self.parameters.get(&full_arg) {
if info.takes_value {
match args_iter.next() {
Some(value) => args.arguments.insert(full_arg, Some(value)),
None => fail(&self),
};
} else {
args.flags.insert(full_arg);
}
} else if arg == "-h" || arg == "--help" {
self.help();
std::process::exit(0);
} else {
args.positional.push(arg);
}
}
let mut missing_args = Vec::new();
for (arg, value_opt) in &args.arguments {
if value_opt.is_none() {
missing_args.push(arg);
}
}
if !missing_args.is_empty() {
println!("Missing required arguments: {:?}", missing_args);
fail(&self);
}
return args;
}
}