From 0582580ec67cdc9704fba94f9eb9fd9e91f4b8de Mon Sep 17 00:00:00 2001 From: Ross MacArthur Date: Sun, 25 Aug 2024 16:01:21 +0200 Subject: [PATCH] Support all clap complete shells Fixes #187 --- completions/sheldon.bash | 6 +-- completions/sheldon.zsh | 6 +-- src/cli/mod.rs | 2 - src/cli/raw.rs | 41 ++++++++++++++----- src/cli/testdata/raw_opt_help.golden | 2 +- src/cli/testdata/raw_opt_init_help.golden | 2 +- .../raw_opt_subcommand_required.golden | 2 +- src/cli/tests.rs | 2 +- 8 files changed, 40 insertions(+), 23 deletions(-) diff --git a/completions/sheldon.bash b/completions/sheldon.bash index c4f0cd2b..66a07924 100644 --- a/completions/sheldon.bash +++ b/completions/sheldon.bash @@ -50,7 +50,7 @@ _sheldon() { fi case "${prev}" in --color) - COMPREPLY=($(compgen -f "${cur}")) + COMPREPLY=($(compgen -W "auto always never" -- "${cur}")) return 0 ;; --config-dir) @@ -154,7 +154,7 @@ _sheldon() { fi case "${prev}" in --shell) - COMPREPLY=($(compgen -f "${cur}")) + COMPREPLY=($(compgen -W "bash elvish fish powershell zsh" -- "${cur}")) return 0 ;; *) @@ -186,7 +186,7 @@ _sheldon() { fi case "${prev}" in --shell) - COMPREPLY=($(compgen -f "${cur}")) + COMPREPLY=($(compgen -W "bash fish zsh" -- "${cur}")) return 0 ;; *) diff --git a/completions/sheldon.zsh b/completions/sheldon.zsh index d3218cb1..76dd0bd2 100644 --- a/completions/sheldon.zsh +++ b/completions/sheldon.zsh @@ -15,7 +15,7 @@ _sheldon() { local context curcontext="$curcontext" state line _arguments "${_arguments_options[@]}" : \ -'--color=[Output coloring\: always, auto, or never]:WHEN: ' \ +'--color=[Output coloring]:WHEN:(auto always never)' \ '--config-dir=[The configuration directory]:PATH:_files' \ '--data-dir=[The data directory]:PATH:_files' \ '--config-file=[The config file]:PATH:_files' \ @@ -40,7 +40,7 @@ _sheldon() { case $line[1] in (init) _arguments "${_arguments_options[@]}" : \ -'--shell=[The type of shell, accepted values are\: bash, fish, zsh]:SHELL: ' \ +'--shell=[The type of shell]:SHELL:(bash fish zsh)' \ '-h[Print help]' \ '--help[Print help]' \ && ret=0 @@ -98,7 +98,7 @@ _arguments "${_arguments_options[@]}" : \ ;; (completions) _arguments "${_arguments_options[@]}" : \ -'--shell=[The type of shell, accepted values are\: bash, zsh]:SHELL: ' \ +'--shell=[The type of shell]:SHELL:(bash elvish fish powershell zsh)' \ '-h[Print help]' \ '--help[Print help]' \ && ret=0 diff --git a/src/cli/mod.rs b/src/cli/mod.rs index a3eb2951..0f7d2840 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -13,7 +13,6 @@ use std::process; use anyhow::{anyhow, Context as ResultExt, Result}; use clap::{CommandFactory, Parser}; -use clap_complete as complete; use crate::cli::raw::{Add, RawCommand, RawOpt}; use crate::config::{EditPlugin, GitReference, RawPlugin, Shell}; @@ -98,7 +97,6 @@ impl Opt { Command::Source } RawCommand::Completions { shell } => { - let shell = complete::Shell::from(shell); let mut app = RawOpt::command(); clap_complete::generate(shell, &mut app, build::CRATE_NAME, &mut io::stdout()); process::exit(0); diff --git a/src/cli/raw.rs b/src/cli/raw.rs index 8d8b6155..7724eaaf 100644 --- a/src/cli/raw.rs +++ b/src/cli/raw.rs @@ -2,6 +2,7 @@ use std::path::PathBuf; +use clap::builder::PossibleValue; use clap::{ArgGroup, Parser}; use clap_complete as complete; use url::Url; @@ -44,7 +45,7 @@ pub struct RawOpt { #[clap(long, short)] pub verbose: bool, - /// Output coloring: always, auto, or never. + /// Output coloring. #[clap(long, value_name = "WHEN", default_value_t)] pub color: ColorChoice, @@ -73,7 +74,7 @@ pub struct RawOpt { pub enum RawCommand { /// Initialize a new config file. Init { - /// The type of shell, accepted values are: bash, fish, zsh. + /// The type of shell. #[clap(long, value_name = "SHELL")] shell: Option, }, @@ -119,9 +120,9 @@ pub enum RawCommand { /// Generate completions for the given shell. Completions { - /// The type of shell, accepted values are: bash, zsh. + /// The type of shell. #[clap(long, value_name = "SHELL")] - shell: Shell, + shell: complete::Shell, }, /// Prints detailed version information. @@ -195,13 +196,31 @@ pub struct Add { pub hooks: Option>, } -impl From for complete::Shell { - fn from(s: Shell) -> Self { - match s { - Shell::Bash => complete::Shell::Bash, - Shell::Fish => complete::Shell::Fish, - Shell::Zsh => complete::Shell::Zsh, - } +impl clap::ValueEnum for ColorChoice { + fn value_variants<'a>() -> &'a [Self] { + &[ColorChoice::Auto, ColorChoice::Always, ColorChoice::Never] + } + + fn to_possible_value(&self) -> Option { + Some(match self { + ColorChoice::Auto => PossibleValue::new("auto"), + ColorChoice::Always => PossibleValue::new("always"), + ColorChoice::Never => PossibleValue::new("never"), + }) + } +} + +impl clap::ValueEnum for Shell { + fn value_variants<'a>() -> &'a [Self] { + &[Shell::Bash, Shell::Fish, Shell::Zsh] + } + + fn to_possible_value(&self) -> Option { + Some(match self { + Shell::Bash => PossibleValue::new("bash"), + Shell::Fish => PossibleValue::new("fish"), + Shell::Zsh => PossibleValue::new("zsh"), + }) } } diff --git a/src/cli/testdata/raw_opt_help.golden b/src/cli/testdata/raw_opt_help.golden index 80ba8948..15c01684 100644 --- a/src/cli/testdata/raw_opt_help.golden +++ b/src/cli/testdata/raw_opt_help.golden @@ -19,7 +19,7 @@ Options: -q, --quiet Suppress any informational output --non-interactive Suppress any interactive prompts and assume "yes" as the answer -v, --verbose Use verbose output - --color Output coloring: always, auto, or never [default: auto] + --color Output coloring [default: auto] [possible values: auto, always, never] --config-dir The configuration directory [env: SHELDON_CONFIG_DIR=] --data-dir The data directory [env: SHELDON_DATA_DIR=] --config-file The config file [env: SHELDON_CONFIG_FILE=] diff --git a/src/cli/testdata/raw_opt_init_help.golden b/src/cli/testdata/raw_opt_init_help.golden index 29a97b60..5a43eb3e 100644 --- a/src/cli/testdata/raw_opt_init_help.golden +++ b/src/cli/testdata/raw_opt_init_help.golden @@ -3,5 +3,5 @@ Initialize a new config file Usage: sheldon init [OPTIONS] Options: - --shell The type of shell, accepted values are: bash, fish, zsh + --shell The type of shell [possible values: bash, fish, zsh] -h, --help Print help diff --git a/src/cli/testdata/raw_opt_subcommand_required.golden b/src/cli/testdata/raw_opt_subcommand_required.golden index 80ba8948..15c01684 100644 --- a/src/cli/testdata/raw_opt_subcommand_required.golden +++ b/src/cli/testdata/raw_opt_subcommand_required.golden @@ -19,7 +19,7 @@ Options: -q, --quiet Suppress any informational output --non-interactive Suppress any interactive prompts and assume "yes" as the answer -v, --verbose Use verbose output - --color Output coloring: always, auto, or never [default: auto] + --color Output coloring [default: auto] [possible values: auto, always, never] --config-dir The configuration directory [env: SHELDON_CONFIG_DIR=] --data-dir The data directory [env: SHELDON_DATA_DIR=] --config-file The config file [env: SHELDON_CONFIG_FILE=] diff --git a/src/cli/tests.rs b/src/cli/tests.rs index 504beea5..89f212c6 100644 --- a/src/cli/tests.rs +++ b/src/cli/tests.rs @@ -140,7 +140,7 @@ fn raw_opt_init_with_invalid_shell() { setup(); assert_eq!( raw_opt_err(&["init", "--shell", "ksh",]).kind(), - ErrorKind::ValueValidation + ErrorKind::InvalidValue ); }