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)
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
fn main() {
let args: Vec<String> = env::args().collect();
let name = "single_app";
let app = App::new(name)
.author(env!("CARGO_PKG_AUTHORS"))
.description(env!("CARGO_PKG_DESCRIPTION"))
.usage("single_app [args]")
.version(env!("CARGO_PKG_VERSION"))
.action(action)
.flag(
Flag::new("bye", FlagType::Bool)
.description("single_app args --bye(-b)")
.alias("b"),
);
app.run(args);
}
More examples
examples/multiple_app.rs (line 6)
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
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);
}
examples/nested_multiple_app.rs (line 6)
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
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);
}
examples/top_level_error_handling.rs (line 6)
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),
},
};
}
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)
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
fn main() {
let args: Vec<String> = env::args().collect();
let name = "single_app";
let app = App::new(name)
.author(env!("CARGO_PKG_AUTHORS"))
.description(env!("CARGO_PKG_DESCRIPTION"))
.usage("single_app [args]")
.version(env!("CARGO_PKG_VERSION"))
.action(action)
.flag(
Flag::new("bye", FlagType::Bool)
.description("single_app args --bye(-b)")
.alias("b"),
);
app.run(args);
}
More examples
examples/multiple_app.rs (line 7)
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
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);
}
examples/nested_multiple_app.rs (line 7)
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
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);
}
examples/top_level_error_handling.rs (line 7)
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),
},
};
}
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)
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
fn main() {
let args: Vec<String> = env::args().collect();
let name = "single_app";
let app = App::new(name)
.author(env!("CARGO_PKG_AUTHORS"))
.description(env!("CARGO_PKG_DESCRIPTION"))
.usage("single_app [args]")
.version(env!("CARGO_PKG_VERSION"))
.action(action)
.flag(
Flag::new("bye", FlagType::Bool)
.description("single_app args --bye(-b)")
.alias("b"),
);
app.run(args);
}
More examples
examples/multiple_app.rs (line 8)
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
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);
}
examples/nested_multiple_app.rs (line 8)
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
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);
}
examples/top_level_error_handling.rs (line 8)
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),
},
};
}
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)
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
fn main() {
let args: Vec<String> = env::args().collect();
let name = "single_app";
let app = App::new(name)
.author(env!("CARGO_PKG_AUTHORS"))
.description(env!("CARGO_PKG_DESCRIPTION"))
.usage("single_app [args]")
.version(env!("CARGO_PKG_VERSION"))
.action(action)
.flag(
Flag::new("bye", FlagType::Bool)
.description("single_app args --bye(-b)")
.alias("b"),
);
app.run(args);
}
More examples
examples/multiple_app.rs (line 9)
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
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);
}
examples/nested_multiple_app.rs (line 9)
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
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);
}
examples/top_level_error_handling.rs (line 9)
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),
},
};
}
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)
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
fn main() {
let args: Vec<String> = env::args().collect();
let name = "single_app";
let app = App::new(name)
.author(env!("CARGO_PKG_AUTHORS"))
.description(env!("CARGO_PKG_DESCRIPTION"))
.usage("single_app [args]")
.version(env!("CARGO_PKG_VERSION"))
.action(action)
.flag(
Flag::new("bye", FlagType::Bool)
.description("single_app args --bye(-b)")
.alias("b"),
);
app.run(args);
}
More examples
examples/multiple_app.rs (line 10)
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
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);
}
examples/nested_multiple_app.rs (line 10)
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
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);
}
examples/top_level_error_handling.rs (line 10)
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),
},
};
}
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)
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
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);
}
More examples
examples/nested_multiple_app.rs (line 17)
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
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);
}
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)
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
fn main() {
let args: Vec<String> = env::args().collect();
let name = "single_app";
let app = App::new(name)
.author(env!("CARGO_PKG_AUTHORS"))
.description(env!("CARGO_PKG_DESCRIPTION"))
.usage("single_app [args]")
.version(env!("CARGO_PKG_VERSION"))
.action(action)
.flag(
Flag::new("bye", FlagType::Bool)
.description("single_app args --bye(-b)")
.alias("b"),
);
app.run(args);
}
More 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
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);
}
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
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);
}
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)
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),
},
};
}
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)
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
fn main() {
let args: Vec<String> = env::args().collect();
let name = "single_app";
let app = App::new(name)
.author(env!("CARGO_PKG_AUTHORS"))
.description(env!("CARGO_PKG_DESCRIPTION"))
.usage("single_app [args]")
.version(env!("CARGO_PKG_VERSION"))
.action(action)
.flag(
Flag::new("bye", FlagType::Bool)
.description("single_app args --bye(-b)")
.alias("b"),
);
app.run(args);
}
More examples
examples/multiple_app.rs (lines 12-16)
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
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);
}
examples/nested_multiple_app.rs (lines 12-16)
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
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);
}
examples/top_level_error_handling.rs (lines 20-24)
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),
},
};
}
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)
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
fn main() {
let args: Vec<String> = env::args().collect();
let name = "single_app";
let app = App::new(name)
.author(env!("CARGO_PKG_AUTHORS"))
.description(env!("CARGO_PKG_DESCRIPTION"))
.usage("single_app [args]")
.version(env!("CARGO_PKG_VERSION"))
.action(action)
.flag(
Flag::new("bye", FlagType::Bool)
.description("single_app args --bye(-b)")
.alias("b"),
);
app.run(args);
}
More examples
examples/multiple_app.rs (line 20)
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
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);
}
examples/nested_multiple_app.rs (line 20)
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
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);
}
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)
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),
},
};
}
Trait Implementations§
Auto Trait Implementations§
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