A minimal CLI framework written in Rust
To use seahorse, add this to your Cargo.toml:
toml
[dependencies]
seahorse = "1.0.0"
bash
$ git clone https://github.com/ksk001100/seahorse
$ cd seahorse
$ cargo run --example single_app -- --help
$ cargo run --example multiple_app -- --help
bash
$ cargo new cli
$ cd cli
$ echo 'seahorse = "*"' >> Cargo.toml
```rust use seahorse::{App}; use std::env;
fn main() {
let args: Vec
app.run(args);
} ```
bash
$ cargo build --release
$ ./target/release/cli --help
$ ./target/release/cli John
```rust use seahorse::{App, Context, Command}; use std::env;
fn main() {
let args: Vec
app.run(args);
}
fn default_action(c: &Context) { println!("Hello, {:?}", c.args); }
fn add_action(c: &Context) {
let sum: i32 = c.args.iter().map(|n| n.parse::
fn addcommand() -> Command { Command::new("add") .alias("a") .usage("cli add(a****) [nums...]") .action(addaction) }
fn sub_action(c: &Context) {
let sum: i32 = c.args.iter().map(|n| n.parse::
fn subcommand() -> Command { Command::new("sub") .alias("s") .usage("cli sub(s) [nums...]") .action(subaction) } ```
```bash $ cli John Hello, ["John"]
$ cli add 32 10 43 85
$ cli sub 12 23 89 -124 ```
```rust use seahorse::{App, Command, Context, Flag, FlagType, error::FlagError}; use std::env;
fn main() {
let args: Vec
app.run(args);
}
fn defaultaction(c: &Context) { if c.boolflag("bye") { println!("Bye, {:?}", c.args); } else { println!("Hello, {:?}", c.args); }
if let Ok(age) = c.int_flag("age") {
println!("{:?} is {} years old", c.args, age);
}
}
fn calcaction(c: &Context) {
match c.stringflag("operator") {
Ok(op) => {
let sum: i32 = match &*op {
"add" => c.args.iter().map(|n| n.parse::
println!("{}", sum);
}
Err(e) => match e {
FlagError::Undefiled => panic!("undefined operator..."),
FlagError::ArgumentError => panic!("argument error..."),
FlagError::NotFound => panic!("not found flag..."),
FlagError::ValueTypeError => panic!("value type mismatch..."),
FlagError::TypeError => panic!("flag type mismatch..."),
},
}
}
fn calccommand() -> Command { Command::new("calc") .alias("cl, c") .usage("cli calc(cl, c) [nums...]") .action(calcaction) .flag( Flag::new("operator", FlagType::String) .usage("cli calc [nums...] --operator(-op) [add | sub]") .alias("op"), ) } ```
```bash $ cli John Hello, ["John"]
$ cli John --bye Bye, ["John"]
$ cli John --age 10 Hello, ["John"] ["John"] is 10 years old
$ cli John -b -a=40 Bye, ["John"] ["John"] is 40 years old
$ cli calc --operator add 1 2 3 4 5 15
$ cli calc -op sub 10 6 3 2 -21 ```
Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.
This project is licensed under MIT license
Contribution to the seahorse crate is organized under the terms of the Contributor Covenant, the maintainer of seahorse, @ksk001100, promises to intervene to uphold that code of conduct.