rust_args_parser
Tiny, fast, callback-based CLI argument parser for Rust. No macros. No derive. No global state.
⚙️ Zero
unsafe, 📦std-only. 🧵 Minimal allocations on hot paths. 🎨 Optional colorized help that respectsNO_COLOR.
This crate is a small, pragmatic alternative to heavy frameworks when you want:
- Simple, callback-driven option handling (
fn(Option<&str>, &mut U) -> Result<()>) - Subcommands with aliases (nested
CmdSpec) - Short clusters (
-abc,-j10,-j 10) and long options--name[=value] - Optional/required values with numeric look-ahead (so
-1/-.5/1e3aren't mistaken for options) - Exclusive / required groups (
at_most_one(group),at_least_one(group)) - Environment / default application (
.env("NAME"),.default("value")) - Positionals schema with ranges (
PosSpec::new("FILE").range(1, 10)) - Built-ins:
-h/--help,-V/--version,-A/--authorwhen enabled inEnv
It's inspired by the author's C library c-args-parser, re-imagined in safe Rust.
Table of contents
- Quick start
- Examples
- Subcommands
- Groups (mutually-exclusive / required one)
- Environment & defaults
- Positionals
- Behavior details
- Color & wrapping
- Errors
- Performance & safety
- License
Quick start
Minimal skeleton:
use rust_args_parser as ap;
Run it:
$ demo -h
$ demo --n=3 -vv file1 file2
$ demo --limit 10 file
See
examples/basic.rsin this repo for a full, runnable version.
Examples
Run any example with:
| Example | Teaches | Try |
|---|---|---|
| 01_minimal | one flag + one positional | cargo run --example 01_minimal -- -v file.txt |
| 02_flags_values | clusters, required & optional values | cargo run --example 02_flags_values -- -vv -n10 --limit=5 ./path |
| 03_optional_numbers | optional numeric look-ahead | cargo run --example 03_optional_numbers -- -t -0.25 |
| 04_groups | XOR / REQ_ONE groups | cargo run --example 04_groups -- --mode-a |
| 05_subcommands | subcommands & aliases | cargo run --example 05_subcommands -- remote add https://example |
| 06_env_defaults_and_help | env/defaults & built-ins | THREADS=8 cargo run --example 06_env_defaults_and_help -- -o out.txt |
The sources live under examples/.
Subcommands
Compose subcommands by nesting CmdSpec:
let remote_add = new
.pos;
let remote = new
.aliases // optional
.subs;
let root = new
.subs
.pos;
dispatch descends through bare tokens until it resolves the final command, then parses options/positionals at that depth.
Groups (mutually-exclusive / required one)
Use group ids to express constraints across options:
let root = new.opts;
at_most_one(g) enforces XOR (≤ 1 present in group g), at_least_one(g) enforces REQ_ONE (≥ 1 present in group g). Defaults and env-applied options count toward these rules.
Environment & defaults
Attach environment variables and string defaults to options. Both are applied after parsing and before group/positional checks:
new
.metavar.numeric
.env // uses value from env if present
.default; // otherwise falls back to this
Positionals
Describe positionals per command with names, descriptions and cardinality:
new.one; // exactly one
new.range; // 2..=5
new.desc;
If no positional schema is declared for a command, any leftover bare token becomes an error.
Behavior details
- Option forms
- Long:
--name,--name=value,--name value - Short clusters:
-abc,-j10,-j 10(no-j=10)
- Long:
- Optional values pick up the next token if it looks like a value. Numeric hints allow
-1,-.5,1e9to be consumed as values. --stops option parsing; remaining tokens are positionals.- Built-ins (when configured in
Env):-h|--help,-V|--version,-A|--authorprint tostdoutand returnErr(Error::Exit(0))so you canprocess::exit(0)cleanly.
Color & wrapping
Help text is rendered with optional ANSI color. Use Env::auto_color() to disable when NO_COLOR is set, or Env::color(false) to force plain output. Set wrap_cols to wrap long descriptions.
Errors
dispatch returns Result<()>. Common handling:
match dispatch
All errors implement Display and std::error::Error.
Performance & safety
- Zero
unsafe(#![forbid(unsafe_code)]): the parser is implemented entirely in safe Rust. - No global state; no proc-macros; no derives.
- Minimal allocations: small count vector for options, optional small buffers for help rendering.
- Fast paths for ASCII short clusters and long option parsing.
For heavy usage, see
benches/(Criterion®) and measure on your workload. The library aims to avoid surprises and keep hot paths branch-lean.
License
Dual-licensed under MIT or Apache-2.0 at your option.
SPDX-License-Identifier: MIT OR Apache-2.0
If you contribute, you agree to license your contributions under the same terms.