Struct seahorse::Context

source ·
pub struct Context {
    pub args: Vec<String>,
    /* private fields */
}
Expand description

Context type

This type is used only for Action arguments

Fields§

§args: Vec<String>

Vec<String> with flags and flag values ​​removed from command line arguments

Implementations§

source§

impl Context

source

pub fn new( args: Vec<String>, flags: Option<Vec<Flag>>, help_text: String ) -> Self

Create new instance of Context Parse processing using Vec<String> command line argument and Vec<Flag> as arguments

source

pub fn bool_flag(&self, name: &str) -> bool

Get bool flag

Example

use seahorse::Context;

fn action(c: &Context) {
    if c.bool_flag("bool") {
        println!("True!");
    } else {
        println!("False!");
    }
}
Examples found in repository?
examples/single_app.rs (line 24)
23
24
25
26
27
28
29
fn action(c: &Context) {
    if c.bool_flag("bye") {
        println!("Bye, {:?}", c.args);
    } else {
        println!("Hello, {:?}", c.args);
    }
}
More examples
Hide additional examples
examples/multiple_app.rs (line 11)
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
fn main() {
    let args: Vec<String> = env::args().collect();
    let app = App::new("multiple_app")
        .author(env!("CARGO_PKG_AUTHORS"))
        .description(env!("CARGO_PKG_DESCRIPTION"))
        .usage("multiple_app [command] [arg]")
        .version(env!("CARGO_PKG_VERSION"))
        .action(|c: &Context| println!("{:?} : {}", c.args, c.bool_flag("bool")))
        .flag(
            Flag::new("bool", FlagType::Bool)
                .description("bool flag")
                .alias("b"),
        )
        .command(add_command())
        .command(hello_command());

    app.run(args);
}

fn hello_action(c: &Context) {
    if c.bool_flag("bye") {
        println!("Bye, {:?}", c.args);
    } else {
        println!("Hello, {:?}", c.args);
    }

    match c.int_flag("age") {
        Ok(age) => println!("{:?} is {} years old", c.args, age),
        Err(e) => match e {
            FlagError::TypeError => println!("age flag type error"),
            FlagError::ValueTypeError => println!("value type error"),
            FlagError::Undefined => println!("undefined age flag"),
            FlagError::ArgumentError => println!("age flag argument error"),
            FlagError::NotFound => println!("not found age flag"),
        },
    }

    match c.string_flag("neko") {
        Ok(neko) => println!("neko say {}", neko),
        Err(e) => match e {
            FlagError::TypeError => println!("neko flag type error"),
            FlagError::ValueTypeError => println!("value type error"),
            FlagError::Undefined => println!("undefined neko flag"),
            FlagError::ArgumentError => println!("neko flag argument error"),
            FlagError::NotFound => println!("not found neko flag"),
        },
    }
}
examples/nested_multiple_app.rs (line 11)
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
fn main() {
    let args: Vec<String> = env::args().collect();
    let app = App::new("multiple_app")
        .author(env!("CARGO_PKG_AUTHORS"))
        .description(env!("CARGO_PKG_DESCRIPTION"))
        .usage("multiple_app [command] [arg]")
        .version(env!("CARGO_PKG_VERSION"))
        .action(|c: &Context| println!("{:?} : {}", c.args, c.bool_flag("bool")))
        .flag(
            Flag::new("bool", FlagType::Bool)
                .description("bool flag")
                .alias("b"),
        )
        .command(add_command())
        .command(hello_command());

    app.run(args);
}

fn hello_action(c: &Context) {
    if c.bool_flag("bye") {
        println!("Bye, {:?}", c.args);
    } else {
        println!("Hello, {:?}", c.args);
    }

    match c.int_flag("age") {
        Ok(age) => println!("{:?} is {} years old", c.args, age),
        Err(e) => match e {
            FlagError::TypeError => println!("age flag type error"),
            FlagError::ValueTypeError => println!("value type error"),
            FlagError::Undefined => println!("undefined age flag"),
            FlagError::ArgumentError => println!("age flag argument error"),
            FlagError::NotFound => println!("not found age flag"),
        },
    }

    match c.string_flag("neko") {
        Ok(neko) => println!("neko say {}", neko),
        Err(e) => match e {
            FlagError::TypeError => println!("neko flag type error"),
            FlagError::ValueTypeError => println!("value type error"),
            FlagError::Undefined => println!("undefined neko flag"),
            FlagError::ArgumentError => println!("neko flag argument error"),
            FlagError::NotFound => println!("not found neko flag"),
        },
    }
}
examples/top_level_error_handling.rs (line 12)
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
fn main() {
    let args: Vec<String> = env::args().collect();
    let app = App::new("cli")
        .author(env!("CARGO_PKG_AUTHORS"))
        .description(env!("CARGO_PKG_DESCRIPTION"))
        .usage("multiple_app [command] [arg]")
        .version(env!("CARGO_PKG_VERSION"))
        .action_with_result(|c: &Context| {
            if c.bool_flag("error") {
                Err(ActionError {
                    message: "ERROR...".to_string(),
                })
            } else {
                Ok(())
            }
        })
        .flag(
            Flag::new("error", FlagType::Bool)
                .description("error flag")
                .alias("e"),
        );

    match app.run_with_result(args) {
        Ok(_) => println!("OK"),
        Err(e) => match e {
            ActionError { message } => println!("{}", message),
        },
    };
}
source

pub fn string_flag(&self, name: &str) -> Result<String, FlagError>

Get string flag

Example

use seahorse::Context;

