seahorse

Next, introduce the framework ksk001100/seahorse.

This framework is for writing a cli (command line interface).

A cli is simply the same command as which or curl that you have been executing. We will now create our own command.

It may sound difficult to some, but it is easy if you use a wonderful framework called seahorse.

First you install seahorse, but to install the library in rust, write the package name in Cargo.toml. This will automatically install the library when you build.

Note that library is sometimes abbreviated as lib.

Cargo.toml

[package]
name = "rust"
version = "0.1.0"
edition = "2021"

[dependencies]
seahorse = "*"

Then we write the body code that uses seahorse.

src/main.rs

use seahorse::{App, Context};
use std::env;

fn main() {
    let args: Vec<String> = env::args().collect();
    let app = App::new(env!("CARGO_PKG_NAME"))
        .action(s)

        ;
    app.run(args);
}

fn s(_c: &Context) {
    println!("Hello, world!");
}

The contents are very simple. When the command is executed, Hello, world! is output.

$ cargo build
$ ./target/debug/rust
Hello, world!

What is different now, for example, is that the help option is automatic.

$ ./target/debug/rust -h

Name:
        rust
Flags:
        -h, --help : Show help

To help readers understand the awesomeness of seahorse, ask them to think about the application themselves.

src/main.rs

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

fn main() {
    let args: Vec<String> = env::args().collect();
    let app = App::new(env!("CARGO_PKG_NAME"))
        .action(s)

        .command(
                Command::new("yes")
                .alias("y")
                .action(y),
                )
        .command(
                Command::new("no")
                .alias("n")
                .action(n),
                )

        ;
        app.run(args);
}

fn s(_c: &Context) {
    println!("Hello, world!");
}

fn y(_c: &Context) {
    println!("yes");
}

fn n(_c: &Context) {
    println!("no");
}

Try writing this and executing the command you created.

$ ./target/debug/rust

$ ./target/debug/rust y

$ ./target/debug/rust n

The differences and key points of the code are as follows

  use seahorse::{App, Context
+ , Command
  };

+ 
.command(
        Command::new("yes")
        .alias("y")
        .action(y),
        )

fn y(_c: &Context) {
    println!("yes");
}

Feel free to rewrite or add these values to create your own commands.

Here, the value specified in Command::new means the option name.

In this case, rust yes is the issue of this command.

You can specify omission by alias("y"). In this case, rust y.

The action(y) specifies the function fn y, the contents of which will be executed. The processing of the command body is written in action.

By the way, action does not necessarily have to be a function.

For example, try adding the following code in place. The command is rust t or rust t foo.

src/main.rs

.command(
        Command::new("test")
        .alias("t")
        .action(|c| println!("Hello, {:?}", c.args)),
        )
$ ./target/debug/rust t bluesky
Hello, ["bluesky"]

cli

  • CLI

The term cli has many meanings. It can refer to the cli tool as described above, or it can refer to terminal operations in general.

cui and gui

  • CUI, GUI

There are two kinds of cui and gui. The one we are using now is cui.

The term cli is used in almost the same way.

cui means terminal operation, and gui means operation on graphical os.

It is divided into c-ui and ui, which is just ui. ui stands for user interface.

All common os such as windows and mac are based on gui operation.

author

The author of seahorse is ksk.

Thanks for creating a great framework.

results matching ""

    No results matching ""