| Crates.io | fire |
| lib.rs | fire |
| version | 0.4.0 |
| created_at | 2018-02-26 05:55:20.000122+00 |
| updated_at | 2023-12-18 14:55:17.41332+00 |
| description | fire implementation for rust |
| homepage | |
| repository | https://github.com/aisk/rust-fire |
| max_upload_size | |
| id | 52893 |
| size | 15,824 |

Turn your function(s) to a command line app. Inspires by Google's Python Fire.
cargo add fire
// Turn a single function to CLI app.
#[fire::fire]
fn welcome() {
println!("Welcome!");
}
// Turn mutilple functions to CLI app.
#[fire::fire]
mod some_functions {
pub fn hello(name: String, times: i32) {
for _ in 0..times {
println!("Hello, {name}!");
}
}
pub fn bye() {
println!("Bye!");
}
}
fn main() {
// 'Fire' the functions with command line arguments.
fire::run!();
}
Now you can run your CLI app. By default the single function welcome should be called:
$ cargo build
$ ./target/debug/app
Welcome!
The funtions in the mod should be called as sub-command with it's function name:
$ cargo build
$ ./target/debug/app bye
Bye!
Funtions with arguments will receive the arguments from CLI app arguments, with format like --argname=argvalue:
$ cargo build
$ ./target/debug/app hello --name='John Smith' --times=3
Hello, John Smith!
Hello, John Smith!
Hello, John Smith!
Fire will call .parse() on every argument (except &str), so all types which implements FromStr plus &str is supported. For optional argument, you can use the Option generic type, like Option<String> or Option<i32>;
Licensed under the BSD License.