Struct seahorse::Command

source ·
pub struct Command {
    pub name: String,
    pub description: Option<String>,
    pub usage: Option<String>,
    pub action: Option<Action>,
    pub action_with_result: Option<ActionWithResult>,
    pub flags: Option<Vec<Flag>>,
    pub alias: Option<Vec<String>>,
    pub commands: Option<Vec<Command>>,
}
Expand description

Application command type

Fields§

§name: String

Command name

§description: Option<String>

Command description

§usage: Option<String>

Command usage

§action: Option<Action>

Command action

§action_with_result: Option<ActionWithResult>

Alternate command action that returns a Result

§flags: Option<Vec<Flag>>

Action flags

§alias: Option<Vec<String>>

Command alias

§commands: Option<Vec<Command>>

Implementations§

source§

impl Command

source

pub fn new<T: Into<String>>(name: T) -> Self

Create new instance of Command

Example

use seahorse::Command;

let command = Command::new("cmd");
Examples found in repository?
examples/multiple_app.rs (line 54)
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
fn hello_command() -> Command {
    Command::new("hello")
        .description("hello command")
        .usage("multiple_app hello(he, h) [name]")
        .alias("h")
        .alias("he")
        .action(hello_action)
        .flag(
            Flag::new("bye", FlagType::Bool)
                .description("bye flag")
                .alias("b"),
        )
        .flag(
            Flag::new("age", FlagType::Int)
                .description("age flag")
                .alias("a")
                .alias("ag"),
        )
}

fn add_action(c: &Context) {
    let sum: i32 = c.args.iter().map(|n| n.parse::<i32>().unwrap()).sum();
    println!("{}", sum);
}

fn add_command() -> Command {
    Command::new("add")
        .description("add command")
        .usage("multiple_app add [num...]")
        .action(add_action)
}
More examples
Hide additional examples
examples/nested_multiple_app.rs (line 54)
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
fn hello_command() -> Command {
    Command::new("hello")
        .description("hello command")
        .usage("multiple_app hello(he, h) [name]")
        .alias("h")
        .alias("he")
        .action(hello_action)
        .flag(
            Flag::new("bye", FlagType::Bool)
                .description("bye flag")
                .alias("b"),
        )
        .flag(
            Flag::new("age", FlagType::Int)
                .description("age flag")
                .alias("a")
                .alias("ag"),
        )
        .command(world_command())
}

fn add_action(c: &Context) {
    let sum: i32 = c.args.iter().map(|n| n.parse::<i32>().unwrap()).sum();
    println!("{}", sum);
}

fn add_command() -> Command {
    Command::new("add")
        .description("add command")
        .usage("multiple_app add [num...]")
        .action(add_action)
}

fn world_command() -> Command {
    Command::new("world")
        .description("hello world command")
        .usage("nested_multiple_app hello(he, h) world(w)")
        .alias("w")
        .action(|_| println!("Hello world"))
}
source

pub fn description<T: Into<String>>(self, description: T) -> Self

Set description of the command

Example

use seahorse::Command;

let command = Command::new("cmd")
    .description("cli sub command");
Examples found in repository?
examples/multiple_app.rs (line 55)
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
fn hello_command() -> Command {
    Command::new("hello")
        .description("hello command")
        .usage("multiple_app hello(he, h) [name]")
        .alias("h")
        .alias("he")
        .action(hello_action)
        .flag(
            Flag::new("bye", FlagType::Bool)
                .description("bye flag")
                .alias("b"),
        )
        .flag(
            Flag::new("age", FlagType::Int)
                .description("age flag")
                .alias("a")
                .alias("ag"),
        )
}

fn add_action(c: &Context) {
    let sum: i32 = c.args.iter().map(|n| n.parse::<i32>().unwrap()).sum();
    println!("{}", sum);
}

fn add_command() -> Command {
    Command::new("add")
        .description("add command")
        .usage("multiple_app add [num...]")
        .action(add_action)
}
More examples
Hide additional examples
examples/nested_multiple_app.rs (line 55)
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
fn hello_command() -> Command {
    Command::new("hello")
        .description("hello command")
        .usage("multiple_app hello(he, h) [name]")
        .alias("h")
        .alias("he")
        .action(hello_action)
        .flag(
            Flag::new("bye", FlagType::Bool)
                .description("bye flag")
                .alias("b"),
        )
        .flag(
            Flag::new("age", FlagType::Int)
                .description("age flag")
                .alias("a")
                .alias("ag"),
        )
        .command(world_command())
}

fn add_action(c: &Context) {
    let sum: i32 = c.args.iter().map(|n| n.parse::<i32>().unwrap()).sum();
    println!("{}", sum);
}

fn add_command() -> Command {
    Command::new("add")
        .description("add command")
        .usage("multiple_app add [num...]")
        .action(add_action)
}

fn world_command() -> Command {
    Command::new("world")
        .description("hello world command")
        .usage("nested_multiple_app hello(he, h) world(w)")
        .alias("w")
        .action(|_| println!("Hello world"))
}
source

pub fn usage<T: Into<String>>(self, usage: T) -> Self

Set usage of the command

Example

