Crates.io | bind-args |
lib.rs | bind-args |
version | 0.6.0 |
source | src |
created_at | 2023-06-04 01:18:24.559175 |
updated_at | 2024-10-02 20:51:37.958661 |
description | A command-line argument parser |
homepage | |
repository | https://github.com/eze-works/bind-args |
max_upload_size | |
id | 881935 |
size | 21,692 |
A simple command-line argument parser.
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);
}