dynarg
A simple dynamic argument system
Have you ever wanted to have multiple functions with the same signature, but with very different purposes and behavior?
- Maybe you want to make an image editing app, with lots of tools.
- Maybe you want to make a Rust GCODE implementation.
- Maybe you just want to make a modular shell program and dynamically pick arguments out like fruits off a berry bush, while keeping track of which ones haven't been used.
Regardless, you probably want an API that can:
- Match arguments to static strings, to avoid runtime overhead, while maintaining readability.
- Potentially handle dynamic strings if needed.
- Handle arbitrary argument types.
- Provide convenience functions for working with arguments -- e.g., wrapper functions for common types.
If any of this applies to you, this is a library to consider. Note that for very high-performance applications, it might be better to roll your own custom use case with a Vec (ideally recycled!) of enum, to avoid dynamic dispatch.
This API is at this point considered stable and reasonably mature. It is forbid_unsafe, and written in pure Rust.
Basic example
use Args;
Tracking which argument is used
use Args;
Less basic example
use *;
/// Where normally you'd need to have a fixed set of arguments,
/// each of which would be roughly fixed types
/// -- you can dynamically push arguments on the fly instead.
/// This is useful when you need a consistent function signature
/// for different types of functions,
/// each needing different arguments
/// A custom struct as an example
;
PRs welcome :)
Todo
- Custom type handling
- Replace
Options withResults such that it's possible to identify whether the argument name didn't exist, or the type was wrong - Add
snafu - Add convenience functions (e.g.
get_string(),get_int()) - Properly document gotchas
- Add variant without
used()functionality. - Add more examples
- Benchmarks