2025-11-02 19:49:15 +11:00
|
|
|
# jaarg argument parser library #
|
2025-11-05 21:00:11 +11:00
|
|
|
|
|
|
|
|
Dependency-free, const (mostly), no magic macros, `no_std` & no alloc (though nicer with those).
|
|
|
|
|
Some say it can parse your arguments.
|
2025-11-02 21:53:37 +11:00
|
|
|
|
|
|
|
|
### Obligatory fancy banners ###
|
|
|
|
|
<div>
|
|
|
|
|
<a href="https://crates.io/crates/jaarg">
|
|
|
|
|
<img src="https://img.shields.io/crates/v/jaarg.svg?logo=rust&style=for-the-badge" alt="Crates version" />
|
|
|
|
|
</a>
|
2025-11-15 19:12:17 +11:00
|
|
|
<a href="#licensing">
|
|
|
|
|
<img src="https://img.shields.io/badge/license-MIT%20%7C%20Apache--2.0-green.svg?style=for-the-badge" alt="MIT OR Apache-2.0 License" />
|
2025-11-02 21:53:37 +11:00
|
|
|
</a>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
### Example usage ###
|
|
|
|
|
```rust
|
|
|
|
|
// Variables for arguments to fill
|
|
|
|
|
let mut file = PathBuf::new();
|
|
|
|
|
let mut out: Option<PathBuf> = None;
|
|
|
|
|
let mut number = 0;
|
|
|
|
|
|
|
|
|
|
// Set up arguments table
|
|
|
|
|
enum Arg { Help, Number, File, Out }
|
|
|
|
|
const OPTIONS: Opts<Arg> = Opts::new(&[
|
|
|
|
|
Opt::help_flag(Arg::Help, &["-h", "--help"]).help_text("Show this help and exit."),
|
2025-11-02 22:55:00 +11:00
|
|
|
Opt::value(Arg::Number, &["-n", "--number"], "value")
|
|
|
|
|
.help_text("Optionally specify a number (default: 0)"),
|
|
|
|
|
Opt::positional(Arg::File, "file").required()
|
|
|
|
|
.help_text("Input file."),
|
|
|
|
|
Opt::positional(Arg::Out, "out")
|
|
|
|
|
.help_text("Output destination (optional).")
|
2025-11-02 21:53:37 +11:00
|
|
|
]).with_description("My simple utility.");
|
|
|
|
|
|
2025-11-02 22:55:00 +11:00
|
|
|
// Parse command-line arguments from `std::env::args()`
|
2025-11-02 21:53:37 +11:00
|
|
|
match OPTIONS.parse_easy(|program_name, id, _opt, _name, arg| {
|
|
|
|
|
match id {
|
|
|
|
|
Arg::Help => {
|
|
|
|
|
OPTIONS.print_full_help(program_name);
|
|
|
|
|
return Ok(ParseControl::Quit);
|
|
|
|
|
}
|
|
|
|
|
Arg::Number => { number = str::parse(arg)?; }
|
|
|
|
|
Arg::File => { file = arg.into(); }
|
|
|
|
|
Arg::Out => { out = Some(arg.into()); }
|
|
|
|
|
}
|
|
|
|
|
Ok(ParseControl::Continue)
|
|
|
|
|
}) {
|
|
|
|
|
ParseResult::ContinueSuccess => (),
|
|
|
|
|
ParseResult::ExitSuccess => std::process::exit(0),
|
|
|
|
|
ParseResult::ExitError => std::process::exit(1),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Print the result variables
|
|
|
|
|
println!("{file:?} -> {out:?} (number: {number:?})",
|
|
|
|
|
out = out.unwrap_or(file.with_extension("out")));
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Changelog ###
|
|
|
|
|
|
2025-11-16 13:06:15 +11:00
|
|
|
main:
|
2025-11-26 15:12:49 +11:00
|
|
|
* Fixed coerced `ArgumentError` not being rewritten for positional arguments.
|
2025-11-16 13:06:15 +11:00
|
|
|
* Moved top level includes to `pub use`.
|
2025-11-26 15:26:07 +11:00
|
|
|
* Hopefully work around licence & read me texts not being included in crate.
|
2025-11-15 19:27:26 +11:00
|
|
|
|
2025-11-15 19:41:52 +11:00
|
|
|
v0.2.1:
|
|
|
|
|
* Fixed licence field in `Cargo.toml`.
|
|
|
|
|
|
2025-11-15 19:27:26 +11:00
|
|
|
v0.2.0:
|
2025-11-15 19:12:17 +11:00
|
|
|
* Change licence from `MIT` to `MIT OR Apache-2.0`.
|
2025-11-05 21:00:11 +11:00
|
|
|
* Moved `Opts::parse_map` into newly introduced `alloc` crate, making it accessible for `no_std` users.
|
2025-11-15 16:50:30 +11:00
|
|
|
* More generic & flexible help API: removed forced newline, moved error writer to `StandardErrorUsageWriter`,
|
|
|
|
|
generalised "Usage" line in standard full writer, enough public constructs to roll a custom help writer.
|
|
|
|
|
* Added the ability to exclude options from short usage, full help, or both.
|
2025-11-05 21:24:01 +11:00
|
|
|
* More tests for validating internal behaviour & enabled CI on GitHub.
|
2025-11-15 16:50:30 +11:00
|
|
|
* Added new `no_std` examples.
|
2025-11-04 01:27:41 +11:00
|
|
|
|
|
|
|
|
v0.1.1:
|
2025-11-02 22:10:17 +11:00
|
|
|
* Fixed incorrect error message format for coerced parsing errors.
|
2025-11-04 01:27:41 +11:00
|
|
|
* Cleaned up docstring formatting.
|
2025-11-02 22:55:00 +11:00
|
|
|
* Added basic example.
|
2025-11-02 21:53:37 +11:00
|
|
|
|
|
|
|
|
v0.1.0:
|
|
|
|
|
* Initial release.
|
|
|
|
|
|
2025-11-05 21:00:11 +11:00
|
|
|
### Roadmap ###
|
|
|
|
|
|
|
|
|
|
Near future:
|
|
|
|
|
* More control over parsing behaviour (getopt style, no special casing shorts for Windows style flags, etc.)
|
|
|
|
|
* More practical examples.
|
|
|
|
|
|
|
|
|
|
Long term:
|
|
|
|
|
* Strategy for handling exclusive argument groups.
|
|
|
|
|
* Make use of const traits when they land to improve table setup.
|
|
|
|
|
|
2025-11-02 21:53:37 +11:00
|
|
|
### Projects using jaarg (very cool) ###
|
2025-11-16 13:06:15 +11:00
|
|
|
* [Sprout bootloader](https://github.com/edera-dev/sprout)
|
2025-11-02 21:53:37 +11:00
|
|
|
* [lbminfo](https://github.com/ScrelliCopter/colourcyclinginthehousetonight/tree/main/lbminfo)
|
2025-11-15 19:12:17 +11:00
|
|
|
|
|
|
|
|
### Licensing ###
|
|
|
|
|
|
|
|
|
|
jaarg is dual-licensed under either the [MIT](LICENSE.MIT) or [Apache 2.0](LICENSE.Apache-2.0) licences.
|
|
|
|
|
Pick whichever works best for your project.
|