7 releases (breaking)
Uses new Rust 2024
new 0.7.0 | May 21, 2025 |
---|---|
0.6.0 | May 21, 2025 |
0.5.0 | May 20, 2025 |
0.4.0 | May 19, 2025 |
0.1.0 | Apr 29, 2025 |
#7 in #short
432 downloads per month
Used in llmctx
11KB
281 lines
optz
A no-magic option parser for Rust.
optz
provides a simple, flexible, and minimalistic way to parse
command-line options in Rust applications. It focuses on clarity
and control, avoiding "magic" behavior in favor of explicit
configuration.
Usage
Basic Example
use optz::{Opt, Optz};
let optz = Optz::new("myapp")
.option(
Opt::flag("verbose")
.description("Enable verbose mode")
.short("-v")
)
.option(
Opt::arg("num-items")
.description("Number of items to process")
.default_value("5")
)
.parse()
.unwrap();
if optz.get("verbose").unwrap().unwrap() {
println!("Verbose mode enabled");
}
let count: u32 = optz.get("num-items").unwrap().unwrap();
println!("Processing {} items", count);
Example with Configuration
#[derive(Debug, PartialEq)]
struct MyConfig {
value: i32,
}
let config = MyConfig { value: 42 };
let optz = Optz::new("myapp")
.config(config)
.option(
Opt::flag("verbose")
.description("Enable verbose output")
.short("-v")
)
.parse()
.unwrap();
let retrieved: &MyConfig = optz.get_config().unwrap();
assert_eq!(*retrieved, MyConfig { value: 42 });
Example with Handlers
fn my_handler(_optz: &Optz) -> Result<(), OptzError> {
println!("Custom handler called!");
Ok(())
}
let optz = Optz::from_args("test", vec!["test", "--custom"])
.option(
Opt::flag("custom")
.description("Trigger custom logic")
.handler(my_handler)
)
.parse()
.unwrap();
TODO
- Check types during parsing instead of at
get()