-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsub_cmds.rs
55 lines (51 loc) · 1.33 KB
/
sub_cmds.rs
1
2
3
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use clap::{Parser, Subcommand};
use std::ffi::OsString;
#[derive(Debug, Parser)]
#[command(name = "kitty")]
#[command(about = "A CLI for managing kitties", long_about = None)]
struct Cli {
#[command(subcommand)]
command: Commands,
}
#[derive(Debug, Subcommand)]
enum Commands {
/// Pet a kitty
#[command(arg_required_else_help = true)]
Pet {
/// The kitty to pet
name: String,
},
/// Feed the kitties
Feed {
#[arg(required = true)]
eats: Vec<OsString>,
},
/// Call a kitty over
#[command(arg_required_else_help = true)]
Call {
/// The name to call them
name: String,
},
}
pub fn main() {
let args = Cli::parse();
match args.command {
Commands::Pet { name } => {
println!("Petting {name}");
}
Commands::Feed { eats } => {
let food_n_stuff = eats
.iter()
.enumerate()
.map(|(i, item)| match i {
0 => item.to_str().unwrap().to_string(),
_ => format!(", {}", item.to_str().unwrap()),
})
.collect::<String>();
println!("Feeding the kitties: {}", food_n_stuff);
}
Commands::Call { name } => {
println!("Calling {name} to hang out");
}
}
}