use seahorse::Command;

let command = Command::new("cmd")
    .usage("cli cmd [arg]");
Examples found in repository?
examples/multiple_app.rs (line 56)
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
fn hello_command() -> Command {
    Command::new("hello")
        .description("hello command")
        .usage("multiple_app hello(he, h) [name]")
        .alias("h")
        .alias("he")
        .action(hello_action)
        .flag(
            Flag::new("bye", FlagType::Bool)
                .description("bye flag")
                .alias("b"),
        )
        .flag(
            Flag::new("age", FlagType::Int)
                .description("age flag")
                .alias("a")
                .alias("ag"),
        )
}

fn add_action(c: &Context) {
    let sum: i32 = c.args.iter().map(|n| n.parse::<i32>().unwrap()).sum();
    println!("{}", sum);
}

fn add_command() -> Command {
    Command::new("add")
        .description("add command")
        .usage("multiple_app add [num...]")
        .action(add_action)
}
More examples
Hide additional examples
examples/nested_multiple_app.rs (line 56)
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
fn hello_command() -> Command {
    Command::new("hello")
        .description("hello command")
        .usage("multiple_app hello(he, h) [name]")
        .alias("h")
        .alias("he")
        .action(hello_action)
        .flag(
            Flag::new("bye", FlagType::Bool)
                .description("bye flag")
                .alias("b"),
        )
        .flag(
            Flag::new("age", FlagType::Int)
                .description("age flag")
                .alias("a")
                .alias("ag"),
        )
        .command(world_command())
}

fn add_action(c: &Context) {
    let sum: i32 = c.args.iter().map(|n| n.parse::<i32>().unwrap()).sum();
    println!("{}", sum);
}

fn add_command() -> Command {
    Command::new("add")
        .description("add command")
        .usage("multiple_app add [num...]")
        .action(add_action)
}

fn world_command() -> Command {
    Command::new("world")
        .description("hello world command")
        .usage("nested_multiple_app hello(he, h) world(w)")
        .alias("w")
        .action(|_| println!("Hello world"))
}
source

pub fn action(self, action: Action) -> Self

Set action of the command

Example

use seahorse::{Command, Context, Action};

let action: Action = |c: &Context| println!("{:?}", c.args);
let command = Command::new("cmd")
    .action(action);
Panics

You cannot set both action and action_with_result.

use seahorse::{Action, ActionWithResult, Command, Context};

let action_with_result: ActionWithResult = |c: &Context| {println!("{:?}", c.args); Ok(())};
let action: Action = |c: &Context| println!("{:?}", c.args);
let command = Command::new("cmd")
    .action_with_result(action_with_result)
    .action(action);
Examples found in repository?
examples/multiple_app.rs (line 59)
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
fn hello_command() -> Command {
    Command::new("hello")
        .description("hello command")
        .usage("multiple_app hello(he, h) [name]")
        .alias("h")
        .alias("he")
        .action(hello_action)
        .flag(
            Flag::new("bye", FlagType::Bool)
                .description("bye flag")
                .alias("b"),
        )
        .flag(
            Flag::new("age", FlagType::Int)
                .description("age flag")
                .alias("a")
                .alias("ag"),
        )
}

fn add_action(c: &Context) {
    let sum: i32 = c.args.iter().map(|n| n.parse::<i32>().unwrap()).sum();
    println!("{}", sum);
}

fn add_command() -> Command {
    Command::new("add")
        .description("add command")
        .usage("multiple_app add [num...]")
        .action(add_action)
}
More examples
Hide additional examples
examples/nested_multiple_app.rs (line 59)
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
fn hello_command() -> Command {
    Command::new("hello")
        .description("hello command")
        .usage("multiple_app hello(he, h) [name]")
        .alias("h")
        .alias("he")
        .action(hello_action)
        .flag(
            Flag::new("bye", FlagType::Bool)
                .description("bye flag")
                .alias("b"),
        )
        .flag(
            Flag::new("age", FlagType::Int)
                .description("age flag")
                .alias("a")
                .alias("ag"),
        )
        .command(world_command())
}

fn add_action(c: &Context) {
    let sum: i32 = c.args.iter().map(|n| n.parse::<i32>().unwrap()).sum();
    println!("{}", sum);
}

fn add_command() -> Command {
    Command::new("add")
        .description("add command")
        .usage("multiple_app add [num...]")
        .action(add_action)
}

fn world_command() -> Command {
    Command::new("world")
        .description("hello world command")
        .usage("nested_multiple_app hello(he, h) world(w)")
        .alias("w")
        .action(|_| println!("Hello world"))
}
source

pub fn action_with_result(self, action_with_result: ActionWithResult) -> Self

Set action of the command

Example

use seahorse::{ActionWithResult, Command, Context};

let action_with_result: ActionWithResult = |c: &Context| {println!("{:?}", c.args); Ok(())};
let command = Command::new("cmd")
    .action_with_result(action_with_result);
Panics

You cannot set both action and action_with_result.

use seahorse::{Action, ActionWithResult, Command, Context};

