Rust Fire
Image by T5, source: https://www.pixiv.net/artworks/104959772
Turn a Rust function or module into a command-line application with one attribute.
Installation
One function
/// Welcome a person.
$ app --name "John Smith" --excited
Welcome, John Smith!
#[fire::main] generates the program entry point, so no separate fn main() or
registration call is needed.
Help
-h and --help are generated automatically from the function signature and
documentation comments:
/// Start the HTTP server.
$ app --help
Start the HTTP server.
Usage: app --host <HOST> [--verbose]
Options:
--host <HOST> Address to listen on.
--verbose Enable verbose logging.
-h, --help Print help
For module applications, app --help lists the subcommands and
app <COMMAND> --help describes one subcommand.
Subcommands
Place #[fire::main] on an inline module to turn its functions into subcommands:
$ app hello --name John --times 2
Hello, John!
Hello, John!
$ app bye
Bye!
Rust snake_case function and parameter names are exposed as CLI kebab-case
names.
Parameters
The function signature defines the CLI:
| Rust type | CLI behavior |
|---|---|
T |
required option parsed with FromStr |
Option<T> |
optional option |
bool |
value-less flag, defaulting to false |
&str |
borrowed string option |
Both common option formats are accepted:
$ app --name John
$ app --name=John
A command may return Result. Errors are printed to stderr and the application
exits with status 2.
License
BSD-2-Clause.