add auto-generated zsh completion

This commit is contained in:
steven-omaha
2023-02-23 21:47:03 +01:00
parent 7f4f4ba09e
commit dca0895b39
6 changed files with 30 additions and 117 deletions
+1
View File
@@ -27,3 +27,4 @@ serde_derive = "1.0"
serde = "1.0"
pacdef_macros = { path = "../pacdef_macros", version = "0.1" }
clap_complete = "4.1.3"
+1
View File
@@ -5,6 +5,7 @@ use pacdef_macros::Action;
#[derive(Debug, Action)]
pub enum Actions {
Clean,
Completion,
Edit,
Import,
List,
+6 -5
View File
@@ -9,17 +9,18 @@ use crate::core::get_version_string;
/// Build the `pacdef` argument parser, with subcommands for `version`,
/// `group` and `package`.
fn get_arg_parser() -> Command {
pub fn build_cli() -> Command {
let package_cmd = get_package_cmd();
let group_cmd = get_group_cmd();
let version_cmd = Command::new(VERSION).about("show version info");
let completion_cmd = Command::new(COMPLETION).about("generate shell completion");
Command::new("pacdef")
.about("declarative package manager for Linux")
.version(get_version_string())
.subcommand_required(true)
.arg_required_else_help(true)
.subcommands([package_cmd, group_cmd, version_cmd])
.subcommands([package_cmd, group_cmd, version_cmd, completion_cmd])
}
/// Build the `pacdef group` subcommand.
@@ -67,7 +68,7 @@ fn get_group_cmd() -> Command {
Command::new("group")
.arg_required_else_help(true)
.about("TODO????")
.about("manage groups")
.visible_alias("g")
.subcommand_required(true)
.subcommands([edit, import, list, new, remove, show])
@@ -95,7 +96,7 @@ fn get_package_cmd() -> Command {
Command::new("package")
.arg_required_else_help(true)
.about("TODO????")
.about("manage packages")
.visible_alias("p")
.subcommand_required(true)
.subcommands([clean, review, unmanaged, search, sync])
@@ -104,7 +105,7 @@ fn get_package_cmd() -> Command {
/// Get and parse the CLI arguments.
#[must_use]
pub fn get() -> clap::ArgMatches {
get_arg_parser().get_matches()
build_cli().get_matches()
}
/// For each file argument, return the absolute path to the file.
+12
View File
@@ -5,6 +5,7 @@ use std::path::Path;
use anyhow::{anyhow, bail, ensure, Context, Result};
use clap::ArgMatches;
use clap_complete::{generate, shells::Zsh};
use const_format::formatcp;
use crate::action::*;
@@ -84,6 +85,7 @@ impl Pacdef {
},
Some((VERSION, _)) => Ok(self.show_version()),
Some((COMPLETION, _)) => Ok(self.generate_shell_completion()),
Some((_, _)) => panic!("{ACTION_NOT_MATCHED}"),
None => unreachable!("{UNREACHABLE_ARM}"),
@@ -299,6 +301,16 @@ impl Pacdef {
Ok(())
}
#[allow(clippy::unused_self)]
fn generate_shell_completion(&self) {
generate(
Zsh,
&mut crate::args::build_cli(),
"pacdef",
&mut std::io::stdout(),
);
}
}
/// For the provided CLI arguments, get the path to each corresponding group file.