Skip to content

Commit

Permalink
Support all clap complete shells
Browse files Browse the repository at this point in the history
  • Loading branch information
rossmacarthur committed Aug 25, 2024
1 parent d0330e7 commit 2f59189
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 17 deletions.
2 changes: 0 additions & 2 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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);
Expand Down
41 changes: 30 additions & 11 deletions src/cli/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use std::path::PathBuf;

use clap::builder::PossibleValue;
use clap::{ArgGroup, Parser};
use clap_complete as complete;
use url::Url;
Expand Down Expand Up @@ -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,

Expand Down Expand Up @@ -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<Shell>,
},
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -195,13 +196,31 @@ pub struct Add {
pub hooks: Option<Vec<(String, String)>>,
}

impl From<Shell> 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<PossibleValue> {
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<PossibleValue> {
Some(match self {
Shell::Bash => PossibleValue::new("bash"),
Shell::Fish => PossibleValue::new("fish"),
Shell::Zsh => PossibleValue::new("zsh"),
})
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/cli/testdata/raw_opt_help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -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 <WHEN> Output coloring: always, auto, or never [default: auto]
--color <WHEN> Output coloring [default: auto] [possible values: auto, always, never]
--config-dir <PATH> The configuration directory [env: SHELDON_CONFIG_DIR=]
--data-dir <PATH> The data directory [env: SHELDON_DATA_DIR=]
--config-file <PATH> The config file [env: SHELDON_CONFIG_FILE=]
Expand Down
2 changes: 1 addition & 1 deletion src/cli/testdata/raw_opt_init_help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ Initialize a new config file
Usage: sheldon init [OPTIONS]

Options:
--shell <SHELL> The type of shell, accepted values are: bash, fish, zsh
--shell <SHELL> The type of shell [possible values: bash, fish, zsh]
-h, --help Print help
2 changes: 1 addition & 1 deletion src/cli/testdata/raw_opt_subcommand_required.golden
Original file line number Diff line number Diff line change
Expand Up @@ -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 <WHEN> Output coloring: always, auto, or never [default: auto]
--color <WHEN> Output coloring [default: auto] [possible values: auto, always, never]
--config-dir <PATH> The configuration directory [env: SHELDON_CONFIG_DIR=]
--data-dir <PATH> The data directory [env: SHELDON_DATA_DIR=]
--config-file <PATH> The config file [env: SHELDON_CONFIG_FILE=]
Expand Down
2 changes: 1 addition & 1 deletion src/cli/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}

Expand Down

0 comments on commit 2f59189

Please sign in to comment.