Crates.io | rustyrepl |
lib.rs | rustyrepl |
version | 0.2.0 |
source | src |
created_at | 2022-08-30 14:52:11.639391 |
updated_at | 2023-01-30 15:03:40.926421 |
description | A Rust read, evaluate, print, loop (REPL) utility |
homepage | |
repository | https://github.com/slawlor/repl |
max_upload_size | |
id | 655179 |
size | 23,929 |
The easy Rust Read-Evaluate-Print-Loop (REPL) utility crate
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.
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