#repl #evaluate #print #utility #read #rustyline #parser

rustyrepl

A Rust读取、评估、打印、循环(REPL)实用工具

6个版本

0.2.0 2023年1月30日
0.1.4 2022年8月31日

#477调试

Download history 177/week @ 2024-03-11 202/week @ 2024-03-18 82/week @ 2024-03-25 101/week @ 2024-04-01 94/week @ 2024-04-08 55/week @ 2024-04-15 132/week @ 2024-04-22 424/week @ 2024-04-29 82/week @ 2024-05-06 198/week @ 2024-05-13 284/week @ 2024-05-20 61/week @ 2024-05-27 113/week @ 2024-06-03 62/week @ 2024-06-10 63/week @ 2024-06-17 91/week @ 2024-06-24

335 每月下载

MIT 许可证

24KB
316 代码行

rustyrepl

易于使用的Rust Read-Evaluate-Print-Loop (REPL) 实用工具包

github crates.io docs.rs CI/main

关于

rustyrepl 是一个简单的包,用于在命令行中创建读取、评估、打印、循环(REPL)实用工具。它将 rustylineclap 结合起来,构建一个具有方便的参数解析的简单REPL界面。

目的

  1. 捕获REPL界面的退出/退出操作
  2. 存储和管理REPL历史命令以及这些命令的索引
  3. 允许操作员在任何时候获取帮助菜单,使用Clap支持的完整帮助界面(即子命令帮助)
  4. 处理传入的命令

用法

首先,将 rustyrepl 添加到您的 Cargo.toml 文件中

[dependencies]
rustyrepl = "0.2"

接下来

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
}

这个小型程序将启动一个REPL,其提示符为 ">>",您可以与之交互

>> 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

依赖关系

~6MB
~108K SLoC