clap/completions/
shell.rs

1#[allow(deprecated, unused_imports)]
2use std::ascii::AsciiExt;
3use std::str::FromStr;
4use std::fmt;
5
6/// Describes which shell to produce a completions file for
7#[cfg_attr(feature = "lints", allow(enum_variant_names))]
8#[derive(Debug, Copy, Clone)]
9pub enum Shell {
10    /// Generates a .bash completion file for the Bourne Again SHell (BASH)
11    Bash,
12    /// Generates a .fish completion file for the Friendly Interactive SHell (fish)
13    Fish,
14    /// Generates a completion file for the Z SHell (ZSH)
15    Zsh,
16    /// Generates a completion file for PowerShell
17    PowerShell,
18    /// Generates a completion file for Elvish
19    Elvish,
20}
21
22impl Shell {
23    /// A list of possible variants in `&'static str` form
24    pub fn variants() -> [&'static str; 5] { ["zsh", "bash", "fish", "powershell", "elvish"] }
25}
26
27impl FromStr for Shell {
28    type Err = String;
29
30    fn from_str(s: &str) -> Result<Self, Self::Err> {
31        match s {
32            "ZSH" | _ if s.eq_ignore_ascii_case("zsh") => Ok(Shell::Zsh),
33            "FISH" | _ if s.eq_ignore_ascii_case("fish") => Ok(Shell::Fish),
34            "BASH" | _ if s.eq_ignore_ascii_case("bash") => Ok(Shell::Bash),
35            "POWERSHELL" | _ if s.eq_ignore_ascii_case("powershell") => Ok(Shell::PowerShell),
36            "ELVISH" | _ if s.eq_ignore_ascii_case("elvish") => Ok(Shell::Elvish),
37            _ => Err(String::from("[valid values: bash, fish, zsh, powershell, elvish]")),
38        }
39    }
40}
41
42impl fmt::Display for Shell {
43    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
44        match *self {
45            Shell::Bash => write!(f, "BASH"),
46            Shell::Fish => write!(f, "FISH"),
47            Shell::Zsh => write!(f, "ZSH"),
48            Shell::PowerShell => write!(f, "POWERSHELL"),
49            Shell::Elvish => write!(f, "ELVISH"),
50        }
51    }
52}