sarge
std-only command-line arguments parser
Sarge is a simple, lightweight argument parser. It has two styles of argument: short (e.g. -h) and long (e.g. --help) (and both), and six different argument types: i64, u64, f64, String, bool, and Vec<T> where T: ArgumentType.
Arguments are registered with an ArgumentParser, and when you're ready, ArgumentParser::parse. Parsing does two things: it sets the value of each argument, and returns a Vec<String> of the values not associated with an argument. Arguments can be created easily via the tag function and registered with ArgumentParser::add, returning an ArgumentRef.
Arguments can be retrieved with ArgumentRef::get(self).
Example:
use sarge::prelude::*;
# fn main() {
let parser = ArgumentParser::new();
let help = parser.add(tag::both('h', "help")); let number = parser.add::<i64>(tag::long("number"));
let arguments = vec![
"my_program".to_string(),
"abc".to_string(),
"--number".to_string(),
"123".to_string(),
"def".to_string(),
];
let remainder = parser.parse_args(&arguments).expect("Failed to parse arguments");
assert_eq!(
help.get(), Ok(false) );
assert_eq!(
number.get(),
Ok(123) );
assert_eq!(
remainder, vec![
"abc".to_string(),
"def".to_string(),
]
);
assert_eq!(
parser.binary(), Some("my_program".to_string())
);
# }
Custom Types
Using the ArgumentType macro, you can implement your own types. Here's an
example (taken from src/test/custom_type.rs):
use sarge::{prelude::*, custom::*};
#[derive(Debug, PartialEq, Eq)]
struct MyCustomType(Vec<String>);
impl ArgumentType for MyCustomType {
type Error = ();
fn arg_type() -> ArgumentValueType {
ArgumentValueType::String
}
fn from_value(val: ArgumentValue) -> Result<Self, Self::Error> {
if let ArgumentValue::String(val) = val {
Ok(Self(val.split(' ').map(|s| s.to_string()).collect()))
} else {
Err(())
}
}
}
fn main() {
let parser = ArgumentParser::new();
let my_argument = parser.add::<MyCustomType>(tag::long("myarg"));
let arguments = [
"custom_type_test".to_string(),
"--myarg".to_string(),
"Hello World !".to_string(),
];
let _ = parser.parse_args(&arguments).expect("failed to parse arguments");
assert_eq!(
my_argument.get(),
Ok(
MyCustomType(
vec![
"Hello".to_string(),
"World".to_string(),
"!".to_string(),
]
)
)
);
}