fn action(c: &Context) {
    match c.string_flag("string") {
        Ok(s) => println!("{}", s),
        Err(e) => println!("{}", e)
    }
}
Examples found in repository?
examples/multiple_app.rs (line 41)
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
fn hello_action(c: &Context) {
    if c.bool_flag("bye") {
        println!("Bye, {:?}", c.args);
    } else {
        println!("Hello, {:?}", c.args);
    }

    match c.int_flag("age") {
        Ok(age) => println!("{:?} is {} years old", c.args, age),
        Err(e) => match e {
            FlagError::TypeError => println!("age flag type error"),
            FlagError::ValueTypeError => println!("value type error"),
            FlagError::Undefined => println!("undefined age flag"),
            FlagError::ArgumentError => println!("age flag argument error"),
            FlagError::NotFound => println!("not found age flag"),
        },
    }

    match c.string_flag("neko") {
        Ok(neko) => println!("neko say {}", neko),
        Err(e) => match e {
            FlagError::TypeError => println!("neko flag type error"),
            FlagError::ValueTypeError => println!("value type error"),
            FlagError::Undefined => println!("undefined neko flag"),
            FlagError::ArgumentError => println!("neko flag argument error"),
            FlagError::NotFound => println!("not found neko flag"),
        },
    }
}
More examples
Hide additional examples
examples/nested_multiple_app.rs (line 41)
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
fn hello_action(c: &Context) {
    if c.bool_flag("bye") {
        println!("Bye, {:?}", c.args);
    } else {
        println!("Hello, {:?}", c.args);
    }

    match c.int_flag("age") {
        Ok(age) => println!("{:?} is {} years old", c.args, age),
        Err(e) => match e {
            FlagError::TypeError => println!("age flag type error"),
            FlagError::ValueTypeError => println!("value type error"),
            FlagError::Undefined => println!("undefined age flag"),
            FlagError::ArgumentError => println!("age flag argument error"),
            FlagError::NotFound => println!("not found age flag"),
        },
    }

    match c.string_flag("neko") {
        Ok(neko) => println!("neko say {}", neko),
        Err(e) => match e {
            FlagError::TypeError => println!("neko flag type error"),
            FlagError::ValueTypeError => println!("value type error"),
            FlagError::Undefined => println!("undefined neko flag"),
            FlagError::ArgumentError => println!("neko flag argument error"),
            FlagError::NotFound => println!("not found neko flag"),
        },
    }
}
source

pub fn int_flag(&self, name: &str) -> Result<isize, FlagError>

Get int flag

Example

use seahorse::Context;

fn action(c: &Context) {
    match c.int_flag("int") {
        Ok(i) => println!("{}", i),
        Err(e) => println!("{}", e)
    }
}
Examples found in repository?
examples/multiple_app.rs (line 30)
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
fn hello_action(c: &Context) {
    if c.bool_flag("bye") {
        println!("Bye, {:?}", c.args);
    } else {
        println!("Hello, {:?}", c.args);
    }

    match c.int_flag("age") {
        Ok(age) => println!("{:?} is {} years old", c.args, age),
        Err(e) => match e {
            FlagError::TypeError => println!("age flag type error"),
            FlagError::ValueTypeError => println!("value type error"),
            FlagError::Undefined => println!("undefined age flag"),
            FlagError::ArgumentError => println!("age flag argument error"),
            FlagError::NotFound => println!("not found age flag"),
        },
    }

    match c.string_flag("neko") {
        Ok(neko) => println!("neko say {}", neko),
        Err(e) => match e {
            FlagError::TypeError => println!("neko flag type error"),
            FlagError::ValueTypeError => println!("value type error"),
            FlagError::Undefined => println!("undefined neko flag"),
            FlagError::ArgumentError => println!("neko flag argument error"),
            FlagError::NotFound => println!("not found neko flag"),
        },
    }
}
More examples
Hide additional examples
examples/nested_multiple_app.rs (line 30)
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
fn hello_action(c: &Context) {
    if c.bool_flag("bye") {
        println!("Bye, {:?}", c.args);
    } else {
        println!("Hello, {:?}", c.args);
    }

    match c.int_flag("age") {
        Ok(age) => println!("{:?} is {} years old", c.args, age),
        Err(e) => match e {
            FlagError::TypeError => println!("age flag type error"),
            FlagError::ValueTypeError => println!("value type error"),
            FlagError::Undefined => println!("undefined age flag"),
            FlagError::ArgumentError => println!("age flag argument error"),
            FlagError::NotFound => println!("not found age flag"),
        },
    }

    match c.string_flag("neko") {
        Ok(neko) => println!("neko say {}", neko),
        Err(e) => match e {
            FlagError::TypeError => println!("neko flag type error"),
            FlagError::ValueTypeError => println!("value type error"),
            FlagError::Undefined => println!("undefined neko flag"),
            FlagError::ArgumentError => println!("neko flag argument error"),
            FlagError::NotFound => println!("not found neko flag"),
        },
    }
}
source

pub fn uint_flag(&self, name: &str) -> Result<usize, FlagError>

Get Uint flag

Example

use seahorse::Context;

fn action(c: &Context) {
    match c.uint_flag("uint") {
        Ok(i) => println!("{}", i),
        Err(e) => println!("{}", e)
    }
}
source

pub fn float_flag(&self, name: &str) -> Result<f64, FlagError>

Get float flag

Example

use seahorse::Context;

fn action(c: &Context) {
    match c.float_flag("float") {
        Ok(f) => println!("{}", f),
        Err(e) => println!("{}", e)
    }
}
source

pub fn help(&self)

Display help

Example

use seahorse::Context;

fn action(c: &Context) {
    c.help();
}

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

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

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.