| 1 |
use std::fmt::Display; |
| 2 |
use std::io::Error; |
| 3 |
use std::path::Path; |
| 4 |
use std::str::FromStr; |
| 5 |
|
| 6 |
use clap::ValueEnum; |
| 7 |
use clap::builder::PossibleValue; |
| 8 |
|
| 9 |
use crate::Generator; |
| 10 |
use crate::shells; |
| 11 |
|
| 12 |
/// Shell with auto-generated completion script available. |
| 13 |
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] |
| 14 |
#[non_exhaustive] |
| 15 |
pub enum Shell { |
| 16 |
/// Bourne Again `SHell` (bash) |
| 17 |
Bash, |
| 18 |
/// Elvish shell |
| 19 |
Elvish, |
| 20 |
/// Friendly Interactive `SHell` (fish) |
| 21 |
Fish, |
| 22 |
/// `PowerShell` |
| 23 |
PowerShell, |
| 24 |
/// Z `SHell` (zsh) |
| 25 |
Zsh, |
| 26 |
} |
| 27 |
|
| 28 |
impl Display for Shell { |
| 29 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 30 |
self.to_possible_value() |
| 31 |
.expect("no values are skipped") |
| 32 |
.get_name() |
| 33 |
.fmt(f) |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
impl FromStr for Shell { |
| 38 |
type Err = String; |
| 39 |
|
| 40 |
fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 41 |
for variant in Self::value_variants() { |
| 42 |
if variant.to_possible_value().unwrap().matches(s, false) { |
| 43 |
return Ok(*variant); |
| 44 |
} |
| 45 |
} |
| 46 |
Err(format!("invalid variant: {s}")) |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
// Hand-rolled so it can work even when `derive` feature is disabled |
| 51 |
impl ValueEnum for Shell { |
| 52 |
fn value_variants<'a>() -> &'a [Self] { |
| 53 |
&[ |
| 54 |
Self::Bash, |
| 55 |
Self::Elvish, |
| 56 |
Self::Fish, |
| 57 |
Self::PowerShell, |
| 58 |
Self::Zsh, |
| 59 |
] |
| 60 |
} |
| 61 |
|
| 62 |
fn to_possible_value(&self) -> Option<PossibleValue> { |
| 63 |
Some(match self { |
| 64 |
Self::Bash => PossibleValue::new("bash"), |
| 65 |
Self::Elvish => PossibleValue::new("elvish"), |
| 66 |
Self::Fish => PossibleValue::new("fish"), |
| 67 |
Self::PowerShell => PossibleValue::new("powershell"), |
| 68 |
Self::Zsh => PossibleValue::new("zsh"), |
| 69 |
}) |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
impl Generator for Shell { |
| 74 |
fn file_name(&self, name: &str) -> String { |
| 75 |
match self { |
| 76 |
Self::Bash => shells::Bash.file_name(name), |
| 77 |
Self::Elvish => shells::Elvish.file_name(name), |
| 78 |
Self::Fish => shells::Fish.file_name(name), |
| 79 |
Self::PowerShell => shells::PowerShell.file_name(name), |
| 80 |
Self::Zsh => shells::Zsh.file_name(name), |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
fn generate(&self, cmd: &clap::Command, buf: &mut dyn std::io::Write) { |
| 85 |
self.try_generate(cmd, buf) |
| 86 |
.expect("failed to write completion file"); |
| 87 |
} |
| 88 |
|
| 89 |
fn try_generate(&self, cmd: &clap::Command, buf: &mut dyn std::io::Write) -> Result<(), Error> { |
| 90 |
match self { |
| 91 |
Self::Bash => shells::Bash.try_generate(cmd, buf), |
| 92 |
Self::Elvish => shells::Elvish.try_generate(cmd, buf), |
| 93 |
Self::Fish => shells::Fish.try_generate(cmd, buf), |
| 94 |
Self::PowerShell => shells::PowerShell.try_generate(cmd, buf), |
| 95 |
Self::Zsh => shells::Zsh.try_generate(cmd, buf), |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
impl Shell { |
| 101 |
/// Parse a shell from a path to the executable for the shell |
| 102 |
/// |
| 103 |
/// # Examples |
| 104 |
/// |
| 105 |
/// ``` |
| 106 |
/// use clap_complete::shells::Shell; |
| 107 |
/// |
| 108 |
/// assert_eq!(Shell::from_shell_path("/bin/bash"), Some(Shell::Bash)); |
| 109 |
/// assert_eq!(Shell::from_shell_path("/usr/bin/zsh"), Some(Shell::Zsh)); |
| 110 |
/// assert_eq!(Shell::from_shell_path("/usr/bin/pwsh"), Some(Shell::PowerShell)); |
| 111 |
/// assert_eq!(Shell::from_shell_path("/opt/my_custom_shell"), None); |
| 112 |
/// ``` |
| 113 |
pub fn from_shell_path<P: AsRef<Path>>(path: P) -> Option<Self> { |
| 114 |
parse_shell_from_path(path.as_ref()) |
| 115 |
} |
| 116 |
|
| 117 |
/// Determine the user's current shell from the environment |
| 118 |
/// |
| 119 |
/// This will read the SHELL environment variable and try to determine which shell is in use |
| 120 |
/// from that. |
| 121 |
/// |
| 122 |
/// If SHELL is not set, then on windows, it will default to powershell, and on |
| 123 |
/// other operating systems it will return `None`. |
| 124 |
/// |
| 125 |
/// If SHELL is set, but contains a value that doesn't correspond to one of the supported shell |
| 126 |
/// types, then return `None`. |
| 127 |
/// |
| 128 |
/// # Example: |
| 129 |
/// |
| 130 |
/// ```no_run |
| 131 |
/// # use clap::Command; |
| 132 |
/// use clap_complete::{generate, shells::Shell}; |
| 133 |
/// # fn build_cli() -> Command { |
| 134 |
/// # Command::new("compl") |
| 135 |
/// # } |
| 136 |
/// let mut cmd = build_cli(); |
| 137 |
/// generate(Shell::from_env().unwrap_or(Shell::Bash), &mut cmd, "myapp", &mut std::io::stdout()); |
| 138 |
/// ``` |
| 139 |
pub fn from_env() -> Option<Self> { |
| 140 |
if let Some(env_shell) = std::env::var_os("SHELL") { |
| 141 |
Self::from_shell_path(env_shell) |
| 142 |
} else if cfg!(windows) { |
| 143 |
Some(Self::PowerShell) |
| 144 |
} else { |
| 145 |
None |
| 146 |
} |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
// use a separate function to avoid having to monomorphize the entire function due |
| 151 |
// to from_shell_path being generic |
| 152 |
fn parse_shell_from_path(path: &Path) -> Option<Shell> { |
| 153 |
let name = path.file_stem()?.to_str()?; |
| 154 |
match name { |
| 155 |
"bash" => Some(Shell::Bash), |
| 156 |
"zsh" => Some(Shell::Zsh), |
| 157 |
"fish" => Some(Shell::Fish), |
| 158 |
"elvish" => Some(Shell::Elvish), |
| 159 |
// pwsh is the cross-platform executable. |
| 160 |
"pwsh" | "powershell" | "powershell_ise" => Some(Shell::PowerShell), |
| 161 |
_ => None, |
| 162 |
} |
| 163 |
} |