mirror of
https://github.com/clap-rs/clap
synced 2024-12-14 06:42:33 +00:00
Merge pull request #2407 from logansquirel/master
api: Add clap_generate::Shell enum
This commit is contained in:
commit
7442de7d9a
2 changed files with 58 additions and 0 deletions
|
@ -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.
|
||||
///
|
||||
|
|
54
clap_generate/src/shell.rs
Normal file
54
clap_generate/src/shell.rs
Normal 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]",
|
||||
)),
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue