pub struct App {
pub name: String,
pub author: Option<String>,
pub description: Option<String>,
pub usage: Option<String>,
pub version: Option<String>,
pub commands: Option<Vec<Command>>,
pub action: Option<Action>,
pub action_with_result: Option<ActionWithResult>,
pub flags: Option<Vec<Flag>>,
}
Expand description
Multiple action application entry point
Fields§
§name: String
Application name
Application author
description: Option<String>
Application description
usage: Option<String>
Application usage
version: Option<String>
Application version
commands: Option<Vec<Command>>
Application commands
action: Option<Action>
Application action
action_with_result: Option<ActionWithResult>
Alternate application action that returns a Result
flags: Option<Vec<Flag>>
Application flags
Implementations§
Source§impl App
impl App
Sourcepub fn new<T: Into<String>>(name: T) -> Self
pub fn new<T: Into<String>>(name: T) -> Self
Create new instance of App
Example
use seahorse::App;
let app = App::new("cli");
Examples found in repository?
examples/single_app.rs (line 8)
4fn main() {
5 let args: Vec<String> = env::args().collect();
6 let name = "single_app";
7
8 let app = App::new(name)
9 .author(env!("CARGO_PKG_AUTHORS"))
10 .description(env!("CARGO_PKG_DESCRIPTION"))
11 .usage("single_app [args]")
12 .version(env!("CARGO_PKG_VERSION"))
13 .action(action)
14 .flag(
15 Flag::new("bye", FlagType::Bool)
16 .description("single_app args --bye(-b)")
17 .alias("b"),
18 );
19
20 app.run(args);
21}
More examples
examples/multiple_app.rs (line 6)
4fn main() {
5 let args: Vec<String> = env::args().collect();
6 let app = App::new("multiple_app")
7 .author(env!("CARGO_PKG_AUTHORS"))
8 .description(env!("CARGO_PKG_DESCRIPTION"))
9 .usage("multiple_app [command] [arg]")
10 .version(env!("CARGO_PKG_VERSION"))
11 .action(|c: &Context| println!("{:?} : {}", c.args, c.bool_flag("bool")))
12 .flag(
13 Flag::new("bool", FlagType::Bool)
14 .description("bool flag")
15 .alias("b"),
16 )
17 .command(add_command())
18 .command(hello_command());
19
20 app.run(args);
21}
examples/nested_multiple_app.rs (line 6)
4fn main() {
5 let args: Vec<String> = env::args().collect();
6 let app = App::new("multiple_app")
7 .author(env!("CARGO_PKG_AUTHORS"))
8 .description(env!("CARGO_PKG_DESCRIPTION"))
9 .usage("multiple_app [command] [arg]")
10 .version(env!("CARGO_PKG_VERSION"))
11 .action(|c: &Context| println!("{:?} : {}", c.args, c.bool_flag("bool")))
12 .flag(
13 Flag::new("bool", FlagType::Bool)
14 .description("bool flag")
15 .alias("b"),
16 )
17 .command(add_command())
18 .command(hello_command());
19
20 app.run(args);
21}
examples/top_level_error_handling.rs (line 6)
4fn main() {
5 let args: Vec<String> = env::args().collect();
6 let app = App::new("cli")
7 .author(env!("CARGO_PKG_AUTHORS"))
8 .description(env!("CARGO_PKG_DESCRIPTION"))
9 .usage("multiple_app [command] [arg]")
10 .version(env!("CARGO_PKG_VERSION"))
11 .action_with_result(|c: &Context| {
12 if c.bool_flag("error") {
13 Err(ActionError {
14 message: "ERROR...".to_string(),
15 })
16 } else {
17 Ok(())
18 }
19 })
20 .flag(
21 Flag::new("error", FlagType::Bool)
22 .description("error flag")
23 .alias("e"),
24 );
25
26 match app.run_with_result(args) {
27 Ok(_) => println!("OK"),
28 Err(e) => match e {
29 ActionError { message } => println!("{}", message),
30 },
31 };
32}
Set author of the app
Example
use seahorse::App;
let app = App::new("cli")
.author(env!("CARGO_PKG_AUTHORS"));
Examples found in repository?
examples/single_app.rs (line 9)
4fn main() {
5 let args: Vec<String> = env::args().collect();
6 let name = "single_app";
7
8 let app = App::new(name)
9 .author(env!("CARGO_PKG_AUTHORS"))
10 .description(env!("CARGO_PKG_DESCRIPTION"))
11 .usage("single_app [args]")
12 .version(env!("CARGO_PKG_VERSION"))
13 .action(action)
14 .flag(
15 Flag::new("bye", FlagType::Bool)
16 .description("single_app args --bye(-b)")
17 .alias("b"),
18 );
19
20 app.run(args);
21}
More examples
examples/multiple_app.rs (line 7)
4fn main() {
5 let args: Vec<String> = env::args().collect();
6 let app = App::new("multiple_app")
7 .author(env!("CARGO_PKG_AUTHORS"))
8 .description(env!("CARGO_PKG_DESCRIPTION"))
9 .usage("multiple_app [command] [arg]")
10 .version(env!("CARGO_PKG_VERSION"))
11 .action(|c: &Context| println!("{:?} : {}", c.args, c.bool_flag("bool")))
12 .flag(
13 Flag::new("bool", FlagType::Bool)
14 .description("bool flag")
15 .alias("b"),
16 )
17 .command(add_command())
18 .command(hello_command());
19
20 app.run(args);
21}
examples/nested_multiple_app.rs (line 7)
4fn main() {
5 let args: Vec<String> = env::args().collect();
6 let app = App::new("multiple_app")
7 .author(env!("CARGO_PKG_AUTHORS"))
8 .description(env!("CARGO_PKG_DESCRIPTION"))
9 .usage("multiple_app [command] [arg]")
10 .version(env!("CARGO_PKG_VERSION"))
11 .action(|c: &Context| println!("{:?} : {}", c.args, c.bool_flag("bool")))
12 .flag(
13 Flag::new("bool", FlagType::Bool)
14 .description("bool flag")
15 .alias("b"),
16 )
17 .command(add_command())
18 .command(hello_command());
19
20 app.run(args);
21}
examples/top_level_error_handling.rs (line 7)
4fn main() {
5 let args: Vec<String> = env::args().collect();
6 let app = App::new("cli")
7 .author(env!("CARGO_PKG_AUTHORS"))
8 .description(env!("CARGO_PKG_DESCRIPTION"))
9 .usage("multiple_app [command] [arg]")
10 .version(env!("CARGO_PKG_VERSION"))
11 .action_with_result(|c: &Context| {
12 if c.bool_flag("error") {
13 Err(ActionError {
14 message: "ERROR...".to_string(),
15 })
16 } else {
17 Ok(())
18 }
19 })
20 .flag(
21 Flag::new("error", FlagType::Bool)
22 .description("error flag")
23 .alias("e"),
24 );
25
26 match app.run_with_result(args) {
27 Ok(_) => println!("OK"),
28 Err(e) => match e {
29 ActionError { message } => println!("{}", message),
30 },
31 };
32}
Sourcepub fn description<T: Into<String>>(self, description: T) -> Self
pub fn description<T: Into<String>>(self, description: T) -> Self
Set description of the app
Example
use seahorse::App;
let app = App::new("cli")
.description(env!("CARGO_PKG_DESCRIPTION"));
Examples found in repository?
examples/single_app.rs (line 10)
4fn main() {
5 let args: Vec<String> = env::args().collect();
6 let name = "single_app";
7
8 let app = App::new(name)
9 .author(env!("CARGO_PKG_AUTHORS"))
10 .description(env!("CARGO_PKG_DESCRIPTION"))
11 .usage("single_app [args]")
12 .version(env!("CARGO_PKG_VERSION"))
13 .action(action)
14 .flag(
15 Flag::new("bye", FlagType::Bool)
16 .description("single_app args --bye(-b)")
17 .alias("b"),
18 );
19
20 app.run(args);
21}
More examples
examples/multiple_app.rs (line 8)
4fn main() {
5 let args: Vec<String> = env::args().collect();
6 let app = App::new("multiple_app")
7 .author(env!("CARGO_PKG_AUTHORS"))
8 .description(env!("CARGO_PKG_DESCRIPTION"))
9 .usage("multiple_app [command] [arg]")
10 .version(env!("CARGO_PKG_VERSION"))
11 .action(|c: &Context| println!("{:?} : {}", c.args, c.bool_flag("bool")))
12 .flag(
13 Flag::new("bool", FlagType::Bool)
14 .description("bool flag")
15 .alias("b"),
16 )
17 .command(add_command())
18 .command(hello_command());
19
20 app.run(args);
21}
examples/nested_multiple_app.rs (line 8)
4fn main() {
5 let args: Vec<String> = env::args().collect();
6 let app = App::new("multiple_app")
7 .author(env!("CARGO_PKG_AUTHORS"))
8 .description(env!("CARGO_PKG_DESCRIPTION"))
9 .usage("multiple_app [command] [arg]")
10 .version(env!("CARGO_PKG_VERSION"))
11 .action(|c: &Context| println!("{:?} : {}", c.args, c.bool_flag("bool")))
12 .flag(
13 Flag::new("bool", FlagType::Bool)
14 .description("bool flag")
15 .alias("b"),
16 )
17 .command(add_command())
18 .command(hello_command());
19
20 app.run(args);
21}
examples/top_level_error_handling.rs (line 8)
4fn main() {
5 let args: Vec<String> = env::args().collect();
6 let app = App::new("cli")
7 .author(env!("CARGO_PKG_AUTHORS"))
8 .description(env!("CARGO_PKG_DESCRIPTION"))
9 .usage("multiple_app [command] [arg]")
10 .version(env!("CARGO_PKG_VERSION"))
11 .action_with_result(|c: &Context| {
12 if c.bool_flag("error") {
13 Err(ActionError {
14 message: "ERROR...".to_string(),
15 })
16 } else {
17 Ok(())
18 }
19 })
20 .flag(
21 Flag::new("error", FlagType::Bool)
22 .description("error flag")
23 .alias("e"),
24 );
25
26 match app.run_with_result(args) {
27 Ok(_) => println!("OK"),
28 Err(e) => match e {
29 ActionError { message } => println!("{}", message),
30 },
31 };
32}
Sourcepub fn usage<T: Into<String>>(self, usage: T) -> Self
pub fn usage<T: Into<String>>(self, usage: T) -> Self
Set usage of the app
Example
use seahorse::App;
let app = App::new("cli");
app.usage("cli [command] [arg]");
Examples found in repository?
examples/single_app.rs (line 11)
4fn main() {
5 let args: Vec<String> = env::args().collect();
6 let name = "single_app";
7
8 let app = App::new(name)
9 .author(env!("CARGO_PKG_AUTHORS"))
10 .description(env!("CARGO_PKG_DESCRIPTION"))
11 .usage("single_app [args]")
12 .version(env!("CARGO_PKG_VERSION"))
13 .action(action)
14 .flag(
15 Flag::new("bye", FlagType::Bool)
16 .description("single_app args --bye(-b)")
17 .alias("b"),
18 );
19
20 app.run(args);
21}
More examples
examples/multiple_app.rs (line 9)
4fn main() {
5 let args: Vec<String> = env::args().collect();
6 let app = App::new("multiple_app")
7 .author(env!("CARGO_PKG_AUTHORS"))
8 .description(env!("CARGO_PKG_DESCRIPTION"))
9 .usage("multiple_app [command] [arg]")
10 .version(env!("CARGO_PKG_VERSION"))
11 .action(|c: &Context| println!("{:?} : {}", c.args, c.bool_flag("bool")))
12 .flag(
13 Flag::new("bool", FlagType::Bool)
14 .description("bool flag")
15 .alias("b"),
16 )
17 .command(add_command())
18 .command(hello_command());
19
20 app.run(args);
21}
examples/nested_multiple_app.rs (line 9)
4fn main() {
5 let args: Vec<String> = env::args().collect();
6 let app = App::new("multiple_app")
7 .author(env!("CARGO_PKG_AUTHORS"))
8 .description(env!("CARGO_PKG_DESCRIPTION"))
9 .usage("multiple_app [command] [arg]")
10 .version(env!("CARGO_PKG_VERSION"))
11 .action(|c: &Context| println!("{:?} : {}", c.args, c.bool_flag("bool")))
12 .flag(
13 Flag::new("bool", FlagType::Bool)
14 .description("bool flag")
15 .alias("b"),
16 )
17 .command(add_command())
18 .command(hello_command());
19
20 app.run(args);
21}
examples/top_level_error_handling.rs (line 9)
4fn main() {
5 let args: Vec<String> = env::args().collect();
6 let app = App::new("cli")
7 .author(env!("CARGO_PKG_AUTHORS"))
8 .description(env!("CARGO_PKG_DESCRIPTION"))
9 .usage("multiple_app [command] [arg]")
10 .version(env!("CARGO_PKG_VERSION"))
11 .action_with_result(|c: &Context| {
12 if c.bool_flag("error") {
13 Err(ActionError {
14 message: "ERROR...".to_string(),
15 })
16 } else {
17 Ok(())
18 }
19 })
20 .flag(
21 Flag::new("error", FlagType::Bool)
22 .description("error flag")
23 .alias("e"),
24 );
25
26 match app.run_with_result(args) {
27 Ok(_) => println!("OK"),
28 Err(e) => match e {
29 ActionError { message } => println!("{}", message),
30 },
31 };
32}
Sourcepub fn version<T: Into<String>>(self, version: T) -> Self
pub fn version<T: Into<String>>(self, version: T) -> Self
Set version of the app
Example
use seahorse::App;
let app = App::new("cli");
app.version(env!("CARGO_PKG_VERSION"));
Examples found in repository?
examples/single_app.rs (line 12)
4fn main() {
5 let args: Vec<String> = env::args().collect();
6 let name = "single_app";
7
8 let app = App::new(name)
9 .author(env!("CARGO_PKG_AUTHORS"))
10 .description(env!("CARGO_PKG_DESCRIPTION"))
11 .usage("single_app [args]")
12 .version(env!("CARGO_PKG_VERSION"))
13 .action(action)
14 .flag(
15 Flag::new("bye", FlagType::Bool)
16 .description("single_app args --bye(-b)")
17 .alias("b"),
18 );
19
20 app.run(args);
21}
More examples
examples/multiple_app.rs (line 10)
4fn main() {
5 let args: Vec<String> = env::args().collect();
6 let app = App::new("multiple_app")
7 .author(env!("CARGO_PKG_AUTHORS"))
8 .description(env!("CARGO_PKG_DESCRIPTION"))
9 .usage("multiple_app [command] [arg]")
10 .version(env!("CARGO_PKG_VERSION"))
11 .action(|c: &Context| println!("{:?} : {}", c.args, c.bool_flag("bool")))
12 .flag(
13 Flag::new("bool", FlagType::Bool)
14 .description("bool flag")
15 .alias("b"),
16 )
17 .command(add_command())
18 .command(hello_command());
19
20 app.run(args);
21}
examples/nested_multiple_app.rs (line 10)
4fn main() {
5 let args: Vec<String> = env::args().collect();
6 let app = App::new("multiple_app")
7 .author(env!("CARGO_PKG_AUTHORS"))
8 .description(env!("CARGO_PKG_DESCRIPTION"))
9 .usage("multiple_app [command] [arg]")
10 .version(env!("CARGO_PKG_VERSION"))
11 .action(|c: &Context| println!("{:?} : {}", c.args, c.bool_flag("bool")))
12 .flag(
13 Flag::new("bool", FlagType::Bool)
14 .description("bool flag")
15 .alias("b"),
16 )
17 .command(add_command())
18 .command(hello_command());
19
20 app.run(args);
21}
examples/top_level_error_handling.rs (line 10)
4fn main() {
5 let args: Vec<String> = env::args().collect();
6 let app = App::new("cli")
7 .author(env!("CARGO_PKG_AUTHORS"))
8 .description(env!("CARGO_PKG_DESCRIPTION"))
9 .usage("multiple_app [command] [arg]")
10 .version(env!("CARGO_PKG_VERSION"))
11 .action_with_result(|c: &Context| {
12 if c.bool_flag("error") {
13 Err(ActionError {
14 message: "ERROR...".to_string(),
15 })
16 } else {
17 Ok(())
18 }
19 })
20 .flag(
21 Flag::new("error", FlagType::Bool)
22 .description("error flag")
23 .alias("e"),
24 );
25
26 match app.run_with_result(args) {
27 Ok(_) => println!("OK"),
28 Err(e) => match e {
29 ActionError { message } => println!("{}", message),
30 },
31 };
32}
Sourcepub fn command(self, command: Command) -> Self
pub fn command(self, command: Command) -> Self
Set command of the app
Example
use seahorse::{App, Command};
let command = Command::new("hello")
.usage("cli hello [arg]")
.action(|c| println!("{:?}", c.args));
let app = App::new("cli")
.command(command);
§Panics
You cannot set a command named as same as registered ones.
ⓘ
use seahorse::{App, Command};
let command1 = Command::new("hello")
.usage("cli hello [arg]")
.action(|c| println!("{:?}", c.args));
let command2 = Command::new("hello")
.usage("cli hello [arg]")
.action(|c| println!("{:?}", c.args));
let app = App::new("cli")
.command(command1)
.command(command2);
Examples found in repository?
examples/multiple_app.rs (line 17)
4fn main() {
5 let args: Vec<String> = env::args().collect();
6 let app = App::new("multiple_app")
7 .author(env!("CARGO_PKG_AUTHORS"))
8 .description(env!("CARGO_PKG_DESCRIPTION"))
9 .usage("multiple_app [command] [arg]")
10 .version(env!("CARGO_PKG_VERSION"))
11 .action(|c: &Context| println!("{:?} : {}", c.args, c.bool_flag("bool")))
12 .flag(
13 Flag::new("bool", FlagType::Bool)
14 .description("bool flag")
15 .alias("b"),
16 )
17 .command(add_command())
18 .command(hello_command());
19
20 app.run(args);
21}
More examples
examples/nested_multiple_app.rs (line 17)
4fn main() {
5 let args: Vec<String> = env::args().collect();
6 let app = App::new("multiple_app")
7 .author(env!("CARGO_PKG_AUTHORS"))
8 .description(env!("CARGO_PKG_DESCRIPTION"))
9 .usage("multiple_app [command] [arg]")
10 .version(env!("CARGO_PKG_VERSION"))
11 .action(|c: &Context| println!("{:?} : {}", c.args, c.bool_flag("bool")))
12 .flag(
13 Flag::new("bool", FlagType::Bool)
14 .description("bool flag")
15 .alias("b"),
16 )
17 .command(add_command())
18 .command(hello_command());
19
20 app.run(args);
21}
Sourcepub fn action(self, action: Action) -> Self
pub fn action(self, action: Action) -> Self
Set action of the app
Example
use seahorse::{Action, App, Context};
let action: Action = |c: &Context| println!("{:?}", c.args);
let app = App::new("cli")
.action(action);
§Panics
You cannot set both action and action_with_result.
ⓘ
use seahorse::{Action, ActionWithResult, App, Context};
let action_with_result: ActionWithResult = |c: &Context| {println!("{:?}", c.args); Ok(())};
let action: Action = |c: &Context| println!("{:?}", c.args);
let app = App::new("cli")
.action_with_result(action_with_result)
.action(action);
Examples found in repository?
examples/single_app.rs (line 13)
4fn main() {
5 let args: Vec<String> = env::args().collect();
6 let name = "single_app";
7
8 let app = App::new(name)
9 .author(env!("CARGO_PKG_AUTHORS"))
10 .description(env!("CARGO_PKG_DESCRIPTION"))
11 .usage("single_app [args]")
12 .version(env!("CARGO_PKG_VERSION"))
13 .action(action)
14 .flag(
15 Flag::new("bye", FlagType::Bool)
16 .description("single_app args --bye(-b)")
17 .alias("b"),
18 );
19
20 app.run(args);
21}
More examples
examples/multiple_app.rs (line 11)
4fn main() {
5 let args: Vec<String> = env::args().collect();
6 let app = App::new("multiple_app")
7 .author(env!("CARGO_PKG_AUTHORS"))
8 .description(env!("CARGO_PKG_DESCRIPTION"))
9 .usage("multiple_app [command] [arg]")
10 .version(env!("CARGO_PKG_VERSION"))
11 .action(|c: &Context| println!("{:?} : {}", c.args, c.bool_flag("bool")))
12 .flag(
13 Flag::new("bool", FlagType::Bool)
14 .description("bool flag")
15 .alias("b"),
16 )
17 .command(add_command())
18 .command(hello_command());
19
20 app.run(args);
21}
examples/nested_multiple_app.rs (line 11)
4fn main() {
5 let args: Vec<String> = env::args().collect();
6 let app = App::new("multiple_app")
7 .author(env!("CARGO_PKG_AUTHORS"))
8 .description(env!("CARGO_PKG_DESCRIPTION"))
9 .usage("multiple_app [command] [arg]")
10 .version(env!("CARGO_PKG_VERSION"))
11 .action(|c: &Context| println!("{:?} : {}", c.args, c.bool_flag("bool")))
12 .flag(
13 Flag::new("bool", FlagType::Bool)
14 .description("bool flag")
15 .alias("b"),
16 )
17 .command(add_command())
18 .command(hello_command());
19
20 app.run(args);
21}
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 app
Example
use seahorse::{ActionWithResult, App, Context};
let action_with_result: ActionWithResult = |c: &Context| {println!("{:?}", c.args); Ok(())};
let app = App::new("cli")
.action_with_result(action_with_result);
§Panics
You cannot set both action and action_with_result.
ⓘ
use seahorse::{Action, ActionWithResult, App, Context};
let action_with_result: ActionWithResult = |c: &Context| {println!("{:?}", c.args); Ok(())};
let action: Action = |c: &Context| println!("{:?}", c.args);
let app = App::new("cli")
.action(action)
.action_with_result(action_with_result);
Examples found in repository?
examples/top_level_error_handling.rs (lines 11-19)
4fn main() {
5 let args: Vec<String> = env::args().collect();
6 let app = App::new("cli")
7 .author(env!("CARGO_PKG_AUTHORS"))
8 .description(env!("CARGO_PKG_DESCRIPTION"))
9 .usage("multiple_app [command] [arg]")
10 .version(env!("CARGO_PKG_VERSION"))
11 .action_with_result(|c: &Context| {
12 if c.bool_flag("error") {
13 Err(ActionError {
14 message: "ERROR...".to_string(),
15 })
16 } else {
17 Ok(())
18 }
19 })
20 .flag(
21 Flag::new("error", FlagType::Bool)
22 .description("error flag")
23 .alias("e"),
24 );
25
26 match app.run_with_result(args) {
27 Ok(_) => println!("OK"),
28 Err(e) => match e {
29 ActionError { message } => println!("{}", message),
30 },
31 };
32}
Sourcepub fn flag(self, flag: Flag) -> Self
pub fn flag(self, flag: Flag) -> Self
Set flag of the app
Example
use seahorse::{App, Flag, FlagType};
let app = App::new("cli")
.flag(Flag::new("bool", FlagType::Bool))
.flag(Flag::new("int", FlagType::Int));
Examples found in repository?
examples/single_app.rs (lines 14-18)
4fn main() {
5 let args: Vec<String> = env::args().collect();
6 let name = "single_app";
7
8 let app = App::new(name)
9 .author(env!("CARGO_PKG_AUTHORS"))
10 .description(env!("CARGO_PKG_DESCRIPTION"))
11 .usage("single_app [args]")
12 .version(env!("CARGO_PKG_VERSION"))
13 .action(action)
14 .flag(
15 Flag::new("bye", FlagType::Bool)
16 .description("single_app args --bye(-b)")
17 .alias("b"),
18 );
19
20 app.run(args);
21}
More examples
examples/multiple_app.rs (lines 12-16)
4fn main() {
5 let args: Vec<String> = env::args().collect();
6 let app = App::new("multiple_app")
7 .author(env!("CARGO_PKG_AUTHORS"))
8 .description(env!("CARGO_PKG_DESCRIPTION"))
9 .usage("multiple_app [command] [arg]")
10 .version(env!("CARGO_PKG_VERSION"))
11 .action(|c: &Context| println!("{:?} : {}", c.args, c.bool_flag("bool")))
12 .flag(
13 Flag::new("bool", FlagType::Bool)
14 .description("bool flag")
15 .alias("b"),
16 )
17 .command(add_command())
18 .command(hello_command());
19
20 app.run(args);
21}
examples/nested_multiple_app.rs (lines 12-16)
4fn main() {
5 let args: Vec<String> = env::args().collect();
6 let app = App::new("multiple_app")
7 .author(env!("CARGO_PKG_AUTHORS"))
8 .description(env!("CARGO_PKG_DESCRIPTION"))
9 .usage("multiple_app [command] [arg]")
10 .version(env!("CARGO_PKG_VERSION"))
11 .action(|c: &Context| println!("{:?} : {}", c.args, c.bool_flag("bool")))
12 .flag(
13 Flag::new("bool", FlagType::Bool)
14 .description("bool flag")
15 .alias("b"),
16 )
17 .command(add_command())
18 .command(hello_command());
19
20 app.run(args);
21}
examples/top_level_error_handling.rs (lines 20-24)
4fn main() {
5 let args: Vec<String> = env::args().collect();
6 let app = App::new("cli")
7 .author(env!("CARGO_PKG_AUTHORS"))
8 .description(env!("CARGO_PKG_DESCRIPTION"))
9 .usage("multiple_app [command] [arg]")
10 .version(env!("CARGO_PKG_VERSION"))
11 .action_with_result(|c: &Context| {
12 if c.bool_flag("error") {
13 Err(ActionError {
14 message: "ERROR...".to_string(),
15 })
16 } else {
17 Ok(())
18 }
19 })
20 .flag(
21 Flag::new("error", FlagType::Bool)
22 .description("error flag")
23 .alias("e"),
24 );
25
26 match app.run_with_result(args) {
27 Ok(_) => println!("OK"),
28 Err(e) => match e {
29 ActionError { message } => println!("{}", message),
30 },
31 };
32}
Sourcepub fn run(&self, args: Vec<String>)
pub fn run(&self, args: Vec<String>)
Run app
Example
use std::env;
use seahorse::App;
let args: Vec<String> = env::args().collect();
let app = App::new("cli");
app.run(args);
Examples found in repository?
examples/single_app.rs (line 20)
4fn main() {
5 let args: Vec<String> = env::args().collect();
6 let name = "single_app";
7
8 let app = App::new(name)
9 .author(env!("CARGO_PKG_AUTHORS"))
10 .description(env!("CARGO_PKG_DESCRIPTION"))
11 .usage("single_app [args]")
12 .version(env!("CARGO_PKG_VERSION"))
13 .action(action)
14 .flag(
15 Flag::new("bye", FlagType::Bool)
16 .description("single_app args --bye(-b)")
17 .alias("b"),
18 );
19
20 app.run(args);
21}
More examples
examples/multiple_app.rs (line 20)
4fn main() {
5 let args: Vec<String> = env::args().collect();
6 let app = App::new("multiple_app")
7 .author(env!("CARGO_PKG_AUTHORS"))
8 .description(env!("CARGO_PKG_DESCRIPTION"))
9 .usage("multiple_app [command] [arg]")
10 .version(env!("CARGO_PKG_VERSION"))
11 .action(|c: &Context| println!("{:?} : {}", c.args, c.bool_flag("bool")))
12 .flag(
13 Flag::new("bool", FlagType::Bool)
14 .description("bool flag")
15 .alias("b"),
16 )
17 .command(add_command())
18 .command(hello_command());
19
20 app.run(args);
21}
examples/nested_multiple_app.rs (line 20)
4fn main() {
5 let args: Vec<String> = env::args().collect();
6 let app = App::new("multiple_app")
7 .author(env!("CARGO_PKG_AUTHORS"))
8 .description(env!("CARGO_PKG_DESCRIPTION"))
9 .usage("multiple_app [command] [arg]")
10 .version(env!("CARGO_PKG_VERSION"))
11 .action(|c: &Context| println!("{:?} : {}", c.args, c.bool_flag("bool")))
12 .flag(
13 Flag::new("bool", FlagType::Bool)
14 .description("bool flag")
15 .alias("b"),
16 )
17 .command(add_command())
18 .command(hello_command());
19
20 app.run(args);
21}
Sourcepub fn run_with_result(&self, args: Vec<String>) -> ActionResult
pub fn run_with_result(&self, args: Vec<String>) -> ActionResult
Run app, returning a result
Example
use std::env;
use seahorse::App;
let args: Vec<String> = env::args().collect();
let app = App::new("cli");
let result = app.run_with_result(args);
Examples found in repository?
examples/top_level_error_handling.rs (line 26)
4fn main() {
5 let args: Vec<String> = env::args().collect();
6 let app = App::new("cli")
7 .author(env!("CARGO_PKG_AUTHORS"))
8 .description(env!("CARGO_PKG_DESCRIPTION"))
9 .usage("multiple_app [command] [arg]")
10 .version(env!("CARGO_PKG_VERSION"))
11 .action_with_result(|c: &Context| {
12 if c.bool_flag("error") {
13 Err(ActionError {
14 message: "ERROR...".to_string(),
15 })
16 } else {
17 Ok(())
18 }
19 })
20 .flag(
21 Flag::new("error", FlagType::Bool)
22 .description("error flag")
23 .alias("e"),
24 );
25
26 match app.run_with_result(args) {
27 Ok(_) => println!("OK"),
28 Err(e) => match e {
29 ActionError { message } => println!("{}", message),
30 },
31 };
32}
Trait Implementations§
Auto Trait Implementations§
impl Freeze for App
impl RefUnwindSafe for App
impl Send for App
impl Sync for App
impl Unpin for App
impl UnwindSafe for App
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