[][src]Crate pico_args

An ultra simple CLI arguments parser.

  • Only flags, options and free arguments are supported.
  • Arguments can be separated by a space or =.
  • Non UTF-8 arguments are supported.
  • No help generation.
  • No combined flags (like -vvv or -abc).
  • Arguments are parsed in a linear order. From first to last.

Example

use pico_args::Arguments;

struct Args {
    help: bool,
    version: bool,
    number: u32,
    opt_number: Option<u32>,
    width: u32,
    free: Vec<String>,
}

fn parse_width(s: &str) -> Result<u32, String> {
    s.parse().map_err(|_| "not a number".to_string())
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut args = Arguments::from_env();
    // Arguments can be parsed in any order.
    let args = Args {
        // You can use a slice for multiple commands
        help: args.contains(["-h", "--help"]),
        // or just a string for a single one.
        version: args.contains("-V"),
        // Parses a value that implements `FromStr`.
        number: args.value_from_str("--number")?.unwrap_or(5),
        // Parses an optional value that implements `FromStr`.
        opt_number: args.value_from_str("--opt-number")?,
        // Parses a value using a specified function.
        width: args.value_from_fn("--width", parse_width)?.unwrap_or(10),
        // Will return all free arguments or an error if any flags are left.
        free: args.free()?,
    };

    Ok(())
}

Structs

Arguments

An arguments parser.

Enums

Error

A list of possible errors.