27 releases (6 breaking)

✓ Uses Rust 2018 edition

new 0.7.1 Mar 25, 2020
0.7.0 Mar 4, 2020
0.6.2 Feb 27, 2020
0.5.3 Feb 24, 2020
0.1.1 Dec 9, 2019

#37 in Command-line interface

Download history 113/week @ 2019-12-09 10/week @ 2019-12-16 27/week @ 2019-12-23 7/week @ 2019-12-30 18/week @ 2020-01-06 16/week @ 2020-01-13 50/week @ 2020-01-20 3/week @ 2020-01-27 8/week @ 2020-02-03 54/week @ 2020-02-10 350/week @ 2020-02-17 229/week @ 2020-02-24 58/week @ 2020-03-02 82/week @ 2020-03-09 96/week @ 2020-03-16

373 downloads per month
Used in 4 crates

Custom license

45KB
844 lines

seahorse

crates.io releases count issues count forks count license github actions CI

Logo

A minimal CLI framework written in Rust

Features

  • Easy to use
  • No dependencies
  • Typed flags(Bool, String, Int, Float)

Documentation

Here

Usage

To use seahorse, add this to your Cargo.toml:

[dependencies]
seahorse = "0.7.1"

Example

Run example

$ git clone https://github.com/ksk001100/seahorse
$ cd seahorse
$ cargo run --example single_app -- --help
$ cargo run --example multiple_app -- --help

Quick Start

$ cargo new cli
$ cd cli
$ echo 'seahorse = "*"' >> Cargo.toml
use seahorse::{App};
use std::env;

fn main() {
    let args: Vec<String> = env::args().collect();
        let app = App::new()
            .name(env!("CARGO_PKG_NAME"))
            .author(env!("CARGO_PKG_AUTHORS"))
            .version(env!("CARGO_PKG_VERSION"))
            .usage("cli [args]")
            .action(|c| println("Hello, {:?}", c.args));
    
        app.run(args);
}
$ cargo build --release
$ ./target/release/cli --help
$ ./target/release/cli John

Multiple command application

use seahorse::{App, Context, Command, Flag, FlagType};
use std::env;

fn main() {
    let args: Vec<String> = env::args().collect();
    let app = App::new()
        .name(env!("CARGO_PKG_NAME"))
        .author(env!("CARGO_PKG_AUTHORS"))
        .version(env!("CARGO_PKG_VERSION"))
        .usage("cli [name]")
        .action(default_action)
        .command(add_command())
        .command(sub_command());

    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::<i32>().unwrap()).sum();
    println!("{}", sum);
}

fn add_command() -> Command {
    Command::new()
        .name("add")
        .usage("cli add [nums...]")
        .action(add_action)
}

fn sub_action(c: &Context) {
    let sum: i32 = c.args.iter().map(|n| n.parse::<i32>().unwrap() * -1).sum();
    println!("{}", sum);
}

fn sub_command() -> Command {
    Command::new()
        .name("sub")
        .usage("cli sub [nums...]")
        .action(sub_action)
}
$ cli John
Hello, ["John"]

$ cli add 32 10 43
85

$ cli sub 12 23 89
-124

Branch processing by flag

use seahorse::{App, Command, Context, Flag, FlagType};
use std::env;

fn main() {
    let args: Vec<String> = env::args().collect();
    let app = App::new()
        .name(env!("CARGO_PKG_NAME"))
        .author(env!("CARGO_PKG_AUTHORS"))
        .version(env!("CARGO_PKG_VERSION"))
        .usage("cli [name]")
        .action(default_action)
        .flag(Flag::new("bye", "cli [name] --bye(-b)", FlagType::Bool).alias("b"))
        .flag(Flag::new("age", "cli [name] --age(-a)", FlagType::Int).alias("a"))
        .command(calc_command());

    app.run(args);
}

fn default_action(c: &Context) {
    if c.bool_flag("bye") {
        println!("Bye, {:?}", c.args);
    } else {
        println!("Hello, {:?}", c.args);
    }

    if let Some(age) = c.int_flag("age") {
        println!("{:?} is {} years old", c.args, age);
    }
}

fn calc_action(c: &Context) {
    match c.string_flag("operator") {
        Some(op) => {
            let sum: i32 = match &*op {
                "add" => c.args.iter().map(|n| n.parse::<i32>().unwrap()).sum(),
                "sub" => c.args.iter().map(|n| n.parse::<i32>().unwrap() * -1).sum(),
                _ => panic!("undefined operator..."),
            };

            println!("{}", sum);
        }
        None => panic!(),
    }
}

fn calc_command() -> Command {
    Command::new()
        .name("calc")
        .usage("cli calc [nums...]")
        .action(calc_action)
        .flag(
            Flag::new(
                "operator",
                "cli calc [nums...] --operator(-op) [add | sub]",
                FlagType::String,
            )
            .alias("op"),
        )
}
$ 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

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.

No runtime deps