Skip to main content

Command

Struct Command 

Source
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: String

Command name

§action: Option<Action>

Command action

§authors: String

Command authors

§copyright: String

Command copyright

§license: License

License of command

§description: Option<String>

Command description

§usage: String

Command usage

§l_flags: Vector<Flag>

local flags of command

§c_flags: Vector<Flag>

common flags of command

§alias: Vector<String>

alias for command

§version: String

Command version

§sub: Vector<Command>

container of sub-command

Implementations§

Source§

impl Command

Source

pub fn new() -> Command

Creare new instance of Command

Examples found in repository?
examples/multi.rs (line 70)
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}
Source

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

Source

pub fn with_name<T: Into<String>>(name: T) -> Command

Create new instance of Command with name

Examples found in repository?
examples/single.rs (line 16)
15fn main() {
16	let _ = Command::with_name("single")
17		.action(act)
18		.local_flag(Flag::new_bool("reverse").short_alias('r'))
19		.single_run(env::args().collect::<Vec<String>>());
20}
More examples
Hide additional examples
examples/multi.rs (line 17)
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}
examples/test.rs (line 22)
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}
Source

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

Source

pub fn run_with_auto_arg_collect(self) -> Result<ActionResult, ActionError>

Run command with collecting args automatically

Source

pub fn single_run( self, raw_args: Vec<String>, ) -> Result<ActionResult, ActionError>

Run command as single(do not have sub) command ルートからサブコマンドがないシンプルな状態の時 アクションが登録されていなければサブコマンドがあるかを調査する

Examples found in repository?
examples/single.rs (line 19)
15fn main() {
16	let _ = Command::with_name("single")
17		.action(act)
18		.local_flag(Flag::new_bool("reverse").short_alias('r'))
19		.single_run(env::args().collect::<Vec<String>>());
20}
Source

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

Set Command’s name

Examples found in repository?
examples/multi.rs (line 71)
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}
Source

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

Set command’s usage

Examples found in repository?
examples/multi.rs (line 19)
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
Hide additional examples
examples/quick_start.rs (line 8)
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}
examples/test.rs (line 9)
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}
Source

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

Set command’s action

Examples found in repository?
examples/single.rs (line 17)
15fn main() {
16	let _ = Command::with_name("single")
17		.action(act)
18		.local_flag(Flag::new_bool("reverse").short_alias('r'))
19		.single_run(env::args().collect::<Vec<String>>());
20}
More examples
Hide additional examples
examples/multi.rs (line 27)
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, &current_command) {
37		return call_help(&context, &current_command);
38	}
39	let r: bool =
40		context.get_flag_value_of("reverse", &current_command) == Some(FlagValue::Bool(true));
41	let c: bool =
42		context.get_flag_value_of("by-char", &current_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}
examples/test.rs (line 24)
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}
Source

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

Set command’s authors

Source

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

Set command’s copyright

Source

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

Add a local flag to command

Examples found in repository?
examples/single.rs (line 18)
15fn main() {
16	let _ = Command::with_name("single")
17		.action(act)
18		.local_flag(Flag::new_bool("reverse").short_alias('r'))
19		.single_run(env::args().collect::<Vec<String>>());
20}
More examples
Hide additional examples
examples/multi.rs (lines 22-26)
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, &current_command) {
37		return call_help(&context, &current_command);
38	}
39	let r: bool =
40		context.get_flag_value_of("reverse", &current_command) == Some(FlagValue::Bool(true));
41	let c: bool =
42		context.get_flag_value_of("by-char", &current_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}
examples/quick_start.rs (lines 14-19)
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}
examples/test.rs (lines 15-20)
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}
Source

pub fn local_flags(self, flags: Vec<Flag>) -> Self

Add a local flags to command

Source

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

Add a common flag to command

Examples found in repository?
examples/multi.rs (line 20)
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
Hide additional examples
examples/quick_start.rs (lines 9-13)
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}
examples/test.rs (lines 10-14)
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}
Source

pub fn command_flags(self, flags: Vec<Flag>) -> Self

Add a common flag to command

Source

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

Set command’s description

Examples found in repository?
examples/multi.rs (line 18)
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
Hide additional examples
examples/test.rs (line 23)
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}
Source

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

Set command’s version

Source

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

Add command’s sub command

Examples found in repository?
examples/multi.rs (line 28)
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
Hide additional examples
examples/test.rs (lines 21-28)
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}
Source

pub fn sub_commands(self, sub_commands: Vec<Command>) -> Self

Add sub commands

Source

pub fn license(self, license: License) -> Self

Sets license

Source

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

Add command’s alias

Examples found in repository?
examples/multi.rs (line 72)
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}
Source

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を返す

Source

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で取り出して返す

Source

pub fn get_mut_sub(&mut self, name_or_alias: &str) -> Option<&mut Command>

Gets sub command mutable reference matches name_or_alias.

Source

pub fn has_sub(&self) -> bool

Returns true if this command has sub command(s).

Source

pub fn derive_route_init_vector(&self) -> Vector<String>

Returns init Vector for Context’s route

Source§

impl Command

Source

pub fn run_from_args( self, raw_args: Vec<String>, ) -> Result<ActionResult, ActionError>

Run commands with raw_args

Examples found in repository?
examples/multi.rs (line 13)
12fn main() {
13	let _ = root_command().run_from_args(std::env::args().collect());
14}
More examples
Hide additional examples
examples/quick_start.rs (line 31)
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}
examples/test.rs (line 39)
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}
Source

pub fn run_with_context( self, context: Context, ) -> Result<ActionResult, ActionError>

Run command with context

Source

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を見てもサブコマンド行きかコマンドでそのまま処理すればいいか分からなかった時の処理用

Source

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内で第一引数からサブコマンドかそうでないか分からなかった時に再帰処理を行って割り当てを行う関数

Source

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のハンドリング、もしくはエラー表示のみ

Trait Implementations§

Source§

impl Clone for Command

Source§

fn clone(&self) -> Command

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Command

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Command

Source§

fn default() -> Command

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

impl From<String> for Command

Source§

fn from(name: String) -> Self

Converts to this type from the input type.
Source§

impl Run<Context> for Command

Source§

fn run(self, c: Context) -> Result<ActionResult, ActionError>

run function
Source§

impl Run<Vec<String>> for Command

Source§

fn run(self, args: Vec<String>) -> Result<ActionResult, ActionError>

run function

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.