Merge pull request #2407 from logansquirel/master

api: Add clap_generate::Shell enum
This commit is contained in:
Pavan Kumar Sunkara 2021-03-11 21:24:30 +05:30 committed by GitHub
commit 7442de7d9a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 58 additions and 0 deletions

View file

@ -28,6 +28,8 @@ mod macros;
/// Contains some popular generators
pub mod generators;
/// Contains supported shells for auto-completion scripts
mod shell;
use std::ffi::OsString;
use std::fs::File;
@ -36,6 +38,8 @@ use std::path::PathBuf;
#[doc(inline)]
pub use generators::Generator;
#[doc(inline)]
pub use shell::Shell;
/// Generate a file for a specified generator at compile time.
///

View file

@ -0,0 +1,54 @@
use std::fmt::Display;
use std::str::FromStr;
/// Shell with auto-generated completion script available.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[non_exhaustive]
pub enum Shell {
/// Bourne Again SHell (bash)
Bash,
/// Elvish shell
Elvish,
/// Friendly Interactive SHell (fish)
Fish,
/// PowerShell
PowerShell,
/// Z SHell (zsh)
Zsh,
}
impl Shell {
/// A list of supported shells in `[&'static str]` form.
pub fn variants() -> [&'static str; 5] {
["bash", "elvish", "fish", "powershell", "zsh"]
}
}
impl Display for Shell {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match *self {
Shell::Bash => write!(f, "bash"),
Shell::Elvish => write!(f, "elvish"),
Shell::Fish => write!(f, "fish"),
Shell::PowerShell => write!(f, "powershell"),
Shell::Zsh => write!(f, "zsh"),
}
}
}
impl FromStr for Shell {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_ascii_lowercase().as_str() {
"bash" => Ok(Shell::Bash),
"elvish" => Ok(Shell::Elvish),
"fish" => Ok(Shell::Fish),
"powershell" => Ok(Shell::PowerShell),
"zsh" => Ok(Shell::Zsh),
_ => Err(String::from(
"[valid values: bash, elvish, fish, powershell, zsh]",
)),
}
}
}