26 releases

new 0.7.2 Feb 15, 2024
0.7.0 Jan 21, 2024
0.6.8 Jun 1, 2023
0.6.7 Feb 8, 2023
0.3.1 Mar 5, 2021

#69 in Command-line interface

Download history 1964/week @ 2023-11-01 1640/week @ 2023-11-08 3196/week @ 2023-11-15 2693/week @ 2023-11-22 3009/week @ 2023-11-29 2716/week @ 2023-12-06 2736/week @ 2023-12-13 1128/week @ 2023-12-20 987/week @ 2023-12-27 1913/week @ 2024-01-03 2333/week @ 2024-01-10 3933/week @ 2024-01-17 3410/week @ 2024-01-24 2151/week @ 2024-01-31 2448/week @ 2024-02-07 3046/week @ 2024-02-14

12,051 downloads per month
Used in guff_css

WTFPL license

63KB
1K SLoC

Argyle

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

This crate contains an agnostic CLI argument parser for Unix platforms called Argue. Unlike more robust libraries like clap, Argue does not hold information about expected or required arguments; it merely parses the raw arguments (std::env::args_os) 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 owned bytes rather than (os)strings, again leaving the choice of later conversion entirely up to the implementor.

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.7.*"

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.
            Err(ArgyleError::WantsVersion) => {
                println!("MyApp v{}", env!("CARGO_PKG_VERSION"));
            },
            // A "-h" or "--help" flag was present.
            Err(ArgyleError::WantsHelp) => {
                println!("Help stuff goes here...");
            },
            // An actual error!
            Err(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");

    ...
}

License

See also: CREDITS.md

Copyright © 2024 Blobfolio, LLC <hello@blobfolio.com>

This work is free. You can redistribute it and/or modify it under the terms of the Do What The Fuck You Want To Public License, Version 2.

DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004

Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>

Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.

DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

0. You just DO WHAT THE FUCK YOU WANT TO.

No runtime deps