#repl #cli #rustyline

bin+lib simple-repl

A very simple base for an interactive repl / interactive CLI

4 releases

0.1.4 Oct 9, 2023
0.1.2 Oct 9, 2023
0.1.1 Oct 8, 2023
0.1.0 Oct 8, 2023

#1765 in Command line utilities

Download history 5/week @ 2023-11-04 7/week @ 2023-11-11 17/week @ 2023-11-18 22/week @ 2023-11-25 15/week @ 2023-12-02 10/week @ 2023-12-09 12/week @ 2023-12-16 17/week @ 2023-12-23 4/week @ 2023-12-30 7/week @ 2024-01-06 8/week @ 2024-01-13 6/week @ 2024-01-20 14/week @ 2024-01-27 4/week @ 2024-02-03 17/week @ 2024-02-10 82/week @ 2024-02-17

117 downloads per month

MIT license

8KB
65 lines

simple-repl

This is a very simple base for interactive repl / CLI applications.

The purpose of this is, primarily, to prevent me from having to rewrite this 'boilerplate' in every small CLI application I have been working on.

This uses the rustyline crate to provide command history, line editing, etc.

Usage:

//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)
        }
    }
}

Try it out! The above example is in main.rs, and can be run on your PC with cargo run!

I do not provide any guarantees, warranties, etc. This is licensed under the MIT license:

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.

Dependencies

~3.5MB
~62K SLoC