Trait CommandHandler

Source
pub trait CommandHandler<W>
where W: Write,
{ // Required method fn execute(&self, output: &mut W, args: &[&str]) -> CommandResult; }
Expand description

Trait for creating new commands

Returning CommandResult::Break instructs the Cmd.run() loop to break.

§Examples

// CommandHandler that prints out help message
use std::io;
use std::io::Write;
use rusty_cmd::command_handler::{CommandHandler, CommandResult};

#[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").unwrap();
        CommandResult::Continue
    }
}

/// CommandHandler that prints out a greeting
#[derive(Default)]
pub struct Greet;

impl<W> CommandHandler<W> for Greet
    where W: std::io::Write {
    fn execute(&self, output: &mut W, args: &[&str]) -> CommandResult {
        let joined_args = args.join(", ");
        match args.len() {
            0 => output.write(format!("Hello, {}!", joined_args).as_bytes()).unwrap(),
            _ => output.write(b"Hello!").unwrap(),
        };
        CommandResult::Continue
    }
}

Required Methods§

Source

fn execute(&self, output: &mut W, args: &[&str]) -> CommandResult

Required method to execute a command

Implementors§

Source§

impl<F, W> CommandHandler<W> for F
where W: Write, F: Fn(&mut W, &[&str]) -> CommandResult,

Blanket CommandHandler implementation for Fn(&mut W, &&str) -> CommandResult allows CommandHandlers to be registered as closures

Source§

impl<W: Write> CommandHandler<W> for Quit