let action_with_result: ActionWithResult = |c: &Context| {println!("{:?}", c.args); Ok(())};
let action: Action = |c: &Context| println!("{:?}", c.args);
let command = Command::new("cmd")
    .action(action)
    .action_with_result(action_with_result);
source

pub fn flag(self, flag: Flag) -> Self

Set flag of the command

Example

use seahorse::{Command, Flag, FlagType};

let command = Command::new("cmd")
    .flag(Flag::new("bool", FlagType::Bool))
    .flag(Flag::new("int", FlagType::Int));
Examples found in repository?
examples/multiple_app.rs (lines 60-64)
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
fn hello_command() -> Command {
    Command::new("hello")
        .description("hello command")
        .usage("multiple_app hello(he, h) [name]")
        .alias("h")
        .alias("he")
        .action(hello_action)
        .flag(
            Flag::new("bye", FlagType::Bool)
                .description("bye flag")
                .alias("b"),
        )
        .flag(
            Flag::new("age", FlagType::Int)
                .description("age flag")
                .alias("a")
                .alias("ag"),
        )
}
More examples
Hide additional examples
examples/nested_multiple_app.rs (lines 60-64)
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
fn hello_command() -> Command {
    Command::new("hello")
        .description("hello command")
        .usage("multiple_app hello(he, h) [name]")
        .alias("h")
        .alias("he")
        .action(hello_action)
        .flag(
            Flag::new("bye", FlagType::Bool)
                .description("bye flag")
                .alias("b"),
        )
        .flag(
            Flag::new("age", FlagType::Int)
                .description("age flag")
                .alias("a")
                .alias("ag"),
        )
        .command(world_command())
}
source

pub fn alias<T: Into<String>>(self, name: T) -> Self

Set alias of the command

Example

use seahorse::Command;

let command = Command::new("cmd")
    .alias("c");
Examples found in repository?
examples/multiple_app.rs (line 57)
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
fn hello_command() -> Command {
    Command::new("hello")
        .description("hello command")
        .usage("multiple_app hello(he, h) [name]")
        .alias("h")
        .alias("he")
        .action(hello_action)
        .flag(
            Flag::new("bye", FlagType::Bool)
                .description("bye flag")
                .alias("b"),
        )
        .flag(
            Flag::new("age", FlagType::Int)
                .description("age flag")
                .alias("a")
                .alias("ag"),
        )
}
More examples
Hide additional examples
examples/nested_multiple_app.rs (line 57)
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
fn hello_command() -> Command {
    Command::new("hello")
        .description("hello command")
        .usage("multiple_app hello(he, h) [name]")
        .alias("h")
        .alias("he")
        .action(hello_action)
        .flag(
            Flag::new("bye", FlagType::Bool)
                .description("bye flag")
                .alias("b"),
        )
        .flag(
            Flag::new("age", FlagType::Int)
                .description("age flag")
                .alias("a")
                .alias("ag"),
        )
        .command(world_command())
}

fn add_action(c: &Context) {
    let sum: i32 = c.args.iter().map(|n| n.parse::<i32>().unwrap()).sum();
    println!("{}", sum);
}

fn add_command() -> Command {
    Command::new("add")
        .description("add command")
        .usage("multiple_app add [num...]")
        .action(add_action)
}

fn world_command() -> Command {
    Command::new("world")
        .description("hello world command")
        .usage("nested_multiple_app hello(he, h) world(w)")
        .alias("w")
        .action(|_| println!("Hello world"))
}
source

pub fn command(self, command: Command) -> Self

Set sub command of the command

Example

use seahorse::{App, Command};

let sub_command = Command::new("world")
    .usage("cli hello world")
    .action(|_| println!("Hello world!"));

let command = Command::new("hello")
    .usage("cli hello [arg]")
    .action(|c| println!("{:?}", c.args))
    .command(sub_command);

let app = App::new("cli")
    .command(command);
Panics

You cannot set a command named as same as registered ones.

use seahorse::{App, Command};

let sub_command1 = Command::new("world")
    .usage("cli hello world")
    .action(|_| println!("Hello world!"));

let sub_command2 = Command::new("world")
    .usage("cli hello world")
    .action(|_| println!("Hello world!"));

let command = Command::new("hello")
    .usage("cli hello [arg]")
    .action(|c| println!("{:?}", c.args))
    .command(sub_command1)
    .command(sub_command2);

let app = App::new("cli")
    .command(command);
Examples found in repository?
examples/nested_multiple_app.rs (line 71)
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
fn hello_command() -> Command {
    Command::new("hello")
        .description("hello command")
        .usage("multiple_app hello(he, h) [name]")
        .alias("h")
        .alias("he")
        .action(hello_action)
        .flag(
            Flag::new("bye", FlagType::Bool)
                .description("bye flag")
                .alias("b"),
        )
        .flag(
            Flag::new("age", FlagType::Int)
                .description("age flag")
                .alias("a")
                .alias("ag"),
        )
        .command(world_command())
}
source

pub fn run_with_result(&self, args: Vec<String>) -> ActionResult

Run command Call this function only from App

Trait Implementations§

source§

impl Default for Command

source§

fn default() -> Command

Returns the “default value” for a type. Read more

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.