#repl #cli-applications #interactive #rustyline #boilerplate #history #editing

bin+lib simple-repl

一个非常简单的交互式 repl / 交互式 CLI 基础

4 个版本

0.1.4 2023年10月9日
0.1.2 2023年10月9日
0.1.1 2023年10月8日
0.1.0 2023年10月8日

#10#rustyline

MIT 许可证

8KB
65

simple-repl

这是一个非常简单的交互式 repl / CLI 应用程序的基

其主要目的是为了防止我不得不在每个我正在工作的小型 CLI 应用程序中重新编写这个 '样板代码'。

此代码使用 rustyline 库来提供命令历史记录、行编辑等。

用法

//use the library
use simple_repl::{repl, EvalResult, ReplResult, ReplError};

fn main() {
    println!("REPL Test:");
    println!();

    //pass control to the repl loop, with the desired evaluator
    let res = repl(eval);

    //check the results from the repl for a passthrough value or error
    if let Ok(ReplResult::Passthrough(value)) = res {
        println!("REPL Returned: {value}");
    }
    if let Err(ReplError::EvaluatorError(value)) = res {
        println!("{value:?}");
    }
}

//define our error types, you can also just use a String if you don't want to define a type
#[derive(Debug)]
enum EvalError{
    IdkMan,
    Message(String),
}


//the evaluator is passed a single "line" to be evaluated
//this example shows a few different evaluation choices (i.e. an interactive CLI!)

//Any input that is not "quit", "pass1", "error1", or "error2" will be echo'd back to the user

//pass1 shows an example of passing a value back through to the caller of the repl
//error1 and error2 show different possible errors being returned to the caller of the repl

//evaluator must return Result<EvalResult<T>, EvalError<E>> and accept a &str

//if `Err(E)` or `EvalResult::ExitRepl` is returned, the repl loop ends and control is returned
//to the caller

//if EvalResult::Passthrough is returned, the type T must be provided

//if EvalResult::Continue is passed, control is returned to the repl loop for additional user input
fn eval(input: &str) -> Result<EvalResult<isize>, EvalError> {
    match input {
        "quit" => Ok(EvalResult::ExitRepl),
        "pass1" => Ok(EvalResult::Passthrough(1)),
        "error1" => Err(EvalError::Message("Error".to_string())),
        "error2" => Err(EvalError::IdkMan),
        x => {
            println!("{x}");
            println!();
            Ok(EvalResult::Continue)
        }
    }
}

试试看!上面的示例在 main.rs 中,可以在您的电脑上使用 cargo run 运行!

我不提供任何保证、保修等。此代码基于 MIT 许可证

Copyright © 2023 Ything LLC 

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the “Software”), to deal in the Software without restriction, including without limitation the 
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO 
THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 

IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
OR OTHER DEALINGS IN THE SOFTWARE.

依赖关系

~3.5MB
~63K SLoC