1 use clap::{App, AppSettings, Arg};
2 
main()3 fn main() {
4     let matches = App::new("pacman")
5         .about("package manager utility")
6         .version("5.2.1")
7         .setting(AppSettings::SubcommandRequiredElseHelp)
8         .author("Pacman Development Team")
9         // Query subcommand
10         //
11         // Only a few of its arguments are implemented below.
12         .subcommand(
13             App::new("query")
14                 .short_flag('Q')
15                 .long_flag("query")
16                 .about("Query the package database.")
17                 .arg(
18                     Arg::new("search")
19                         .short('s')
20                         .long("search")
21                         .help("search locally installed packages for matching strings")
22                         .conflicts_with("info")
23                         .takes_value(true)
24                         .multiple_values(true),
25                 )
26                 .arg(
27                     Arg::new("info")
28                         .long("info")
29                         .short('i')
30                         .conflicts_with("search")
31                         .help("view package information")
32                         .takes_value(true)
33                         .multiple_values(true),
34                 ),
35         )
36         // Sync subcommand
37         //
38         // Only a few of its arguments are implemented below.
39         .subcommand(
40             App::new("sync")
41                 .short_flag('S')
42                 .long_flag("sync")
43                 .about("Synchronize packages.")
44                 .arg(
45                     Arg::new("search")
46                         .short('s')
47                         .long("search")
48                         .conflicts_with("info")
49                         .takes_value(true)
50                         .multiple_values(true)
51                         .help("search remote repositories for matching strings"),
52                 )
53                 .arg(
54                     Arg::new("info")
55                         .long("info")
56                         .conflicts_with("search")
57                         .short('i')
58                         .help("view package information"),
59                 )
60                 .arg(
61                     Arg::new("package")
62                         .help("packages")
63                         .required_unless_present("search")
64                         .takes_value(true)
65                         .multiple_values(true),
66                 ),
67         )
68         .get_matches();
69 
70     match matches.subcommand() {
71         Some(("sync", sync_matches)) => {
72             if sync_matches.is_present("search") {
73                 let packages: Vec<_> = sync_matches.values_of("search").unwrap().collect();
74                 let values = packages.join(", ");
75                 println!("Searching for {}...", values);
76                 return;
77             }
78 
79             let packages: Vec<_> = sync_matches.values_of("package").unwrap().collect();
80             let values = packages.join(", ");
81 
82             if sync_matches.is_present("info") {
83                 println!("Retrieving info for {}...", values);
84             } else {
85                 println!("Installing {}...", values);
86             }
87         }
88         Some(("query", query_matches)) => {
89             if let Some(packages) = query_matches.values_of("info") {
90                 let comma_sep = packages.collect::<Vec<_>>().join(", ");
91                 println!("Retrieving info for {}...", comma_sep);
92             } else if let Some(queries) = query_matches.values_of("search") {
93                 let comma_sep = queries.collect::<Vec<_>>().join(", ");
94                 println!("Searching Locally for {}...", comma_sep);
95             } else {
96                 println!("Displaying all locally installed packages...");
97             }
98         }
99         _ => unreachable!(), // If all subcommands are defined above, anything else is unreachable
100     }
101 }
102