clap/examples/pacman.rs

112 lines
4.1 KiB
Rust
Raw Normal View History

use clap::{Arg, ArgAction, Command};
fn main() {
2022-02-12 03:48:29 +00:00
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(
2022-02-12 03:48:29 +00:00
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")
.action(ArgAction::Set)
.num_args(1..),
)
.arg(
Arg::new("info")
.long("info")
.short('i')
.conflicts_with("search")
.help("view package information")
.action(ArgAction::Set)
.num_args(1..),
),
)
// Sync subcommand
//
// Only a few of its arguments are implemented below.
.subcommand(
2022-02-12 03:48:29 +00:00
Command::new("sync")
.short_flag('S')
.long_flag("sync")
.about("Synchronize packages.")
.arg(
Arg::new("search")
.short('s')
.long("search")
.conflicts_with("info")
.action(ArgAction::Set)
.num_args(1..)
.help("search remote repositories for matching strings"),
)
.arg(
Arg::new("info")
.long("info")
.conflicts_with("search")
.short('i')
.action(ArgAction::SetTrue)
.help("view package information"),
)
.arg(
Arg::new("package")
.help("packages")
2020-08-15 17:11:50 +00:00
.required_unless_present("search")
.action(ArgAction::Set)
.num_args(1..),
),
)
.get_matches();
match matches.subcommand() {
2020-08-05 12:36:02 +00:00
Some(("sync", sync_matches)) => {
if sync_matches.contains_id("search") {
2022-05-23 15:50:11 +00:00
let packages: Vec<_> = sync_matches
.get_many::<String>("search")
.expect("contains_id")
2022-05-23 15:50:11 +00:00
.map(|s| s.as_str())
.collect();
let values = packages.join(", ");
println!("Searching for {values}...");
return;
}
2022-05-23 15:50:11 +00:00
let packages: Vec<_> = sync_matches
.get_many::<String>("package")
.expect("is present")
.map(|s| s.as_str())
.collect();
let values = packages.join(", ");
if sync_matches.get_flag("info") {
println!("Retrieving info for {values}...");
} else {
println!("Installing {values}...");
}
}
2020-08-05 12:36:02 +00:00
Some(("query", query_matches)) => {
if let Some(packages) = query_matches.get_many::<String>("info") {
2022-05-23 15:50:11 +00:00
let comma_sep = packages.map(|s| s.as_str()).collect::<Vec<_>>().join(", ");
println!("Retrieving info for {comma_sep}...");
} else if let Some(queries) = query_matches.get_many::<String>("search") {
2022-05-23 15:50:11 +00:00
let comma_sep = queries.map(|s| s.as_str()).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
}
}