Coder Social home page Coder Social logo

repl's Introduction

rustyrepl

The easy Rust Read-Evaluate-Print-Loop (REPL) utility crate

github crates.io docs.rs CI/main

About

rustyrepl is a simple crate to facilitate creation of Read, Evaluate, Print, Loop utilities at the command-line. A unique combination of rustyline and clap to build a simple REPL interface with handy argument parsing.

Purpose

  1. Capturing exits/quits of the REPL interface
  2. Storing and managing REPL history commands as well as an index of said commands for you
  3. Allowing operators to get a help menu at any point, using the full Clap supported help interface (i.e. sub-command help as well)
  4. Processing the commands as incoming

Usage

First, add rustyrepl to your Cargo.toml file

[dependencies]
rustyrepl = "0.2"

Next:

use anyhow::Result;
use clap::{Parser, Subcommand};
use rustyrepl::{Repl, ReplCommandProcessor};
/// The enum of sub-commands supported by the CLI
#[derive(Subcommand, Clone, Debug)]
pub enum Command {
    /// Execute a test command
    Test,
}
/// The general CLI, essentially a wrapper for the sub-commands [Commands]
#[derive(Parser, Clone, Debug)]
pub struct Cli {
    #[clap(subcommand)]
    command: Command,
}
#[derive(Debug)]
pub struct CliProcessor {}
#[async_trait::async_trait]
impl ReplCommandProcessor<Cli> for CliProcessor {
    fn is_quit(&self, command: &str) -> bool {
        matches!(command, "quit" | "exit")
    }
    async fn process_command(&self, command: Cli) -> Result<()> {
        match command.command {
            Command::Test => println!("A wild test appeared!"),
        }
        Ok(())
    }
}
// MAIN //
#[tokio::main]
async fn main() -> Result<()> {
    let processor: Box<dyn ReplCommandProcessor<Cli>> = Box::new(CliProcessor {});
    let mut repl = Repl::<Cli>::new(processor, None, Some(">>".to_string()))?;
    repl.process().await
}

This small program will startup up a REPL with the prompt ">>" which you can interact with

>> help
The general CLI, essentially a wrapper for the sub-commands [Commands]

Usage: repl-interface <COMMAND>

Commands:
  test  Execute a test command
  help  Print this message or the help of the given subcommand(s)

Options:
  -h, --help  Print help

repl's People

Contributors

slawlor avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.