#shell #cli #interactive #terminal


A library to run custom interactive shells

10 releases (6 breaking)

0.7.0 Nov 13, 2022
0.6.0 Sep 25, 2021
0.5.1 Jul 25, 2021
0.5.0 Jun 19, 2021
0.1.1 Jun 7, 2021

#167 in Command-line interface

Download history 18/week @ 2022-10-07 10/week @ 2022-10-14 18/week @ 2022-10-21 24/week @ 2022-10-28 26/week @ 2022-11-04 39/week @ 2022-11-11 22/week @ 2022-11-18 22/week @ 2022-11-25 10/week @ 2022-12-02 20/week @ 2022-12-09 22/week @ 2022-12-16 13/week @ 2022-12-23 14/week @ 2022-12-30 13/week @ 2023-01-06 10/week @ 2023-01-13 27/week @ 2023-01-20

68 downloads per month
Used in nbc_cli


967 lines


Shellfish is a library to include interactive shells within a program. This may be useful when building terminal application where a persistent state is needed, so a basic cli is not enough; but a full tui is over the scope of the project. Shellfish provides a middle way, allowing interactive command editing whilst saving a state that all commands are given access to.

The shell

By default the shell contains only 3 built-in commands:

  • help - displays help information.
  • quit - quits the shell.
  • exit - exits the shell.

The last two are identical, only the names differ.

When a command is added by the user (see bellow) the help is automatically generated and displayed. Keep in mind this help should be kept rather short, and any additional help should be through a dedicated help option.


The following features are available:

  • rustyline, for better input.
  • app, for command line argument parsing.
  • async, for async. This can be coupled with tokio or async_std
  • clap, for integration with the clap library.


The following code creates a basic shell, with the added commands:

  • greet, greets the user.
  • echo, echoes the input.
  • count, increments a counter.
  • cat, it is cat.

Also, if run with arguments than the shell is run non-interactvely.

use shellfish::{app, Command, Shell};
use std::convert::TryInto;
use std::error::Error;
use std::fmt;
use std::ops::AddAssign;
use async_std::prelude::*;
use std::pin::Pin;

extern crate async_std;

extern crate shellfish;

async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Define a shell
    let mut shell = Shell::new_async(0_u64, "<[Shellfish Example]>-$ ");

    // Add some commands
        Command::new("greets you.".to_string(), greet),

        Command::new("prints the input.".to_string(), echo),

        Command::new("increments a counter.".to_string(), count),

            "Displays a plaintext file.".to_string(),
            async_fn!(u64, cat)

    // Check if we have > 2 args, if so no need for interactive shell
    let mut args = std::env::args();
    if args.nth(1).is_some() {
        // Create the app from the shell.
        let mut app: app::App<u64, app::DefaultAsyncCLIHandler> =

        // Set the binary name
        app.handler.proj_name = Some("shellfish-example".to_string());

        // Run it
    } else {
        // Run the shell

/// Greets the user
fn greet(_state: &mut u64, args: Vec<String>) -> Result<(), Box<dyn Error>> {
    let arg = args.get(1).ok_or_else(|| Box::new(GreetingError))?;
    println!("Greetings {}, my good friend.", arg);

/// Echos the input
fn echo(_state: &mut u64, args: Vec<String>) -> Result<(), Box<dyn Error>> {
    let mut args = args.iter();
    for arg in args {
        print!("{} ", arg);

/// Acts as a counter
fn count(state: &mut u64, _args: Vec<String>) -> Result<(), Box<dyn Error>> {
    println!("You have used this counter {} times", state);

/// Asynchronously reads a file
async fn cat(_state: &mut u64, args: Vec<String>) -> Result<(), Box<dyn Error>> {
    use async_std::fs;

    if let Some(file) = args.get(1) {
        let mut contents = String::new();
        let mut file = fs::File::open(file).await?;
        file.read_to_string(&mut contents).await?;
        println!("{}", contents);

/// Greeting error
pub struct GreetingError;

impl fmt::Display for GreetingError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "No name specified")

impl Error for GreetingError {}

Clap support

clap allows for much cleaner and easier handling of command line arguments, as can be seen below:

/// Simple command to greet a person
/// This command will greet the person based of a multitide
/// of option flags, see below.
#[derive(Parser, Debug)]
#[clap(author, version, about)]
struct Args {
    /// Name of the person to greet
    name: String,

    /// Age of the person to greet
    #[clap(short, long)]
    age: Option<u8>,

    /// Whether to be formal or note
    #[clap(short, long)]
    formal: bool,


fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Define a shell
    let mut shell = Shell::new((), "<[Shellfish CLAP Example]>-$ ");
    shell.commands.insert("greet", clap_command!((), Args, greet));


fn greet(state: &mut (), args: Args) -> Result<(), Box<dyn std::error::Error>> {
    //-- snip --//

For larger projects it is recommended to use clap to cut down on boiler-plait.


~152K SLoC