Trait rusty_cmd::command_handler::CommandHandler

source ·
pub trait CommandHandler<W = Stdout>: Debug + AToAny {
    // Required method
    fn execute(&self, _stdout: &mut W, _args: String) -> usize;
}
Expand description

Interface for creating new commands

Defines io::Stdout as the default generic type

§Examples

// CommandHandler that prints out help message
use std::io;
use std::io::Write;
use cmd::command_handler::CommandHandler;

#[derive(Debug, Default)]
pub struct Help;

impl CommandHandler for Help {
    fn execute(&self, _stdout: &mut io::Stdout, _args: String) -> usize {
        writeln!(_stdout, "Help message").unwrap();
        1
    }
}

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

impl<W: io::Write> CommandHandler<W> for Greet {
    fn execute(&self, _stdout: &mut W, _args: String) -> usize {
        match _args.len() {
            0 => _stdout.write(format!("Hello, {}!", _args).as_bytes()).unwrap(),
            _ => _stdout.write(b"Hello!").unwrap(),
        };
        1
    }
}

Required Methods§

source

fn execute(&self, _stdout: &mut W, _args: String) -> usize

Required method to execute a command

Implementors§