Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/slawlor/repl
Rust Read-Evaluate-Print-Loop utility crate
https://github.com/slawlor/repl
Last synced: about 1 month ago
JSON representation
Rust Read-Evaluate-Print-Loop utility crate
- Host: GitHub
- URL: https://github.com/slawlor/repl
- Owner: slawlor
- License: mit
- Created: 2022-08-29T15:39:14.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2023-01-30T15:02:12.000Z (almost 2 years ago)
- Last Synced: 2024-09-14T09:12:06.413Z (2 months ago)
- Language: Rust
- Size: 31.3 KB
- Stars: 11
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING
- License: LICENSE
Awesome Lists containing this project
README
# rustyrepl
The easy Rust Read-Evaluate-Print-Loop (REPL) utility crate
[](https://github.com/slawlor/repl)
[](https://crates.io/crates/rustyrepl)
[](https://docs.rs/rustyrepl)
[![CI/main](https://github.com/slawlor/repl/actions/workflows/ci.yaml/badge.svg?branch=main)](https://github.com/slawlor/repl/actions/workflows/ci.yaml)## 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
```toml
[dependencies]
rustyrepl = "0.2"
```Next:
```rust
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 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> = Box::new(CliProcessor {});
let mut repl = Repl::::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
```text
>> help
The general CLI, essentially a wrapper for the sub-commands [Commands]Usage: repl-interface
Commands:
test Execute a test command
help Print this message or the help of the given subcommand(s)Options:
-h, --help Print help```