summaryrefslogtreecommitdiffstats
path: root/third_party/rust/clap/examples/pacman.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
commit36d22d82aa202bb199967e9512281e9a53db42c9 (patch)
tree105e8c98ddea1c1e4784a60a5a6410fa416be2de /third_party/rust/clap/examples/pacman.rs
parentInitial commit. (diff)
downloadfirefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz
firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip
Adding upstream version 115.7.0esr.upstream/115.7.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/clap/examples/pacman.rs')
-rw-r--r--third_party/rust/clap/examples/pacman.rs102
1 files changed, 102 insertions, 0 deletions
diff --git a/third_party/rust/clap/examples/pacman.rs b/third_party/rust/clap/examples/pacman.rs
new file mode 100644
index 0000000000..c088a8fe91
--- /dev/null
+++ b/third_party/rust/clap/examples/pacman.rs
@@ -0,0 +1,102 @@
+use clap::{Arg, Command};
+
+fn main() {
+ let matches = Command::new("pacman")
+ .about("package manager utility")
+ .version("5.2.1")
+ .subcommand_required(true)
+ .arg_required_else_help(true)
+ .author("Pacman Development Team")
+ // Query subcommand
+ //
+ // Only a few of its arguments are implemented below.
+ .subcommand(
+ Command::new("query")
+ .short_flag('Q')
+ .long_flag("query")
+ .about("Query the package database.")
+ .arg(
+ Arg::new("search")
+ .short('s')
+ .long("search")
+ .help("search locally installed packages for matching strings")
+ .conflicts_with("info")
+ .takes_value(true)
+ .multiple_values(true),
+ )
+ .arg(
+ Arg::new("info")
+ .long("info")
+ .short('i')
+ .conflicts_with("search")
+ .help("view package information")
+ .takes_value(true)
+ .multiple_values(true),
+ ),
+ )
+ // Sync subcommand
+ //
+ // Only a few of its arguments are implemented below.
+ .subcommand(
+ Command::new("sync")
+ .short_flag('S')
+ .long_flag("sync")
+ .about("Synchronize packages.")
+ .arg(
+ Arg::new("search")
+ .short('s')
+ .long("search")
+ .conflicts_with("info")
+ .takes_value(true)
+ .multiple_values(true)
+ .help("search remote repositories for matching strings"),
+ )
+ .arg(
+ Arg::new("info")
+ .long("info")
+ .conflicts_with("search")
+ .short('i')
+ .help("view package information"),
+ )
+ .arg(
+ Arg::new("package")
+ .help("packages")
+ .required_unless_present("search")
+ .takes_value(true)
+ .multiple_values(true),
+ ),
+ )
+ .get_matches();
+
+ match matches.subcommand() {
+ Some(("sync", sync_matches)) => {
+ if sync_matches.is_present("search") {
+ let packages: Vec<_> = sync_matches.values_of("search").unwrap().collect();
+ let values = packages.join(", ");
+ println!("Searching for {}...", values);
+ return;
+ }
+
+ let packages: Vec<_> = sync_matches.values_of("package").unwrap().collect();
+ let values = packages.join(", ");
+
+ if sync_matches.is_present("info") {
+ println!("Retrieving info for {}...", values);
+ } else {
+ println!("Installing {}...", values);
+ }
+ }
+ Some(("query", query_matches)) => {
+ if let Some(packages) = query_matches.values_of("info") {
+ let comma_sep = packages.collect::<Vec<_>>().join(", ");
+ println!("Retrieving info for {}...", comma_sep);
+ } else if let Some(queries) = query_matches.values_of("search") {
+ let comma_sep = queries.collect::<Vec<_>>().join(", ");
+ println!("Searching Locally for {}...", comma_sep);
+ } else {
+ println!("Displaying all locally installed packages...");
+ }
+ }
+ _ => unreachable!(), // If all subcommands are defined above, anything else is unreachable
+ }
+}