pub struct Command {
pub name: String,
pub action: Option<Action>,
pub authors: String,
pub copyright: String,
pub license: License,
pub description: Option<String>,
pub usage: String,
pub l_flags: Vector<Flag>,
pub c_flags: Vector<Flag>,
pub alias: Vector<String>,
pub version: String,
pub sub: Vector<Command>,
}Expand description
The struct for command information store and command execution This can be root and edge コマンドの情報格納&実行用構造体です。root(根)にもedge(葉)にもなりえます。
Fields§
§name: StringCommand name
action: Option<Action>Command action
Command authors
copyright: StringCommand copyright
license: LicenseLicense of command
description: Option<String>Command description
usage: StringCommand usage
l_flags: Vector<Flag>local flags of command
c_flags: Vector<Flag>common flags of command
alias: Vector<String>alias for command
version: StringCommand version
sub: Vector<Command>container of sub-command
Implementations§
Source§impl Command
impl Command
Sourcepub fn new() -> Command
pub fn new() -> Command
Creare new instance of Command
Examples found in repository?
69fn add_command() -> Command {
70 Command::new()
71 .name("add")
72 .alias("a")
73 .action(add_action)
74 .local_flag(Flag::new_bool("detail").short_alias('d'))
75}
76
77fn add_action(cmd: Command, c: Context) -> Result<ActionResult, ActionError> {
78 if called_help(&c, &cmd) {
79 return call_help(&c, &cmd);
80 }
81 let f = |(str, sum), num: f64| (format!("{str} {num} +"), sum + num);
82 let (mut str, sum): (String, f64) =
83 if c.get_flag_value_of("reverse", &cmd) == Some(FlagValue::Bool(true)) {
84 c.args
85 .iter()
86 .rev()
87 .filter_map(|arg| arg.parse().ok())
88 .fold((String::new(), 0.0), f)
89 } else {
90 c.args
91 .iter()
92 .filter_map(|arg| arg.parse().ok())
93 .fold((String::new(), 0.0), f)
94 };
95 str.pop();
96 str.pop();
97
98 if c.get_flag_value_of("detail", &cmd).unwrap().is_bool_true() {
99 println!("{str} = {sum}");
100 } else {
101 println!("{sum}");
102 }
103 Ok(ActionResult::Done)
104}
105
106fn sub_command() -> Command {
107 Command::new()
108 .name("sub")
109 .alias("s")
110 .action(sub_action)
111 .local_flag(Flag::new_bool("sort").short_alias('s'))
112}Sourcepub fn with_base<T: Into<String>>(
name: T,
authors: T,
version: T,
description: T,
action: Option<Action>,
) -> Command
pub fn with_base<T: Into<String>>( name: T, authors: T, version: T, description: T, action: Option<Action>, ) -> Command
Create new instance of Command with name,authors,version
Sourcepub fn with_name<T: Into<String>>(name: T) -> Command
pub fn with_name<T: Into<String>>(name: T) -> Command
Create new instance of Command with name
Examples found in repository?
More examples
16fn root_command() -> Command {
17 Command::with_name("multi")
18 .description("root command sample: arg printer")
19 .usage(presets::usage("multi"))
20 .common_flag(Flag::new_bool("help").short_alias('h'))
21 .common_flag(Flag::new_bool("reverse").short_alias('r'))
22 .local_flag(
23 Flag::new_bool("by-char")
24 .short_alias('c')
25 .description("process at char units"),
26 )
27 .action(print_args)
28 .sub_command(add_command())
29 .sub_command(sub_command())
30}7fn main() {
8 let _r = preset_root!(act)
9 .usage(env!("CARGO_PKG_NAME").to_string() + " [args]")
10 .common_flag(
11 Flag::new_bool("help")
12 .short_alias('h')
13 .description("show help"),
14 )
15 .local_flag(
16 Flag::new_bool("local")
17 .short_alias('l')
18 //.alias("test")
19 .description("local flag"),
20 )
21 .sub_command(
22 Command::with_name("sub")
23 .description("sub description")
24 .action(sub_act)
25 .usage("sub usage")
26 .local_flag(Flag::new_bool("sflag").description("sub local flag"))
27 .sub_command(Command::with_name("leaf").description("leaf description")),
28 )
29 .sub_command(
30 Command::with_name("help")
31 .description("show help")
32 .action(help_action),
33 )
34 .sub_command(
35 Command::with_name("help2")
36 .description("show help2")
37 .action(help_command_action!(help_tablize_with_alias_dedup)),
38 )
39 .run_from_args(env::args().collect());
40}Sourcepub fn with_all_field(
name: String,
action: Option<Action>,
authors: String,
copyright: String,
license: License,
description: Option<String>,
usage: String,
local_flags: Vector<Flag>,
common_flags: Vector<Flag>,
alias: Vector<String>,
version: String,
sub: Vector<Command>,
) -> Command
pub fn with_all_field( name: String, action: Option<Action>, authors: String, copyright: String, license: License, description: Option<String>, usage: String, local_flags: Vector<Flag>, common_flags: Vector<Flag>, alias: Vector<String>, version: String, sub: Vector<Command>, ) -> Command
Create new instance of Command with more options
Sourcepub fn run_with_auto_arg_collect(self) -> Result<ActionResult, ActionError>
pub fn run_with_auto_arg_collect(self) -> Result<ActionResult, ActionError>
Run command with collecting args automatically
Sourcepub fn single_run(
self,
raw_args: Vec<String>,
) -> Result<ActionResult, ActionError>
pub fn single_run( self, raw_args: Vec<String>, ) -> Result<ActionResult, ActionError>
Run command as single(do not have sub) command ルートからサブコマンドがないシンプルな状態の時 アクションが登録されていなければサブコマンドがあるかを調査する
Sourcepub fn name<T: Into<String>>(self, name: T) -> Command
pub fn name<T: Into<String>>(self, name: T) -> Command
Set Command’s name
Examples found in repository?
69fn add_command() -> Command {
70 Command::new()
71 .name("add")
72 .alias("a")
73 .action(add_action)
74 .local_flag(Flag::new_bool("detail").short_alias('d'))
75}
76
77fn add_action(cmd: Command, c: Context) -> Result<ActionResult, ActionError> {
78 if called_help(&c, &cmd) {
79 return call_help(&c, &cmd);
80 }
81 let f = |(str, sum), num: f64| (format!("{str} {num} +"), sum + num);
82 let (mut str, sum): (String, f64) =
83 if c.get_flag_value_of("reverse", &cmd) == Some(FlagValue::Bool(true)) {
84 c.args
85 .iter()
86 .rev()
87 .filter_map(|arg| arg.parse().ok())
88 .fold((String::new(), 0.0), f)
89 } else {
90 c.args
91 .iter()
92 .filter_map(|arg| arg.parse().ok())
93 .fold((String::new(), 0.0), f)
94 };
95 str.pop();
96 str.pop();
97
98 if c.get_flag_value_of("detail", &cmd).unwrap().is_bool_true() {
99 println!("{str} = {sum}");
100 } else {
101 println!("{sum}");
102 }
103 Ok(ActionResult::Done)
104}
105
106fn sub_command() -> Command {
107 Command::new()
108 .name("sub")
109 .alias("s")
110 .action(sub_action)
111 .local_flag(Flag::new_bool("sort").short_alias('s'))
112}Sourcepub fn usage<T: Into<String>>(self, usage: T) -> Self
pub fn usage<T: Into<String>>(self, usage: T) -> Self
Set command’s usage
Examples found in repository?
16fn root_command() -> Command {
17 Command::with_name("multi")
18 .description("root command sample: arg printer")
19 .usage(presets::usage("multi"))
20 .common_flag(Flag::new_bool("help").short_alias('h'))
21 .common_flag(Flag::new_bool("reverse").short_alias('r'))
22 .local_flag(
23 Flag::new_bool("by-char")
24 .short_alias('c')
25 .description("process at char units"),
26 )
27 .action(print_args)
28 .sub_command(add_command())
29 .sub_command(sub_command())
30}More examples
6fn main() {
7 let _r = preset_root!(act)
8 .usage(env!("CARGO_PKG_NAME").to_string() + " [args]")
9 .common_flag(
10 Flag::new_bool("help")
11 .short_alias('h')
12 .description("show help"),
13 )
14 .local_flag(
15 Flag::new_bool("local")
16 .short_alias('l')
17 //.alias("test")
18 .description("local flag"),
19 )
20 /* If you want to use help subcommand,uncomment this block and add preset_help_command to use.
21 .sub_command(preset_help_command!(help_tablize_with_alias_dedup))
22 */
23 /* If you want to use subcommand, uncomment this block, then remove this line and the line above sub_act function.
24 .sub_command(
25 Command::with_name("sub")
26 .desctiption("sub description")
27 .action(sub_act)
28 .usage("sub usage")
29 .local_flag(Flag::new_bool("sflag").description("sub local flag")),
30 )*/
31 .run_from_args(env::args().collect());
32}7fn main() {
8 let _r = preset_root!(act)
9 .usage(env!("CARGO_PKG_NAME").to_string() + " [args]")
10 .common_flag(
11 Flag::new_bool("help")
12 .short_alias('h')
13 .description("show help"),
14 )
15 .local_flag(
16 Flag::new_bool("local")
17 .short_alias('l')
18 //.alias("test")
19 .description("local flag"),
20 )
21 .sub_command(
22 Command::with_name("sub")
23 .description("sub description")
24 .action(sub_act)
25 .usage("sub usage")
26 .local_flag(Flag::new_bool("sflag").description("sub local flag"))
27 .sub_command(Command::with_name("leaf").description("leaf description")),
28 )
29 .sub_command(
30 Command::with_name("help")
31 .description("show help")
32 .action(help_action),
33 )
34 .sub_command(
35 Command::with_name("help2")
36 .description("show help2")
37 .action(help_command_action!(help_tablize_with_alias_dedup)),
38 )
39 .run_from_args(env::args().collect());
40}Sourcepub fn action(self, action: Action) -> Self
pub fn action(self, action: Action) -> Self
Set command’s action
Examples found in repository?
More examples
16fn root_command() -> Command {
17 Command::with_name("multi")
18 .description("root command sample: arg printer")
19 .usage(presets::usage("multi"))
20 .common_flag(Flag::new_bool("help").short_alias('h'))
21 .common_flag(Flag::new_bool("reverse").short_alias('r'))
22 .local_flag(
23 Flag::new_bool("by-char")
24 .short_alias('c')
25 .description("process at char units"),
26 )
27 .action(print_args)
28 .sub_command(add_command())
29 .sub_command(sub_command())
30}
31fn call_help(c: &Context, cur_cmd: &Command) -> Result<ActionResult, ActionError> {
32 println!("{}", presets::func::help(cur_cmd, c));
33 done!()
34}
35fn print_args(current_command: Command, context: Context) -> Result<ActionResult, ActionError> {
36 if called_help(&context, ¤t_command) {
37 return call_help(&context, ¤t_command);
38 }
39 let r: bool =
40 context.get_flag_value_of("reverse", ¤t_command) == Some(FlagValue::Bool(true));
41 let c: bool =
42 context.get_flag_value_of("by-char", ¤t_command) == Some(FlagValue::Bool(true));
43 let str = {
44 let str = if r && !c {
45 context
46 .args
47 .iter()
48 .rev()
49 .fold(String::new(), |c, arg| c + arg)
50 } else {
51 context.args.iter().fold(String::new(), |c, arg| c + arg)
52 };
53 if c {
54 str.chars().rev().collect::<String>()
55 } else {
56 str
57 }
58 };
59
60 println!("{str}");
61
62 Ok(ActionResult::Done)
63}
64
65fn called_help(c: &Context, cc: &Command) -> bool {
66 Some(FlagValue::Bool(true)) == c.get_flag_value_of("help", cc)
67}
68
69fn add_command() -> Command {
70 Command::new()
71 .name("add")
72 .alias("a")
73 .action(add_action)
74 .local_flag(Flag::new_bool("detail").short_alias('d'))
75}
76
77fn add_action(cmd: Command, c: Context) -> Result<ActionResult, ActionError> {
78 if called_help(&c, &cmd) {
79 return call_help(&c, &cmd);
80 }
81 let f = |(str, sum), num: f64| (format!("{str} {num} +"), sum + num);
82 let (mut str, sum): (String, f64) =
83 if c.get_flag_value_of("reverse", &cmd) == Some(FlagValue::Bool(true)) {
84 c.args
85 .iter()
86 .rev()
87 .filter_map(|arg| arg.parse().ok())
88 .fold((String::new(), 0.0), f)
89 } else {
90 c.args
91 .iter()
92 .filter_map(|arg| arg.parse().ok())
93 .fold((String::new(), 0.0), f)
94 };
95 str.pop();
96 str.pop();
97
98 if c.get_flag_value_of("detail", &cmd).unwrap().is_bool_true() {
99 println!("{str} = {sum}");
100 } else {
101 println!("{sum}");
102 }
103 Ok(ActionResult::Done)
104}
105
106fn sub_command() -> Command {
107 Command::new()
108 .name("sub")
109 .alias("s")
110 .action(sub_action)
111 .local_flag(Flag::new_bool("sort").short_alias('s'))
112}7fn main() {
8 let _r = preset_root!(act)
9 .usage(env!("CARGO_PKG_NAME").to_string() + " [args]")
10 .common_flag(
11 Flag::new_bool("help")
12 .short_alias('h')
13 .description("show help"),
14 )
15 .local_flag(
16 Flag::new_bool("local")
17 .short_alias('l')
18 //.alias("test")
19 .description("local flag"),
20 )
21 .sub_command(
22 Command::with_name("sub")
23 .description("sub description")
24 .action(sub_act)
25 .usage("sub usage")
26 .local_flag(Flag::new_bool("sflag").description("sub local flag"))
27 .sub_command(Command::with_name("leaf").description("leaf description")),
28 )
29 .sub_command(
30 Command::with_name("help")
31 .description("show help")
32 .action(help_action),
33 )
34 .sub_command(
35 Command::with_name("help2")
36 .description("show help2")
37 .action(help_command_action!(help_tablize_with_alias_dedup)),
38 )
39 .run_from_args(env::args().collect());
40}Set command’s authors
Sourcepub fn local_flag(self, flag: Flag) -> Self
pub fn local_flag(self, flag: Flag) -> Self
Add a local flag to command
Examples found in repository?
More examples
16fn root_command() -> Command {
17 Command::with_name("multi")
18 .description("root command sample: arg printer")
19 .usage(presets::usage("multi"))
20 .common_flag(Flag::new_bool("help").short_alias('h'))
21 .common_flag(Flag::new_bool("reverse").short_alias('r'))
22 .local_flag(
23 Flag::new_bool("by-char")
24 .short_alias('c')
25 .description("process at char units"),
26 )
27 .action(print_args)
28 .sub_command(add_command())
29 .sub_command(sub_command())
30}
31fn call_help(c: &Context, cur_cmd: &Command) -> Result<ActionResult, ActionError> {
32 println!("{}", presets::func::help(cur_cmd, c));
33 done!()
34}
35fn print_args(current_command: Command, context: Context) -> Result<ActionResult, ActionError> {
36 if called_help(&context, ¤t_command) {
37 return call_help(&context, ¤t_command);
38 }
39 let r: bool =
40 context.get_flag_value_of("reverse", ¤t_command) == Some(FlagValue::Bool(true));
41 let c: bool =
42 context.get_flag_value_of("by-char", ¤t_command) == Some(FlagValue::Bool(true));
43 let str = {
44 let str = if r && !c {
45 context
46 .args
47 .iter()
48 .rev()
49 .fold(String::new(), |c, arg| c + arg)
50 } else {
51 context.args.iter().fold(String::new(), |c, arg| c + arg)
52 };
53 if c {
54 str.chars().rev().collect::<String>()
55 } else {
56 str
57 }
58 };
59
60 println!("{str}");
61
62 Ok(ActionResult::Done)
63}
64
65fn called_help(c: &Context, cc: &Command) -> bool {
66 Some(FlagValue::Bool(true)) == c.get_flag_value_of("help", cc)
67}
68
69fn add_command() -> Command {
70 Command::new()
71 .name("add")
72 .alias("a")
73 .action(add_action)
74 .local_flag(Flag::new_bool("detail").short_alias('d'))
75}
76
77fn add_action(cmd: Command, c: Context) -> Result<ActionResult, ActionError> {
78 if called_help(&c, &cmd) {
79 return call_help(&c, &cmd);
80 }
81 let f = |(str, sum), num: f64| (format!("{str} {num} +"), sum + num);
82 let (mut str, sum): (String, f64) =
83 if c.get_flag_value_of("reverse", &cmd) == Some(FlagValue::Bool(true)) {
84 c.args
85 .iter()
86 .rev()
87 .filter_map(|arg| arg.parse().ok())
88 .fold((String::new(), 0.0), f)
89 } else {
90 c.args
91 .iter()
92 .filter_map(|arg| arg.parse().ok())
93 .fold((String::new(), 0.0), f)
94 };
95 str.pop();
96 str.pop();
97
98 if c.get_flag_value_of("detail", &cmd).unwrap().is_bool_true() {
99 println!("{str} = {sum}");
100 } else {
101 println!("{sum}");
102 }
103 Ok(ActionResult::Done)
104}
105
106fn sub_command() -> Command {
107 Command::new()
108 .name("sub")
109 .alias("s")
110 .action(sub_action)
111 .local_flag(Flag::new_bool("sort").short_alias('s'))
112}6fn main() {
7 let _r = preset_root!(act)
8 .usage(env!("CARGO_PKG_NAME").to_string() + " [args]")
9 .common_flag(
10 Flag::new_bool("help")
11 .short_alias('h')
12 .description("show help"),
13 )
14 .local_flag(
15 Flag::new_bool("local")
16 .short_alias('l')
17 //.alias("test")
18 .description("local flag"),
19 )
20 /* If you want to use help subcommand,uncomment this block and add preset_help_command to use.
21 .sub_command(preset_help_command!(help_tablize_with_alias_dedup))
22 */
23 /* If you want to use subcommand, uncomment this block, then remove this line and the line above sub_act function.
24 .sub_command(
25 Command::with_name("sub")
26 .desctiption("sub description")
27 .action(sub_act)
28 .usage("sub usage")
29 .local_flag(Flag::new_bool("sflag").description("sub local flag")),
30 )*/
31 .run_from_args(env::args().collect());
32}7fn main() {
8 let _r = preset_root!(act)
9 .usage(env!("CARGO_PKG_NAME").to_string() + " [args]")
10 .common_flag(
11 Flag::new_bool("help")
12 .short_alias('h')
13 .description("show help"),
14 )
15 .local_flag(
16 Flag::new_bool("local")
17 .short_alias('l')
18 //.alias("test")
19 .description("local flag"),
20 )
21 .sub_command(
22 Command::with_name("sub")
23 .description("sub description")
24 .action(sub_act)
25 .usage("sub usage")
26 .local_flag(Flag::new_bool("sflag").description("sub local flag"))
27 .sub_command(Command::with_name("leaf").description("leaf description")),
28 )
29 .sub_command(
30 Command::with_name("help")
31 .description("show help")
32 .action(help_action),
33 )
34 .sub_command(
35 Command::with_name("help2")
36 .description("show help2")
37 .action(help_command_action!(help_tablize_with_alias_dedup)),
38 )
39 .run_from_args(env::args().collect());
40}Sourcepub fn local_flags(self, flags: Vec<Flag>) -> Self
pub fn local_flags(self, flags: Vec<Flag>) -> Self
Add a local flags to command
Sourcepub fn common_flag(self, flag: Flag) -> Self
pub fn common_flag(self, flag: Flag) -> Self
Add a common flag to command
Examples found in repository?
16fn root_command() -> Command {
17 Command::with_name("multi")
18 .description("root command sample: arg printer")
19 .usage(presets::usage("multi"))
20 .common_flag(Flag::new_bool("help").short_alias('h'))
21 .common_flag(Flag::new_bool("reverse").short_alias('r'))
22 .local_flag(
23 Flag::new_bool("by-char")
24 .short_alias('c')
25 .description("process at char units"),
26 )
27 .action(print_args)
28 .sub_command(add_command())
29 .sub_command(sub_command())
30}More examples
6fn main() {
7 let _r = preset_root!(act)
8 .usage(env!("CARGO_PKG_NAME").to_string() + " [args]")
9 .common_flag(
10 Flag::new_bool("help")
11 .short_alias('h')
12 .description("show help"),
13 )
14 .local_flag(
15 Flag::new_bool("local")
16 .short_alias('l')
17 //.alias("test")
18 .description("local flag"),
19 )
20 /* If you want to use help subcommand,uncomment this block and add preset_help_command to use.
21 .sub_command(preset_help_command!(help_tablize_with_alias_dedup))
22 */
23 /* If you want to use subcommand, uncomment this block, then remove this line and the line above sub_act function.
24 .sub_command(
25 Command::with_name("sub")
26 .desctiption("sub description")
27 .action(sub_act)
28 .usage("sub usage")
29 .local_flag(Flag::new_bool("sflag").description("sub local flag")),
30 )*/
31 .run_from_args(env::args().collect());
32}7fn main() {
8 let _r = preset_root!(act)
9 .usage(env!("CARGO_PKG_NAME").to_string() + " [args]")
10 .common_flag(
11 Flag::new_bool("help")
12 .short_alias('h')
13 .description("show help"),
14 )
15 .local_flag(
16 Flag::new_bool("local")
17 .short_alias('l')
18 //.alias("test")
19 .description("local flag"),
20 )
21 .sub_command(
22 Command::with_name("sub")
23 .description("sub description")
24 .action(sub_act)
25 .usage("sub usage")
26 .local_flag(Flag::new_bool("sflag").description("sub local flag"))
27 .sub_command(Command::with_name("leaf").description("leaf description")),
28 )
29 .sub_command(
30 Command::with_name("help")
31 .description("show help")
32 .action(help_action),
33 )
34 .sub_command(
35 Command::with_name("help2")
36 .description("show help2")
37 .action(help_command_action!(help_tablize_with_alias_dedup)),
38 )
39 .run_from_args(env::args().collect());
40}Sourcepub fn command_flags(self, flags: Vec<Flag>) -> Self
pub fn command_flags(self, flags: Vec<Flag>) -> Self
Add a common flag to command
Sourcepub fn description<T: Into<String>>(self, description: T) -> Self
pub fn description<T: Into<String>>(self, description: T) -> Self
Set command’s description
Examples found in repository?
16fn root_command() -> Command {
17 Command::with_name("multi")
18 .description("root command sample: arg printer")
19 .usage(presets::usage("multi"))
20 .common_flag(Flag::new_bool("help").short_alias('h'))
21 .common_flag(Flag::new_bool("reverse").short_alias('r'))
22 .local_flag(
23 Flag::new_bool("by-char")
24 .short_alias('c')
25 .description("process at char units"),
26 )
27 .action(print_args)
28 .sub_command(add_command())
29 .sub_command(sub_command())
30}More examples
7fn main() {
8 let _r = preset_root!(act)
9 .usage(env!("CARGO_PKG_NAME").to_string() + " [args]")
10 .common_flag(
11 Flag::new_bool("help")
12 .short_alias('h')
13 .description("show help"),
14 )
15 .local_flag(
16 Flag::new_bool("local")
17 .short_alias('l')
18 //.alias("test")
19 .description("local flag"),
20 )
21 .sub_command(
22 Command::with_name("sub")
23 .description("sub description")
24 .action(sub_act)
25 .usage("sub usage")
26 .local_flag(Flag::new_bool("sflag").description("sub local flag"))
27 .sub_command(Command::with_name("leaf").description("leaf description")),
28 )
29 .sub_command(
30 Command::with_name("help")
31 .description("show help")
32 .action(help_action),
33 )
34 .sub_command(
35 Command::with_name("help2")
36 .description("show help2")
37 .action(help_command_action!(help_tablize_with_alias_dedup)),
38 )
39 .run_from_args(env::args().collect());
40}Sourcepub fn sub_command(self, sub_command: Command) -> Self
pub fn sub_command(self, sub_command: Command) -> Self
Add command’s sub command
Examples found in repository?
16fn root_command() -> Command {
17 Command::with_name("multi")
18 .description("root command sample: arg printer")
19 .usage(presets::usage("multi"))
20 .common_flag(Flag::new_bool("help").short_alias('h'))
21 .common_flag(Flag::new_bool("reverse").short_alias('r'))
22 .local_flag(
23 Flag::new_bool("by-char")
24 .short_alias('c')
25 .description("process at char units"),
26 )
27 .action(print_args)
28 .sub_command(add_command())
29 .sub_command(sub_command())
30}More examples
7fn main() {
8 let _r = preset_root!(act)
9 .usage(env!("CARGO_PKG_NAME").to_string() + " [args]")
10 .common_flag(
11 Flag::new_bool("help")
12 .short_alias('h')
13 .description("show help"),
14 )
15 .local_flag(
16 Flag::new_bool("local")
17 .short_alias('l')
18 //.alias("test")
19 .description("local flag"),
20 )
21 .sub_command(
22 Command::with_name("sub")
23 .description("sub description")
24 .action(sub_act)
25 .usage("sub usage")
26 .local_flag(Flag::new_bool("sflag").description("sub local flag"))
27 .sub_command(Command::with_name("leaf").description("leaf description")),
28 )
29 .sub_command(
30 Command::with_name("help")
31 .description("show help")
32 .action(help_action),
33 )
34 .sub_command(
35 Command::with_name("help2")
36 .description("show help2")
37 .action(help_command_action!(help_tablize_with_alias_dedup)),
38 )
39 .run_from_args(env::args().collect());
40}Sourcepub fn sub_commands(self, sub_commands: Vec<Command>) -> Self
pub fn sub_commands(self, sub_commands: Vec<Command>) -> Self
Add sub commands
Sourcepub fn alias<T: Into<String>>(self, a: T) -> Self
pub fn alias<T: Into<String>>(self, a: T) -> Self
Add command’s alias
Examples found in repository?
69fn add_command() -> Command {
70 Command::new()
71 .name("add")
72 .alias("a")
73 .action(add_action)
74 .local_flag(Flag::new_bool("detail").short_alias('d'))
75}
76
77fn add_action(cmd: Command, c: Context) -> Result<ActionResult, ActionError> {
78 if called_help(&c, &cmd) {
79 return call_help(&c, &cmd);
80 }
81 let f = |(str, sum), num: f64| (format!("{str} {num} +"), sum + num);
82 let (mut str, sum): (String, f64) =
83 if c.get_flag_value_of("reverse", &cmd) == Some(FlagValue::Bool(true)) {
84 c.args
85 .iter()
86 .rev()
87 .filter_map(|arg| arg.parse().ok())
88 .fold((String::new(), 0.0), f)
89 } else {
90 c.args
91 .iter()
92 .filter_map(|arg| arg.parse().ok())
93 .fold((String::new(), 0.0), f)
94 };
95 str.pop();
96 str.pop();
97
98 if c.get_flag_value_of("detail", &cmd).unwrap().is_bool_true() {
99 println!("{str} = {sum}");
100 } else {
101 println!("{sum}");
102 }
103 Ok(ActionResult::Done)
104}
105
106fn sub_command() -> Command {
107 Command::new()
108 .name("sub")
109 .alias("s")
110 .action(sub_action)
111 .local_flag(Flag::new_bool("sort").short_alias('s'))
112}Sourcepub fn is(&self, name_or_alias: &str) -> bool
pub fn is(&self, name_or_alias: &str) -> bool
Returns true if name_or_alias matches command’s name or one of alias at least
name_or_aliasがコマンド名かエイリアスのうち少なくとも一つにマッチした場合trueを返す
Sourcepub fn take_sub(&mut self, name_or_alias: &str) -> Option<Command>
pub fn take_sub(&mut self, name_or_alias: &str) -> Option<Command>
Take sub command matches name_or_alias.
name_or_aliasに一致するサブコマンドがある場合、保持しているVectorからswap_removeで取り出して返す
Sourcepub fn get_mut_sub(&mut self, name_or_alias: &str) -> Option<&mut Command>
pub fn get_mut_sub(&mut self, name_or_alias: &str) -> Option<&mut Command>
Gets sub command mutable reference matches name_or_alias.
Sourcepub fn derive_route_init_vector(&self) -> Vector<String>
pub fn derive_route_init_vector(&self) -> Vector<String>
Returns init Vector for Context’s route
Source§impl Command
impl Command
Sourcepub fn run_from_args(
self,
raw_args: Vec<String>,
) -> Result<ActionResult, ActionError>
pub fn run_from_args( self, raw_args: Vec<String>, ) -> Result<ActionResult, ActionError>
Run commands with raw_args
Examples found in repository?
More examples
6fn main() {
7 let _r = preset_root!(act)
8 .usage(env!("CARGO_PKG_NAME").to_string() + " [args]")
9 .common_flag(
10 Flag::new_bool("help")
11 .short_alias('h')
12 .description("show help"),
13 )
14 .local_flag(
15 Flag::new_bool("local")
16 .short_alias('l')
17 //.alias("test")
18 .description("local flag"),
19 )
20 /* If you want to use help subcommand,uncomment this block and add preset_help_command to use.
21 .sub_command(preset_help_command!(help_tablize_with_alias_dedup))
22 */
23 /* If you want to use subcommand, uncomment this block, then remove this line and the line above sub_act function.
24 .sub_command(
25 Command::with_name("sub")
26 .desctiption("sub description")
27 .action(sub_act)
28 .usage("sub usage")
29 .local_flag(Flag::new_bool("sflag").description("sub local flag")),
30 )*/
31 .run_from_args(env::args().collect());
32}7fn main() {
8 let _r = preset_root!(act)
9 .usage(env!("CARGO_PKG_NAME").to_string() + " [args]")
10 .common_flag(
11 Flag::new_bool("help")
12 .short_alias('h')
13 .description("show help"),
14 )
15 .local_flag(
16 Flag::new_bool("local")
17 .short_alias('l')
18 //.alias("test")
19 .description("local flag"),
20 )
21 .sub_command(
22 Command::with_name("sub")
23 .description("sub description")
24 .action(sub_act)
25 .usage("sub usage")
26 .local_flag(Flag::new_bool("sflag").description("sub local flag"))
27 .sub_command(Command::with_name("leaf").description("leaf description")),
28 )
29 .sub_command(
30 Command::with_name("help")
31 .description("show help")
32 .action(help_action),
33 )
34 .sub_command(
35 Command::with_name("help2")
36 .description("show help2")
37 .action(help_command_action!(help_tablize_with_alias_dedup)),
38 )
39 .run_from_args(env::args().collect());
40}Sourcepub fn run_with_context(
self,
context: Context,
) -> Result<ActionResult, ActionError>
pub fn run_with_context( self, context: Context, ) -> Result<ActionResult, ActionError>
Run command with context
Sourcepub fn assign_context(
self,
c: Context,
p: Parser,
inter_mediate_args: VecDeque<MiddleArg>,
last: MiddleArg,
) -> Result<ActionResult, ActionError>
pub fn assign_context( self, c: Context, p: Parser, inter_mediate_args: VecDeque<MiddleArg>, last: MiddleArg, ) -> Result<ActionResult, ActionError>
Assign context to sub command or self own action. コンテキストのargsを見てもサブコマンド行きかコマンドでそのまま処理すればいいか分からなかった時の処理用
Sourcepub fn assign_run(
self,
args: VecDeque<String>,
inter_mediate_args: VecDeque<MiddleArg>,
p: Parser,
raw_args: Vec<String>,
exe_path: String,
last: MiddleArg,
) -> Result<ActionResult, ActionError>
pub fn assign_run( self, args: VecDeque<String>, inter_mediate_args: VecDeque<MiddleArg>, p: Parser, raw_args: Vec<String>, exe_path: String, last: MiddleArg, ) -> Result<ActionResult, ActionError>
Assign subcomannd’s run or command’s own action with no context
コンテキストが生成されていないときに、run_from_args内で第一引数からサブコマンドかそうでないか分からなかった時に再帰処理を行って割り当てを行う関数
Sourcepub fn handle_sub_result(
self,
req: Result<ActionResult, ActionError>,
) -> Result<ActionResult, ActionError>
pub fn handle_sub_result( self, req: Result<ActionResult, ActionError>, ) -> Result<ActionResult, ActionError>
Handle sub action’s result (Result<ActionResult, ActionError>).
Implemented: at ParentActionRequest and Err
アクションの結果であるResult<ActionResult, ActionError>をハンドルする関数。現在はParentActionRequestのハンドリング、もしくはエラー表示のみ