Thanks to visit codestin.com
Credit goes to lib.rs

9 breaking releases

Uses new Rust 2024

0.10.0 Oct 12, 2025
0.8.0 Sep 29, 2025
0.7.0 May 21, 2025

#583 in Configuration


Used in 3 crates

MIT license

12KB
314 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()

No runtime deps