cpkg/
cli.rs

1use clap::crate_authors;
2use clap::crate_description;
3use clap::crate_name;
4use clap::crate_version;
5use clap::Arg;
6use clap::Command;
7
8#[must_use]
9pub fn get_command_line() -> Command {
10    Command::new(crate_name!())
11        .about(crate_description!())
12        .author(crate_authors!("\n"))
13        .version(crate_version!())
14        .propagate_version(true)
15        .arg_required_else_help(true)
16        .subcommands([
17            get_install_subcommand(),
18            get_remove_subcommand(),
19            get_reinstall_subcommand(),
20        ])
21}
22
23// Commands
24
25pub const SUBCOMMAND_INSTALL: &str = "install";
26
27#[must_use]
28fn get_install_subcommand() -> Command {
29    Command::new(SUBCOMMAND_INSTALL)
30        .about("Installs given package(s)")
31        .visible_aliases(["add"])
32        .args([
33            get_argument_packages_list(),
34            get_argument_assume_yes(),
35            get_argument_dry_run(),
36        ])
37}
38
39pub const SUBCOMMAND_REMOVE: &str = "remove";
40
41#[must_use]
42pub fn get_remove_subcommand() -> Command {
43    Command::new(SUBCOMMAND_REMOVE)
44        .about("Removes given package(s)")
45        .visible_aliases(["uninstall", "delete"])
46        .args([
47            get_argument_packages_list(),
48            get_argument_assume_yes(),
49            get_argument_dry_run(),
50        ])
51}
52
53pub const SUBCOMMAND_REINSTALL: &str = "reinstall";
54
55#[must_use]
56pub fn get_reinstall_subcommand() -> Command {
57    Command::new(SUBCOMMAND_REINSTALL)
58        .about("Reinstalls given package(s)")
59        .args([
60            get_argument_packages_list(),
61            get_argument_assume_yes(),
62            get_argument_dry_run(),
63        ])
64}
65
66// Arguments
67
68pub const ARGUMENT_ASSUME_YES: &str = "YES";
69
70#[must_use]
71pub fn get_argument_assume_yes() -> Arg {
72    Arg::new(ARGUMENT_ASSUME_YES)
73        .short('y')
74        .long("assume-yes")
75        .visible_alias("yes")
76        .action(clap::ArgAction::SetTrue)
77        .help("Assume yes for all confirmation prompts")
78}
79
80pub const ARGUMENT_DRY_RUN: &str = "DRY_RUN";
81
82#[must_use]
83pub fn get_argument_dry_run() -> Arg {
84    Arg::new(ARGUMENT_DRY_RUN)
85        .short('d')
86        .long("dry-run")
87        .visible_alias("simulate")
88        .action(clap::ArgAction::SetTrue)
89        .help("Only print what would be done rather than actually doing it")
90}
91
92pub const ARGUMENT_PACKAGES: &str = "PACKAGES";
93
94#[must_use]
95pub fn get_argument_packages_list() -> Arg {
96    Arg::new(ARGUMENT_PACKAGES).num_args(1..).required(true)
97}