rusterm

Crates.iorusterm
lib.rsrusterm
version0.2.2
sourcesrc
created_at2021-08-11 08:40:03.28654
updated_at2021-08-28 06:48:37.915066
descriptionLibrary providing a simple, quick command line interpreter for Rust projects.
homepage
repositoryhttps://github.com/tduck973564/rusterm
max_upload_size
id434687
size53,740
Thomas (tduck973564)

documentation

https://docs.rs/rusterm/

README

Rusterm

docs dependency status build status

A fast and easy console library.

This provides a basic, minimal framework for making a basic command interpreter over applications.

Syntax

<command> [stringargument] ["string argument with spaces"] [33] [4.8]

Note that 33 and 4.8 are two different types, Integer and Float.

Examples

use rusterm::prelude::*;

fn main() {
    let mut command_table: HashMap<String, Command>= HashMap::new();
    command_table.insert("add".to_string(), add);
    let console = Console::new(command_table, ">> ");
    console.run_repl();
}

fn add(mut args: Arguments) -> Result<(), RustermError> {
    let mut sum = 0;
    for _ in 0..args.len() {
        let arg: i32 = args.pop_arg()?.try_into()?;
        sum += arg;
    }
    println!("{}", sum);
    Ok(())
}

Writing functions that work as commands

Every function that you write to be used in the Console must follow this signature: fn(mut args: brc::lex::Arguments) -> Result<(), brc::error::Error>. To use user-inputted arguments in your function, you must continually pop the command arguments from the args parameter, and then convert them into the type you expect. Example below:

use rusterm::prelude::*;

fn echo(mut args: Arguments) -> Result<(), RustermError> {
    let first_argument: String = args.pop_arg()?.try_into()?; // Expects a String, as per the type annotation. You can expect a f64, i32 or String.
    println!("{}", first_argument);
    Ok(())
}
Commit count: 0

cargo fmt