Crates.io | rusty-cmd |
lib.rs | rusty-cmd |
version | 2.0.0 |
source | src |
created_at | 2024-05-07 05:10:36.827369 |
updated_at | 2024-06-06 03:19:50.790832 |
description | A crate for creating custom line-oriented command interpreters in Rust. |
homepage | |
repository | https://github.com/miguelalizo/cmd |
max_upload_size | |
id | 1231836 |
size | 23,694 |
A crate for creating custom line-oriented command interpreters in Rust.
I wrote this as a Rust learning project and was inspired by Python's cmd and the dev.to article: A small library for writing line oriented-command interpreters in rust.
rusty-cmd provides two modules:
cmd
: Used for creating the Cmd
struct that contains the CommandHandler
implementations in a HashMap
.command_handler
: Contains the CommandHandler
trait.use std::io;
use std::io::Write;
use rusty_cmd::cmd::Cmd;
use rusty_cmd::command_handler::{CommandHandler, CommandResult};
use rusty_cmd::handlers::Quit;
/// CommandHandler that prints out help message
#[derive(Default)]
pub struct Help;
impl<W> CommandHandler<W> for Help
where
W: std::io::Write,
{
fn execute(&self, output: &mut W, _args: &[&str]) -> CommandResult {
writeln!(output, "Help message").expect("Should be able to write to output");
CommandResult::Continue
}
}
/// CommandHandler that emulates the basic bash touch command to create a new file
#[derive(Default)]
pub struct Touch;
impl<W> CommandHandler<W> for Touch
where
W: std::io::Write,
{
fn execute(&self, output: &mut W, _args: &[&str]) -> CommandResult {
let option_filename = _args.first();
match option_filename {
Some(filename) => {
let fs_result = std::fs::File::create(filename);
match fs_result {
Ok(file) => writeln!(output, "Created file: {:?}", file)
.expect("Should be able to write to output"),
Err(_) => writeln!(output, "Could not create file: {}", filename)
.expect("Should be able to write to output"),
}
}
None => println!("Need to specify a filename"),
}
CommandResult::Continue
}
}
fn main() -> Result<(), std::io::Error> {
let mut cmd = Cmd::new(io::BufReader::new(io::stdin()), io::stdout());
let help = Help;
let hello = Touch;
let quit = Quit::default();
cmd.add_cmd(String::from("help"), help)?;
cmd.add_cmd(String::from("touch"), hello)?;
cmd.add_cmd_fn(String::from("greet"), |output, _args| {
writeln!(output, "hello!").expect("Should be able to write to output");
CommandResult::Continue
})?;
cmd.add_cmd(String::from("quit"), quit)?;
cmd.run()?;
Ok(())
}
To use rusty-cmd in your project, add the following to your Cargo.toml
file:
[dependencies]
rusty-cmd = "2.0.0"
Then import the crate in your Rust code:
use rusty_cmd::command_handler::{CommandHandler, CommandResult};
use rusty_cmd::cmd::Cmd;
We welcome contributions! See our CONTIBUTING guide
Miguel Alizo 🦀✅ |
John Aughey 🦀✅ |
This project is licensed under the MIT License - see the LICENSE file for details.