bind-args

Crates.iobind-args
lib.rsbind-args
version0.6.0
sourcesrc
created_at2023-06-04 01:18:24.559175
updated_at2024-10-02 20:51:37.958661
descriptionA command-line argument parser
homepage
repositoryhttps://github.com/eze-works/bind-args
max_upload_size
id881935
size21,692
Eze Anyanwu (eze-works)

documentation

README

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);
}
Commit count: 9

cargo fmt