getargs-derive
A Clap-style #[derive] command-line argument parser for no_std and embedded Rust.
Describe your CLI as a struct, and getargs-derive generates a zero-allocation parser on top of
getargs, with compile-time help, subcommands, helpful errors and shell completions.
If you know Clap's derive API, you already know this one.
Why getargs-derive?
no_stdand no allocation by default. The parser only needscore: parsed values and errors borrow the arguments, and repeated values fit in fixed-capacity collections. It runs the same on a microcontroller, in a kernel or in a regular binary.- Help computed at compile time.
--help,--versionand the help of every subcommand are&'static strconstants built by the macro, so no help-formatting code ends up in your binary. - Clap-familiar attributes.
#[arg(short, long, default_value_t = ...)], doc comments as help text,#[derive(ValueEnum)],#[derive(Subcommand)],#[command(flatten)],env,conflicts_with,requires, argument groups, aliases,Countflags and more, with Clap's help layout and error wording. - Helpful errors. Clap-style messages with "did you mean" suggestions for misspelled options and subcommands, and compile-time diagnostics for invalid attributes instead of runtime panics.
- Shell completions and man pages. With the
stdfeature, generate bash, zsh and fish completion scripts and a roff man page from the same struct. - Small dependency tree. At run time, only
getargs, which has no dependencies itself, plusheaplessif you enable it. - Non-UTF-8 arguments. Parse raw
&[u8]arguments anywhere, andOsString/PathBuffields withstd.
Installation
or in Cargo.toml:
[]
= "0.1"
= "0.5"
No feature is enabled by default:
| Feature | Enables |
|---|---|
| (none) | Everything not listed below, in no_std without an allocator. Repeated values are stored in Values<T, N>. |
heapless |
heapless::Vec<T, N> fields for repeated values, stored inline without allocation. |
alloc |
Vec<T> fields for repeated values. |
std |
Environment variable fallbacks from the process environment, shell completions and man pages, OsStr/Path/PathBuf fields and from_args_os. Implies alloc. |
For a regular command-line application, enable std:
Quick Start
use ;
/// Convert configuration files between formats.
confconv --help prints the help generated at compile time, the Cli::HELP constant:
Convert configuration files between formats
Usage: confconv [OPTIONS] <INPUT>
Arguments:
<INPUT> File to convert
Options:
-f, --format <FORMAT> Output format [default: toml] [possible values: json, yaml, toml]
-o, --output <OUTPUT> Write the result to this file instead of standard output
-v, --verbose... Print more details, repeat for even more
-h, --help Print help
-V, --version Print version
and a typo gets a suggestion:
$ confconv config.json --frmat yaml
error: unexpected argument '--frmat' found
tip: a similar argument exists: '--format'
The parser never prints or exits by itself: from_args takes any iterator of &str and returns the struct or an
Error, so the same code works in tests, in no_std firmware reading arguments from a serial console, or behind parse
when you already have a getargs::Options. With the std feature, Cli::from_args_os() reads std::env::args_os() directly
and accepts non-UTF-8 arguments.
use GetArgs;
let cli = from_args.unwrap;
assert_eq!;
assert!;
assert_eq!;
Compared to Clap
getargs-derive implements a Clap-inspired subset of Clap's derive API for programs where Clap does not fit, such as no_std targets.
It is not a drop-in replacement: Clap Compatibility lists what is shared and what is not supported.
| getargs-derive | Clap | |
|---|---|---|
no_std, no allocator |
yes | no, requires std |
| Help text | &'static str built at compile time |
built at run time |
| Derive API, doc comments as help | yes | yes |
| Builder API | no | yes |
Subcommands, flatten, ValueEnum |
yes | yes |
Defaults, env fallbacks, aliases, Count flags |
yes | yes |
| Relations | conflicts_with, requires, required_unless_present, groups |
many more variants |
| Value validation | parser functions, integer ranges, possible_values |
value_parser! and custom parsers |
| "Did you mean" suggestions | yes | yes |
| Non-UTF-8 arguments | yes | yes |
| Shell completions | bash, zsh, fish (std) |
more shells, dynamic completion |
| Man pages | yes (std) |
with clap_mangen |
| Colored output, terminal-width wrapping | no | yes |
| Prints help and exits | no, your code decides | yes, by default |
Unsupported Clap attributes are rejected at compile time rather than silently ignored.
Guide
- Attribute Reference:
#[arg(...)]and#[command(...)]attributes, argument resolution and supported field types. - Flag Actions:
SetTrue,SetFalseandCountflags. - Argument Aliases: hidden and visible long and short aliases.
- Value Enums:
#[derive(ValueEnum)]and#[arg(value_enum)]for arguments accepting a fixed set of values. - Default Values:
default_valueanddefault_value_t. - Environment Variables:
envfallbacks,parse_with_envand thestdfeature. - Positional and Optional Arguments: positional arguments and
Option<T>fields. - Repeated Values: repeated options,
value_delimiter, positionals collecting the remaining arguments withnum_args, and theValues,heapless::VecandVecstorage options. - Argument Relations:
conflicts_with,requires,required_unless_presentand argument groups. - Value Validation:
value_parserfunctions and ranges, andpossible_values. - Parsing Modes: values starting with
-, negative numbers andlastpositionals, and the limits imposed bygetargs. - Flattened Structs:
#[command(flatten)]to reuse the arguments of another struct. - Subcommands:
#[derive(Subcommand)],#[command(subcommand)]fields, nested subcommands andglobalarguments. - Generated Help: the
HELPandLONG_HELPconstants,-h/--help,disable_help_flag, and help customization (hidden arguments, headings, ordering, extra text and wrapping). - Version Flag:
#[command(version)],-V/--versionand theVERSION/VERSION_TEXTconstants. - Error Handling: the
Errorenum, its Clap-style messages and "did you mean" suggestions. - Shell Completions and Man Pages: the
Commandmetadata trait, and bash, zsh and fish completion scripts and man pages with thestdfeature. - Non-UTF-8 Arguments:
from_bytesandparse_bytesfor raw&[u8]arguments,&[u8],&OsStrandPathBuffields, andfrom_args_oswith thestdfeature. - Clap Compatibility: what is shared with Clap, and what is not supported.
no_stdSupport: allocation-free parsing and the crate features.
Every guide page lists the compile-time diagnostics related to its topic.
Minimum Supported Rust Version
getargs-derive requires Rust 1.88 or newer, and is tested against it in CI.
License
Licensed under the MIT License.
Developed by Alix ANNERAUD.