]> git.proxmox.com Git - rustc.git/blob - vendor/clap/examples/pacman.rs
New upstream version 1.63.0+dfsg1
[rustc.git] / vendor / clap / examples / pacman.rs
1 use clap::{Arg, ArgAction, Command};
2
3 fn main() {
4 let matches = Command::new("pacman")
5 .about("package manager utility")
6 .version("5.2.1")
7 .subcommand_required(true)
8 .arg_required_else_help(true)
9 .author("Pacman Development Team")
10 // Query subcommand
11 //
12 // Only a few of its arguments are implemented below.
13 .subcommand(
14 Command::new("query")
15 .short_flag('Q')
16 .long_flag("query")
17 .about("Query the package database.")
18 .arg(
19 Arg::new("search")
20 .short('s')
21 .long("search")
22 .help("search locally installed packages for matching strings")
23 .conflicts_with("info")
24 .takes_value(true)
25 .multiple_values(true),
26 )
27 .arg(
28 Arg::new("info")
29 .long("info")
30 .short('i')
31 .conflicts_with("search")
32 .help("view package information")
33 .takes_value(true)
34 .multiple_values(true),
35 ),
36 )
37 // Sync subcommand
38 //
39 // Only a few of its arguments are implemented below.
40 .subcommand(
41 Command::new("sync")
42 .short_flag('S')
43 .long_flag("sync")
44 .about("Synchronize packages.")
45 .arg(
46 Arg::new("search")
47 .short('s')
48 .long("search")
49 .conflicts_with("info")
50 .takes_value(true)
51 .multiple_values(true)
52 .help("search remote repositories for matching strings"),
53 )
54 .arg(
55 Arg::new("info")
56 .long("info")
57 .conflicts_with("search")
58 .short('i')
59 .action(ArgAction::SetTrue)
60 .help("view package information"),
61 )
62 .arg(
63 Arg::new("package")
64 .help("packages")
65 .required_unless_present("search")
66 .takes_value(true)
67 .multiple_values(true),
68 ),
69 )
70 .get_matches();
71
72 match matches.subcommand() {
73 Some(("sync", sync_matches)) => {
74 if sync_matches.contains_id("search") {
75 let packages: Vec<_> = sync_matches
76 .get_many::<String>("search")
77 .expect("contains_id")
78 .map(|s| s.as_str())
79 .collect();
80 let values = packages.join(", ");
81 println!("Searching for {}...", values);
82 return;
83 }
84
85 let packages: Vec<_> = sync_matches
86 .get_many::<String>("package")
87 .expect("is present")
88 .map(|s| s.as_str())
89 .collect();
90 let values = packages.join(", ");
91
92 if *sync_matches
93 .get_one::<bool>("info")
94 .expect("defaulted by clap")
95 {
96 println!("Retrieving info for {}...", values);
97 } else {
98 println!("Installing {}...", values);
99 }
100 }
101 Some(("query", query_matches)) => {
102 if let Some(packages) = query_matches.get_many::<String>("info") {
103 let comma_sep = packages.map(|s| s.as_str()).collect::<Vec<_>>().join(", ");
104 println!("Retrieving info for {}...", comma_sep);
105 } else if let Some(queries) = query_matches.get_many::<String>("search") {
106 let comma_sep = queries.map(|s| s.as_str()).collect::<Vec<_>>().join(", ");
107 println!("Searching Locally for {}...", comma_sep);
108 } else {
109 println!("Displaying all locally installed packages...");
110 }
111 }
112 _ => unreachable!(), // If all subcommands are defined above, anything else is unreachable
113 }
114 }