2020-06-13 16:28:10 +00:00
|
|
|
// Working with flag subcommands allows behavior similar to the popular Archlinux package manager Pacman.
|
|
|
|
// Man page: https://jlk.fjfi.cvut.cz/arch/manpages/man/pacman.8
|
|
|
|
//
|
|
|
|
// It's suggested that you read examples/20_subcommands.rs prior to learning about `FlagSubCommand`s
|
|
|
|
//
|
2020-06-17 00:01:19 +00:00
|
|
|
// This differs from normal subcommands because it allows passing subcommands in the same fashion as `clap::Arg` in short or long args.
|
2020-06-13 16:28:10 +00:00
|
|
|
//
|
|
|
|
// Top Level App (pacman) TOP
|
|
|
|
// |
|
|
|
|
// ---------------------------------------------------
|
|
|
|
// / | | | | \ \
|
|
|
|
// sync database remove files query deptest upgrade LEVEL 1
|
|
|
|
//
|
|
|
|
// Given the above hierachy, valid runtime uses would be (not an all inclusive list):
|
|
|
|
//
|
|
|
|
// $ pacman -Ss
|
|
|
|
// ^--- subcommand followed by an arg in its scope.
|
|
|
|
//
|
|
|
|
// $ pacman -Qs
|
|
|
|
//
|
|
|
|
// $ pacman -Rns
|
|
|
|
//
|
2020-06-17 00:01:19 +00:00
|
|
|
// NOTE: Subcommands short flags can be uppercase or lowercase.
|
2020-06-13 16:28:10 +00:00
|
|
|
//
|
|
|
|
// $ pacman --sync --search
|
|
|
|
// ^--- subcommand
|
|
|
|
//
|
|
|
|
// $ pacman sync -s
|
|
|
|
// ^--- subcommand
|
|
|
|
//
|
2020-06-17 00:01:19 +00:00
|
|
|
// NOTE: this isn't valid for pacman, but is done implicitly by Clap which
|
2020-06-13 16:28:10 +00:00
|
|
|
// adds support for both flags and standard subcommands out of the box.
|
|
|
|
// Allowing your users to make the choice of what feels more intuitive for them.
|
|
|
|
//
|
|
|
|
// Notice only one command per "level" may be used. You could not, for example, do:
|
|
|
|
//
|
|
|
|
// $ pacman -SQR
|
|
|
|
//
|
|
|
|
// It's also important to know that subcommands each have their own set of matches and may have args
|
|
|
|
// with the same name as other subcommands in a different part of the tree heirachy (i.e. the arg
|
|
|
|
// names aren't in a flat namespace).
|
|
|
|
//
|
|
|
|
// In order to use subcommands in clap, you only need to know which subcommand you're at in your
|
|
|
|
// tree, and which args are defined on that subcommand.
|
|
|
|
//
|
|
|
|
// Let's make a quick program to illustrate. We'll be using the same example as above but for
|
|
|
|
// brevity sake we won't implement all of the subcommands, only a few.
|
2020-06-16 01:52:36 +00:00
|
|
|
use clap::{App, AppSettings, Arg};
|
2020-06-13 16:28:10 +00:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let matches = App::new("pacman")
|
|
|
|
.about("package manager utility")
|
|
|
|
.version("5.2.1")
|
|
|
|
.setting(AppSettings::SubcommandRequiredElseHelp)
|
|
|
|
.author("Pacman Development Team")
|
|
|
|
// Query subcommand
|
|
|
|
//
|
|
|
|
// Only a few of its arguments are implemented below.
|
|
|
|
.subcommand(
|
2020-06-16 01:52:36 +00:00
|
|
|
App::new("query")
|
|
|
|
.short_flag('Q')
|
|
|
|
.long_flag("query")
|
2020-06-13 16:28:10 +00:00
|
|
|
.about("Query the package database.")
|
|
|
|
.arg(
|
|
|
|
Arg::new("search")
|
|
|
|
.short('s')
|
|
|
|
.long("search")
|
|
|
|
.about("search locally-installed packages for matching strings")
|
2020-07-08 01:26:38 +00:00
|
|
|
.conflicts_with("info")
|
|
|
|
.multiple_values(true),
|
2020-06-13 16:28:10 +00:00
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::new("info")
|
|
|
|
.long("info")
|
|
|
|
.short('i')
|
2020-07-08 01:26:38 +00:00
|
|
|
.conflicts_with("search")
|
|
|
|
.about("view package information")
|
|
|
|
.multiple_values(true),
|
2020-06-13 16:28:10 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
// Sync subcommand
|
|
|
|
//
|
|
|
|
// Only a few of its arguments are implemented below.
|
|
|
|
.subcommand(
|
2020-06-16 01:52:36 +00:00
|
|
|
App::new("sync")
|
|
|
|
.short_flag('S')
|
|
|
|
.long_flag("sync")
|
2020-06-13 16:28:10 +00:00
|
|
|
.about("Synchronize packages.")
|
|
|
|
.arg(
|
|
|
|
Arg::new("search")
|
|
|
|
.short('s')
|
|
|
|
.long("search")
|
2020-07-08 01:26:38 +00:00
|
|
|
.conflicts_with("info")
|
|
|
|
.takes_value(true)
|
|
|
|
.multiple_values(true)
|
|
|
|
.about("search remote repositories for matching strings"),
|
2020-06-13 16:28:10 +00:00
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::new("info")
|
|
|
|
.long("info")
|
2020-07-08 01:26:38 +00:00
|
|
|
.conflicts_with("search")
|
2020-06-13 16:28:10 +00:00
|
|
|
.short('i')
|
2020-07-08 01:26:38 +00:00
|
|
|
.about("view package information"),
|
2020-06-13 16:28:10 +00:00
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::new("package")
|
2020-07-08 01:26:38 +00:00
|
|
|
.about("packages")
|
2020-06-13 16:28:10 +00:00
|
|
|
.multiple(true)
|
2020-07-08 01:26:38 +00:00
|
|
|
.required_unless_one(&["search"])
|
2020-06-13 16:28:10 +00:00
|
|
|
.takes_value(true),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.get_matches();
|
|
|
|
|
|
|
|
match matches.subcommand() {
|
|
|
|
("sync", Some(sync_matches)) => {
|
2020-07-08 01:26:38 +00:00
|
|
|
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(", ");
|
|
|
|
|
2020-06-13 16:28:10 +00:00
|
|
|
if sync_matches.is_present("info") {
|
2020-07-08 01:26:38 +00:00
|
|
|
println!("Retrieving info for {}...", values);
|
2020-06-13 16:28:10 +00:00
|
|
|
} else {
|
2020-07-08 01:26:38 +00:00
|
|
|
println!("Installing {}...", values);
|
2020-06-13 16:28:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
("query", Some(query_matches)) => {
|
2020-07-08 01:26:38 +00:00
|
|
|
if let Some(packages) = query_matches.values_of("info") {
|
|
|
|
let comma_sep = packages.collect::<Vec<_>>().join(", ");
|
2020-06-17 00:01:19 +00:00
|
|
|
println!("Retrieving info for {}...", comma_sep);
|
2020-07-08 01:26:38 +00:00
|
|
|
} else if let Some(queries) = query_matches.values_of("search") {
|
|
|
|
let comma_sep = queries.collect::<Vec<_>>().join(", ");
|
2020-06-17 00:01:19 +00:00
|
|
|
println!("Searching Locally for {}...", comma_sep);
|
2020-06-13 16:28:10 +00:00
|
|
|
} else {
|
|
|
|
println!("Displaying all locally installed packages...");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => unreachable!(), // If all subcommands are defined above, anything else is unreachable
|
|
|
|
}
|
|
|
|
}
|