arf
no_std, zero-allocation, const-generic command-line argument parser for
embedded and size-constrained binaries.
- No heap, so everything is on the stack.
- Fully safe rust and
no_stdby default. Optionalstdfeature forstd::error::Error. - Capacity is fixed at compile time through const generics.
- Const builders, so entire parsers can be declared in a
constcontext. - Zero dependencies.
- Crate size is about ~30 KiB in release. (based on rosetta-rs/argparse-rosetta-rs)
Install
[]
= "0.1"
Example
use ;
then ./example will produce something like that:
<input>
Capacity model
Capacities are const generics, so the parser carries no heap state:
| Generic | Where | Meaning |
|---|---|---|
A |
Parser<A> / SubCommand<A> |
Max number of arg definitions |
S |
Parser<A, S, SA> |
Max number of subcommands |
SA |
Parser<A, S, SA> |
Max args per subcommand |
P |
parser.parse::<P, _>(...) |
Max positional values captured |
Exceeding A or S at build time panics. Exceeding P at parse time
returns [ErrorKind::TooManyPositionals].
Subcommands
use ;
let parser = new
.arg
.subcmd
.subcmd;
let result = parser.?;
assert!;
if let Some = result.subcommand
# Ok::
Use [Parser::get_subcommand] to look up a subcommand definition by name
when routing help.
Custom value types
Implement [FromArgValue] for any type used with value_of_parsed:
use FromArgValue;
bool and every built-in integer (u8…u128, i8…i128, usize, isize)
are implemented for you.
Errors
Parsing returns [ParseError], a Copy borrow of the offending token.
[ParseError::write_error] prints a CLI-style error: <message> line;
the Display impl emits the message without a prefix so callers can compose.
Examples
examples/basic.rs— minimal flag/option/positional setupexamples/complete.rs— every feature in one file with a custom value typeexamples/subcommands.rs— cargo-like subcommand routing
Run with cargo run --example <name>.
License
MIT