[][src]Struct medusa::Handler

pub struct Handler { /* fields omitted */ }

Struct that defines a CLI option handler in a programmatic way.

Methods

impl Handler[src]

pub fn new() -> Handler[src]

Create a new empty Handler instance.

Example

use medusa::Handler;

// code ...

let mut handler: Handler = Handler::new();

pub fn parse_args(&mut self, args: Vec<String>) -> Result<(), String>[src]

Manually parse the options and arguments passed to the CLI program.

This method meant to be called internally by CommandLine instance, and indeed it is currently being called internally.

Example

use medusa::Handler;

use std::env;

// code ...

let mut handler: Handler = Handler::new();
let result = handler.parse_args(env::args().collect());
println!("result : {:?}", result);

pub fn get_arg(&self, key: &str) -> Option<ArgType>[src]

Get the argument of CLI option as its key.

This function can be called in the inner side of any action handler function definition to get the argument of the parameter option specified. The value return are the ArgType instance. See its documentation for details. See also the stateful example for code snippet.

Example

use medusa::{ArgType, Handler};

// code ...

fn print_twice(handler: &Handler) {
    if let Some(argtype) = handler.get_arg("--echo") {
        if let ArgType::Content(payload) = argtype {
            println!("printed once more : {}", payload);
        }
    }
}

pub fn get_actions(&self) -> Option<&HashMap<String, Variant>>[src]

Get action handler list in form of HashMap from the current Handler instance.

Example

use medusa::{Handler, Variant};

use std::collections::HashMap;

let mut handler: Handler = Handler::new();
// code ...

if let Some(map) = handler.get_actions() {
    println!("Got some actions here!");
} else {
    println!("No actions defined!");
}

pub fn clone_actions(&self) -> Option<HashMap<String, Variant>>[src]

Clone action handler list in form of HashMap from the current Handler instance.

Note that the return value from this method are a HashMap instance wrapped in Option instance, instead of reference to a HashMap instance.

Example

use medusa::{Handler, Variant};

use std::collections::HashMap;

let mut handler: Handler = Handler::new();
// code ...

if let Some(map) = handler.clone_actions() {
    println!("Got some cloned actions here!");
} else {
    println!("No actions defined!");
}

pub fn extract_actions(self) -> Option<HashMap<String, Variant>>[src]

Consume self instance and return the HashMap containing consumed action handlers wrapped with Option instance.

Note that the current instance are consumed, hence it won't be able to be used again unless it was cloned first.

Example

use medusa::{Handler, Variant};

use std::collections::HashMap;

let mut handler: Handler = Handler::new();
// code ...

if let Some(map) = handler.extract_actions() {
    println!("Got some cloned actions here!");
} else {
    println!("No actions defined!");
}

pub fn get_hints(&self) -> Option<&HashMap<String, String>>[src]

Get action hint list in form of HashMap from the current Handler instance.

Example

use medusa::{Handler, Variant};

use std::collections::HashMap;

let mut handler: Handler = Handler::new();
// code ...

if let Some(map) = handler.get_hints() {
    println!("Got some hints here!");
} else {
    println!("No hints defined!");
}

pub fn clone_hints(&self) -> Option<HashMap<String, String>>[src]

Clone action hint list in form of HashMap from the current Handler instance.

Note that the return value from this method are a HashMap instance wrapped in Option instance, instead of reference to a HashMap instance.

Example

use medusa::{Handler, Variant};

use std::collections::HashMap;

let mut handler: Handler = Handler::new();
// code ...

if let Some(map) = handler.clone_hints() {
    println!("Got some cloned hints here!");
} else {
    println!("No hints defined!");
}

pub fn extract_hints(self) -> Option<HashMap<String, String>>[src]

Consume self instance and return the HashMap containing consumed action hints wrapped with Option instance.

Note that the current instance are consumed, hence it won't be able to be used again unless it was cloned first.

Example

use medusa::{Handler, Variant};

use std::collections::HashMap;

let mut handler: Handler = Handler::new();
// code ...

if let Some(map) = handler.extract_hints() {
    println!("Got some cloned hints here!");
} else {
    println!("No hints defined!");
}

pub fn add(&mut self, parameter: String, action: Variant, hint: String)[src]

Add an action for CLI option handler. Note that you must also provide function hint for this option handler. This hint will be shown if arguments passed to this handler are invalid, or when the CLI is calling the --help option. This kind of hint usually a single line of String, but it is not restricted for multi-line String.

Example

use medusa::{Handler, Variant};

// code ...

fn show_help(handler: &Handler) {
    println!("This is help message!");
}

let mut handler: Handler = Handler::new();
handler.add(
    String::from("--help"),
    Variant::Plain(show_help),
    String::from("Show this help message.")
);

pub fn append(&mut self, handler: Handler)[src]

Append Handler instance passed to the calling Handler instance.

Example

use medusa::{Handler, Variant};

// code ...

let mut handler: Handler = Handler::new();
// code ...

let mut other_handler: Handler = Handler::new();
// code ...

// append the first handler to the second
other_handler.append(handler);

Trait Implementations

impl Clone for Handler[src]

Auto Trait Implementations

impl RefUnwindSafe for Handler

impl Send for Handler

impl Sync for Handler

impl Unpin for Handler

impl UnwindSafe for Handler

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.