Skip to content

A lightweight, agnostic CLI argument parsing library for Rust.

License

Notifications You must be signed in to change notification settings

Blobfolio/argyle

Repository files navigation

Argyle

docs.rs changelog
crates.io ci deps.rs
license contributions welcome

This crate provides an argue! macro for generating a CLI argument enum and parsing iterator, ideal for use cases requiring more than the standard library's barebones args_os helper, but less than full-service options like clap.

Example

The docs contain a lot of addition details, but here's a minimal preview to give you an idea of what it's all about.

use argyle::argue;

// Construct the enum and iterator.
argue! {
    Help    "-h" "--help",
    Version "-V" "--version",
    Stderr       "--stderr",

    @options
    Format       "--format",
    Level   "-l" "--level",
}

/// # Main.
fn main() {
    // Example settings.
    let mut stderr = false;
    let mut format: Option<Format> = None;
    let mut level = 0_u8;
    let mut paths: Vec<PathBuf> = Vec::new();

    // Loop through the environmental arguments, taking whatever actions
    // make sense for your application.
    for arg in Argument::args_os() {
        match arg {
            // You named these!
            Argument::Help => print_help(),
            Argument::Version => print_version(),
            Argument::Stderr => { stderr = true; },

            // Options come with the value as a String.
            Argument::Format(v) => {
                format = Format::from_str(v);
            },
            Argument::Level(v) => {
                level = v.parse().unwrap_or(0);
            },

            // Unmatched String values map to the first generic catchall.
            Argument::Other(v) => {
                eprintln!("Warning: unrecognized CLI argument {v}.");
            },

            // Unmatched values with invalid UTF-8 will be passed through
            // to the second generic catchall as OsString values.
            Argument::OtherOs(v) => {
                eprintln!(
                    "Warning: unrecognized CLI argument {}.",
                    v.display(),
                );
            },
        }
    }

    // Now that the settings have been worked out, do something!
    // …
}

Installation

Add argyle to your dependencies in Cargo.toml, like:

[dependencies]
argyle = "0.14.*"

About

A lightweight, agnostic CLI argument parsing library for Rust.

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project