single/
single.rs

1use combu::{ActionError, ActionResult, Command, Context, Flag, FlagValue};
2use std::env;
3
4/*
5single command example.
6We can run as follows:
7```bash
8$ cargo run --example single a b c d e
9abcde
10$ cargo run --example single a b c d e -r
11edcba
12```
13 */
14
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}
21
22fn act(cmd: Command, c: Context) -> Result<ActionResult, ActionError> {
23	let r = c.get_flag_value_of("reverse", &cmd).unwrap();
24
25	println!(
26		"{}",
27		match r {
28			FlagValue::Bool(true) => {
29				c.args
30					.iter()
31					.rev()
32					.fold(String::new(), |concated, arg| concated + arg)
33			}
34			_ => {
35				c.args
36					.iter()
37					.fold(String::new(), |concated, arg| concated + arg)
38			}
39		}
40	);
41	Ok(ActionResult::Done)
42}