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
impl Command
sourcepub fn new<T: Into<String>>(name: T) -> Self
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
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"))
}
sourcepub fn description<T: Into<String>>(self, description: T) -> Self
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
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"))
}
sourcepub fn usage<T: Into<String>>(self, usage: T) -> Self
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
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"))
}
sourcepub fn action(self, action: Action) -> Self
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
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"))
}
sourcepub fn action_with_result(self, action_with_result: ActionWithResult) -> Self
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);
sourcepub fn flag(self, flag: Flag) -> Self
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
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())
}
sourcepub fn alias<T: Into<String>>(self, name: T) -> Self
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
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"))
}
sourcepub fn command(self, command: Command) -> Self
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())
}
sourcepub fn run_with_result(&self, args: Vec<String>) -> ActionResult
pub fn run_with_result(&self, args: Vec<String>) -> ActionResult
Run command
Call this function only from App
Trait Implementations§
Auto Trait Implementations§
impl RefUnwindSafe for Command
impl Send for Command
impl Sync for Command
impl Unpin for Command
impl UnwindSafe for Command
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more