seahorse
A minimal CLI framework written in Rust
Features
- Easy to use
- No dependencies
Documentation
Here
Usage
To use seahorse, add this to your Cargo.toml:
[dependencies]
seahorse = "0.6.1"
Example
$ git clone https://github.com/ksk001100/seahorse
$ cd seahorse
$ cargo run --example single_app
$ cargo run --example multiple_app
Multiple action application
use seahorse::{color, App, Command, Context, Flag, FlagType};
use std::env;
fn main() {
let args: Vec<String> = env::args().collect();
let app = App::new()
.name("multiple_app")
.author(env!("CARGO_PKG_AUTHORS"))
.description(env!("CARGO_PKG_DESCRIPTION"))
.usage("multiple_app [command] [arg]")
.version(env!("CARGO_PKG_VERSION"))
.command(hello_command());
app.run(args);
}
fn hello_action(c: &Context) {
let name = &c.args[0];
if c.bool_flag("bye") {
println!("Bye, {}", name);
} else {
println!("Hello, {}", name);
}
match c.int_flag("age") {
Some(age) => println!("{} is {} years old", name, age),
None => println!("I don't know {}'s age", name),
}
}
fn hello_command() -> Command {
Command::new()
.name("hello")
.usage("multiple_app hello [name]")
.action(hello_action)
.flag(Flag::new("bye", "multiple_app hello [name] --bye", FlagType::Bool).alias("b"))
.flag(
Flag::new(
"age",
"multiple_app hello [name] --age [age]",
FlagType::Int,
)
.alias("a"),
)
}
$ cargo run
$ cargo run John --bye
$ cargo run John --age 30
$ cargo run John -b -a 30
$ cargo run John --age=30
Single action application
use seahorse::{color, App, Context, Flag, FlagType};
use std::env;
fn main() {
let args: Vec<String> = env::args().collect();
let app = App::new()
.name("single_app")
.author(env!("CARGO_PKG_AUTHORS"))
.description(env!("CARGO_PKG_DESCRIPTION"))
.usage("single_app [args]")
.version(env!("CARGO_PKG_VERSION"))
.action(action)
.flag(
Flag::new("bye", "single_app args --bye", FlagType::Bool)
.alias("b")
.alias("by"),
);
app.run(args);
}
fn action(c: &Context) {
let name = &c.args[0];
if c.bool_flag("bye") {
println!("Bye, {:?}", name);
} else {
println!("Hello, {:?}", name);
}
}
$ cargo run
$ cargo run Bob
$ cargo run Bob --bye
$ cargo run Bob -b
Contributing
Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.
License
This project is licensed under MIT license
Code of Conduct
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.