2 releases
0.1.1 | Mar 4, 2020 |
---|---|
0.1.0 | Mar 4, 2020 |
#157 in #arguments
11KB
238 lines
This crate is usefull if you want to easily have an okay cli interface for your program.
you can use it like this:
use std::env::args;
type Error = Box<dyn std::error::Error>;
use simple_arguments::Arguments;
fn main() -> Result<(), Error> {
let mut number: usize = 0;
let mut string: String = String::new();
let mut boolean: bool = false;
let mut help: bool = false;
let usage;
// you have to do this because the lifetimes of the references must be
// greter than the lifetime of the argument struct
{
let mut arguments = Arguments::new(Some("args_tester"));
let mut args = args();
let exec = args.next().unwrap();
let a: Vec<_> = args.collect();
arguments.add(&mut number, "number", "a number");
arguments.add(&mut boolean, "bool", "a boolean value");
arguments.add(&mut string, "string", "a string");
arguments.add_bool(&mut help, "help", "displays the help message");
usage = arguments.usage();
if let Err(e) = arguments.parse(&a[..]) {
println!("{}", e);
print!("{}", usage);
return Ok(());
}
}
if help {
println!("{}", usage);
return Ok(());
}
println!("{} {} {}", number, boolean, string);
Ok(())
}
here, instead of only defining the arguments' names and converting them to the
correct type after, we make use of a special trait Filler
(which is
implemented for all FromStr types) to automatically convert the arguments.