Crate argyle[][src]

Expand description

Argyle

Documentation crates.io

This crate contains an agnostic CLI argument parser called Argue. Unlike more robust libraries like clap, Argue does not hold information about expected or required arguments; it merely parses the raw arguments into a consistent state so the implementor can query them as needed.

Post-processing is an exercise largely left to the implementing library to do in its own way, in its own time. Argue exposes several methods for quickly querying the individual pieces of the set, but it can also be dereferenced to a slice or consumed into an owned vector for fully manual processing if desired.

Arguments are processed and held as bytes — Cow<'static, [u8]> — rather than (os)strings, again leaving the choice of later conversion entirely up to the implementor. For non-Musl Linux systems, this is almost entirely non-allocating as CLI arguments map directly back to the CStr pointers. For other systems, Argue falls back to std::env::args_os, so requires a bit more allocation.

For simple applications, this agnostic approach can significantly reduce the overhead of processing CLI arguments, but because handling is left to the implementing library, it might be too tedious or limiting for more complex use cases.

Installation

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

[dependencies]
argyle = "0.4.*"

Example

A general setup might look something like the following. Refer to the documentation for Argue for more information, caveats, etc.

use argyle::{
    Argue,
    ArgyleError,
    FLAG_HELP,
    FLAG_REQUIRED,
    FLAG_VERSION,
};

fn main() {
    if let Err(e) = _main() {
        match(e) {
            // A "-V" or "--version" flag was present.
            ArgyleError::WantsVersion => {
                println!("MyApp v{}", env!("CARGO_PKG_VERSION"));
            },
            // A "-h" or "--help" flag was present.
            ArgyleError::WantsHelp => {
                println!("Help stuff goes here...");
            },
            // An actual error!
            e => {
                eprintln!("{}", e);
                std::process::exit(1);
            },
        }
    }
}

fn _main() -> Result<(), ArgyleError> {
    // Parse CLI arguments.
    let args = Argue::new(FLAG_HELP | FLAG_REQUIRED | FLAG_VERSION)?;

    // Pull the pieces you want.
    let clean: bool = args.switch(b"--clean");
    let prefix: Option<&[u8]> = args.option2(b"-p", b"--prefix");

    Ok(())
}

Structs

Argue

Argue is an agnostic CLI argument parser. Unlike more robust libraries like clap, Argue does not hold information about expected or required arguments; it merely parses the raw arguments into a consistent state so the implementor can query them as needed.

Enums

ArgyleError

Error Struct.

Constants

FLAG_DYNAMIC_HELP

Flag: Check For Help Flag.

FLAG_HELP

Flag: Check For Help Flag.

FLAG_REQUIRED

Flag: Argument(s) Required.

FLAG_SUBCOMMAND

Flag: Expect Subcommand.

FLAG_VERSION

Flag: Check For Version Flag.