SimpleArgs: Simple Command-Line Argument Parsing for Rust

This is a simple, small library for parsing command-line arguments in Rust.

You write your own parser which iterates over the arguments. SimpleArgs interprets the raw arguments and gives you high-quality error messages.

Example

``` use simpleargs::{Arg, Args, UsageError, OptionError}; use std::ffi::OsString; use std::str::FromStr;

fn parseargs(mut args: Args) -> Result<(), UsageError> where T: Iterator, { // The input file let mut input: Option = None; // True if -flag was set let mut flag = false; // The value of -xvalue, if it was used let mut xvalue: Option = None; loop { match args.next() { Arg::Positional(arg) => if input.issome() { return Err(UsageError::UnexpectedArgument { arg }); } else { input = Some(arg) } Arg::Named(arg) => arg.parse(|name, value| match name { "flag" => { // parse() above will return an error for -flag=value, // because this function does not use 'value'. flag = true; Ok(()) } "xvalue" => { // Call asstr() for a str, or asosstr() for OsStr. xvalue = Some(i32::fromstr(value.asstr()?)?); Ok(()) } _ => Err(OptionError::Unknown), })?, Arg::End => break, Arg::Error(err) => return Err(err), } } let input = match input { Some(path) => path, None => return Err(UsageError::MissingArgument { name: "input".to_owned() }), }; Ok(()) } ```

Goals and Non-Goals

Limitations

Known limitations we intend to fix:

Opinions

Known limitations that accepted as the library’s design:

Comparisons