getopts: improve error handling

This commit is contained in:
Dietmar Maurer 2018-11-17 09:57:26 +01:00
parent ffff48fcfe
commit 0c9ce2bbf0
3 changed files with 29 additions and 17 deletions

View File

@ -14,15 +14,15 @@ pub struct ParameterError {
impl ParameterError { impl ParameterError {
fn new() -> Self { pub fn new() -> Self {
Self { error_list: vec![] } Self { error_list: vec![] }
} }
fn push(&mut self, value: Error) { pub fn push(&mut self, value: Error) {
self.error_list.push(value); self.error_list.push(value);
} }
fn len(&self) -> usize { pub fn len(&self) -> usize {
self.error_list.len() self.error_list.len()
} }
} }

View File

@ -36,13 +36,19 @@ fn parse_argument(arg: &str) -> RawArgument {
RawArgument::Argument { value: arg.to_string() } RawArgument::Argument { value: arg.to_string() }
} }
pub fn parse_arguments(args: &Vec<String>, schema: &Schema) -> Value { pub fn parse_arguments(
args: &Vec<String>,
schema: &Schema,
) -> Result<(Value,Vec<String>), ParameterError> {
println!("ARGS {:?}", args); let mut errors = ParameterError::new();
let properties = match schema { let properties = match schema {
Schema::Object(ObjectSchema { properties, .. }) => properties, Schema::Object(ObjectSchema { properties, .. }) => properties,
_ => panic!("Expected Object Schema."), _ => {
errors.push(format_err!("parse arguments failed - got strange parameters (expected object schema)."));
return Err(errors);
},
}; };
let mut data: Vec<(String, String)> = vec![]; let mut data: Vec<(String, String)> = vec![];
@ -73,7 +79,8 @@ pub fn parse_arguments(args: &Vec<String>, schema: &Schema) -> Value {
if default == false { if default == false {
data.push((name, "true".to_string())); data.push((name, "true".to_string()));
} else { } else {
panic!("negative Bool requires argument"); errors.push(format_err!("parameter '{}': {}", name,
"boolean requires argument."));
} }
} else { } else {
data.push((name, "true".to_string())); data.push((name, "true".to_string()));
@ -97,13 +104,9 @@ pub fn parse_arguments(args: &Vec<String>, schema: &Schema) -> Value {
if pos >= args.len() { break; } if pos >= args.len() { break; }
} }
println!("Options {:?}", data); if errors.len() > 0 { return Err(errors); }
println!("REST {:?}", rest);
match parse_parameter_strings(&data, schema, true) { let options = parse_parameter_strings(&data, schema, true)?;
Ok(value) => value,
Err(perror) => { Ok((options,rest))
panic!(format!("{:?}", perror));
}
}
} }

View File

@ -18,14 +18,23 @@ use futures::future::Future;
use hyper; use hyper;
fn main() { fn main() {
println!("Fast Static Type Definitions 1"); println!("Proxmox REST Server example.");
let schema = parameter!{ let schema = parameter!{
name => ApiString!{ optional => false } name => ApiString!{ optional => false }
}; };
let args: Vec<String> = std::env::args().skip(1).collect(); let args: Vec<String> = std::env::args().skip(1).collect();
getopts::parse_arguments(&args, &schema); match getopts::parse_arguments(&args, &schema) {
Ok((options, rest)) => {
println!("Got Options: {}", options);
println!("Remaining Arguments: {:?}", rest);
}
Err(err) => {
eprintln!("Unable to parse arguments:\n{}", err);
std::process::exit(-1);
}
}
let addr = ([127, 0, 0, 1], 8007).into(); let addr = ([127, 0, 0, 1], 8007).into();