31 releases

0.10.1 Nov 28, 2024
0.9.0 Oct 15, 2024
0.7.2 Feb 15, 2024
0.6.8 Jun 1, 2023
0.3.1 Mar 5, 2021

#176 in Command-line interface

Download history 7054/week @ 2024-08-17 5829/week @ 2024-08-24 7295/week @ 2024-08-31 5986/week @ 2024-09-07 3862/week @ 2024-09-14 7010/week @ 2024-09-21 6234/week @ 2024-09-28 7279/week @ 2024-10-05 8625/week @ 2024-10-12 9102/week @ 2024-10-19 7254/week @ 2024-10-26 7625/week @ 2024-11-02 4861/week @ 2024-11-09 7180/week @ 2024-11-16 10475/week @ 2024-11-23 12422/week @ 2024-11-30

36,316 downloads per month

WTFPL license

41KB
518 lines

Argyle

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

This crate provides a simple streaming CLI argument parser/iterator called Argue, offering a middle ground between the standard library's barebones std::env::args_os helper and full-service crates like clap.

Argue performs some basic normalization — it handles string conversion in a non-panicking way, recognizes shorthand value assignments like -kval, -k=val, --key=val, and handles end-of-command (--) arguments — and will help identify any special keys/values expected by your app.

The subsequent validation and handling, however, are left entirely up to you. Loop, match, and proceed however you see fit.

If that sounds terrible, just use clap instead. Haha.

Installation

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

[dependencies]
argyle = "0.9.*"

Example

A general setup might look something like the following.

Refer to the documentation for Argue, KeyWord, and Argument for more information, caveats, etc.

use argyle::{Argument, KeyWord};
use std::path::PathBuf;

#[derive(Debug, Clone, Default)]
/// # Configuration.
struct Settings {
    threads: usize,
    verbose: bool,
    paths: Vec<PathBuf>,
}

let args = argyle::args()
    .with_keywords([
        KeyWord::key("-h").unwrap(),            // Boolean flag (short).
        KeyWord::key("--help").unwrap(),        // Boolean flag (long).
        KeyWord::key_with_value("-j").unwrap(), // Expects a value.
        KeyWord::key_with_value("--threads").unwrap(),
    ]);

// Loop and handle!
let mut settings = Settings::default();
for arg in args {
    match arg {
        // Help flag match.
        Argument::Key("-h" | "--help") => {
            println!("Help Screen Goes Here.");
            return;
        },

        // Thread option match.
        Argument::KeyWithValue("-j" | "--threads", value) => {
            settings.threads = value.parse()
                .expect("Maximum threads must be a number!");
        },

        // Something else.
        Argument::Other(v) => {
            settings.paths.push(PathBuf::from(v));
        },

        // Also something else, but not String-able. PathBuf doesn't care
        // about UTF-8, though, so it might be fine!
        Argument::InvalidUtf8(v) => {
            settings.paths.push(PathBuf::from(v));
        },

        // Nothing else is relevant here.
        _ => {},
    }
}

// Now that you're set up, do stuff…

No runtime deps