getopts: improve error handling
This commit is contained in:
parent
ffff48fcfe
commit
0c9ce2bbf0
@ -14,15 +14,15 @@ pub struct ParameterError {
|
||||
|
||||
impl ParameterError {
|
||||
|
||||
fn new() -> Self {
|
||||
pub fn new() -> Self {
|
||||
Self { error_list: vec![] }
|
||||
}
|
||||
|
||||
fn push(&mut self, value: Error) {
|
||||
pub fn push(&mut self, value: Error) {
|
||||
self.error_list.push(value);
|
||||
}
|
||||
|
||||
fn len(&self) -> usize {
|
||||
pub fn len(&self) -> usize {
|
||||
self.error_list.len()
|
||||
}
|
||||
}
|
||||
|
@ -36,13 +36,19 @@ fn parse_argument(arg: &str) -> RawArgument {
|
||||
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 {
|
||||
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![];
|
||||
@ -73,7 +79,8 @@ pub fn parse_arguments(args: &Vec<String>, schema: &Schema) -> Value {
|
||||
if default == false {
|
||||
data.push((name, "true".to_string()));
|
||||
} else {
|
||||
panic!("negative Bool requires argument");
|
||||
errors.push(format_err!("parameter '{}': {}", name,
|
||||
"boolean requires argument."));
|
||||
}
|
||||
} else {
|
||||
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; }
|
||||
}
|
||||
|
||||
println!("Options {:?}", data);
|
||||
println!("REST {:?}", rest);
|
||||
if errors.len() > 0 { return Err(errors); }
|
||||
|
||||
match parse_parameter_strings(&data, schema, true) {
|
||||
Ok(value) => value,
|
||||
Err(perror) => {
|
||||
panic!(format!("{:?}", perror));
|
||||
}
|
||||
}
|
||||
let options = parse_parameter_strings(&data, schema, true)?;
|
||||
|
||||
Ok((options,rest))
|
||||
}
|
||||
|
13
src/main.rs
13
src/main.rs
@ -18,14 +18,23 @@ use futures::future::Future;
|
||||
use hyper;
|
||||
|
||||
fn main() {
|
||||
println!("Fast Static Type Definitions 1");
|
||||
println!("Proxmox REST Server example.");
|
||||
|
||||
let schema = parameter!{
|
||||
name => ApiString!{ optional => false }
|
||||
};
|
||||
|
||||
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();
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user