6 releases (breaking)

0.6.0 Oct 2, 2024
0.5.0 Oct 2, 2024
0.4.0 Oct 1, 2024
0.3.0 Jun 27, 2024
0.1.0 Jun 4, 2023

#679 in Command-line interface

37 downloads per month

MIT license

19KB
323 lines

bind_args

A simple command-line argument parser.

Example

use bind_args::parse;

struct AppArgs {
    verbose: bool,
    log_level: Option<String>,
    path: String,
}

let mut bag = parse(["program", "--log-level", "INFO", "--verbose", "/etc/config"]).unwrap();

let args = AppArgs {
    verbose: bag.remove_flag("verbose"),
    log_level: bag.remove_option("log-level"),
    path: bag.remove_operand().unwrap_or(String::from("/"))
};

assert_eq!(args.verbose, true);
assert_eq!(args.log_level.as_deref(), Some("INFO"));
assert_eq!(args.path, "/etc/config");

// It is important to make sure there are not unexpected arguments.
if !bag.is_empty() {
    let unexpected = bag.remove_remaining().join(", ");
    eprintln!("Unexpected argument(s): {}", unexpected);
    std::process::exit(1);
}

lib.rs:

Parsing command line arguments

The crate revolves around the ArgumentBag data structure from which options, flags and operands may be extracted.

You get an instance of the bag by callind parse or parse_env.

No runtime deps