miniarg
A minimal argument parser, with support for no-std and no-alloc
It mostly supports cmdlines in the form of program -foo value -bar value
.
That means:
- values are strings
- keys start with a single dash
- keys can occur multiple times
The last parameter can also be just a key without a value.
(This can be useful for -help
.)
Usage
Add this to your Cargo.toml
:
[]
= "0.5"
The feature std
is enabled by default and alloc
and derive
are optional.
Examples
A minimal example looks like this:
let cmdline = "executable -key value";
let mut args = parse;
assert_eq!;
assert_eq!;
If you don't want to pass a cmdline, you can use an iterator instead:
let iter = vec!.into_iter;
let mut args = parse_from_iter;
assert_eq!;
assert_eq!;
You can use collect::<Result<Vec<_>, _>>()
to get a Vec
:
let cmdline = "executable -key value";
let args = parse.?;
assert_eq!;
If you compile with std
or alloc
, it also supports passing ToString
instead of strings,
for example your own enum:
let cmdline = "executable -foo value -bar value";
let args = parse
.?;
assert_eq!;
As you can see, the first character of the enum kinds is converted to lowercase.
If you compile with derive
, you can use a custom derive instead:
let cmdline = "executable -foo value -bar value";
let args = parse.?;
assert_eq!;
In this case a help text is generated from the documentation comments on your enum kinds,
help_text()
retrieves it.
The code never panics, but the returned iterator will contain ParseError
s
if anything goes wrong.
You might also want to take a look at the split_args
module for lower level access.
License: MPL-2.0