refactor: Move shell completion into separate function
+use std::path::PathBuf;
+
use anyhow::{bail, Context, Result};
use clap::{IntoApp, Parser};
use clap_complete::{generate_to, shells};
@@ -26,24 +28,13 @@ async fn main() -> Result<()> {
// Parse commandline options.
let opt = CliArguments::parse();
+ // In case the user requested the generation of shell completion file, create it and exit.
if let Some(SubCommand::Completions {
shell,
output_directory,
}) = &opt.cmd
{
- let mut app = CliArguments::command();
- app.set_bin_name("pueue");
- let completion_result = match shell {
- Shell::Bash => generate_to(shells::Bash, &mut app, "pueue", output_directory),
- Shell::Elvish => generate_to(shells::Elvish, &mut app, "pueue", output_directory),
- Shell::Fish => generate_to(shells::Fish, &mut app, "pueue", output_directory),
- Shell::PowerShell => {
- generate_to(shells::PowerShell, &mut app, "pueue", output_directory)
- }
- Shell::Zsh => generate_to(shells::Zsh, &mut app, "pueue", output_directory),
- };
- completion_result.context(format!("Failed to generate completions for {shell:?}"))?;
- return Ok(());
+ return create_shell_completion_file(shell, output_directory);
}
// Init the logger and set the verbosity level depending on the `-v` flags.
@@ -88,3 +79,21 @@ async fn main() -> Result<()> {
Ok(())
}
+
+/// [clap] is capable of creating auto-generated shell completion files.
+/// This function creates such a file for one of the supported shells and puts it into the
+/// specified output directory.
+fn create_shell_completion_file(shell: &Shell, output_directory: &PathBuf) -> Result<()> {
+ let mut app = CliArguments::command();
+ app.set_bin_name("pueue");
+ let completion_result = match shell {
+ Shell::Bash => generate_to(shells::Bash, &mut app, "pueue", output_directory),
+ Shell::Elvish => generate_to(shells::Elvish, &mut app, "pueue", output_directory),
+ Shell::Fish => generate_to(shells::Fish, &mut app, "pueue", output_directory),
+ Shell::PowerShell => generate_to(shells::PowerShell, &mut app, "pueue", output_directory),
+ Shell::Zsh => generate_to(shells::Zsh, &mut app, "pueue", output_directory),
+ };
+ completion_result.context(format!("Failed to generate completions for {shell:?}"))?;
+
+ Ok(())
+}
