26 releases

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

#72 in Command-line interface

Download history 694/week @ 2023-12-23 1365/week @ 2023-12-30 2170/week @ 2024-01-06 2898/week @ 2024-01-13 3594/week @ 2024-01-20 2987/week @ 2024-01-27 1443/week @ 2024-02-03 3451/week @ 2024-02-10 3764/week @ 2024-02-17 3248/week @ 2024-02-24 3450/week @ 2024-03-02 4140/week @ 2024-03-09 3343/week @ 2024-03-16 3458/week @ 2024-03-23 3444/week @ 2024-03-30 3193/week @ 2024-04-06

14,384 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