mirror of
https://github.com/clap-rs/clap
synced 2025-01-24 18:34:59 +00:00
8cc164dafb
Another step towards #1041 This isn't the long term type for `PossibleValue::help`, I just wanted to get the lifetime out of the way first before figuring out how help will work.
91 lines
2.5 KiB
Rust
91 lines
2.5 KiB
Rust
use std::fmt::Display;
|
|
use std::str::FromStr;
|
|
|
|
use clap::builder::PossibleValue;
|
|
use clap::ValueEnum;
|
|
|
|
use crate::shells;
|
|
use crate::Generator;
|
|
|
|
/// 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 Display for Shell {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
self.to_possible_value()
|
|
.expect("no values are skipped")
|
|
.get_name()
|
|
.fmt(f)
|
|
}
|
|
}
|
|
|
|
impl FromStr for Shell {
|
|
type Err = String;
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
for variant in Self::value_variants() {
|
|
if variant.to_possible_value().unwrap().matches(s, false) {
|
|
return Ok(*variant);
|
|
}
|
|
}
|
|
Err(format!("Invalid variant: {}", s))
|
|
}
|
|
}
|
|
|
|
// Hand-rolled so it can work even when `derive` feature is disabled
|
|
impl ValueEnum for Shell {
|
|
fn value_variants<'a>() -> &'a [Self] {
|
|
&[
|
|
Shell::Bash,
|
|
Shell::Elvish,
|
|
Shell::Fish,
|
|
Shell::PowerShell,
|
|
Shell::Zsh,
|
|
]
|
|
}
|
|
|
|
fn to_possible_value<'a>(&self) -> Option<PossibleValue> {
|
|
Some(match self {
|
|
Shell::Bash => PossibleValue::new("bash"),
|
|
Shell::Elvish => PossibleValue::new("elvish"),
|
|
Shell::Fish => PossibleValue::new("fish"),
|
|
Shell::PowerShell => PossibleValue::new("powershell"),
|
|
Shell::Zsh => PossibleValue::new("zsh"),
|
|
})
|
|
}
|
|
}
|
|
|
|
impl Generator for Shell {
|
|
fn file_name(&self, name: &str) -> String {
|
|
match self {
|
|
Shell::Bash => shells::Bash.file_name(name),
|
|
Shell::Elvish => shells::Elvish.file_name(name),
|
|
Shell::Fish => shells::Fish.file_name(name),
|
|
Shell::PowerShell => shells::PowerShell.file_name(name),
|
|
Shell::Zsh => shells::Zsh.file_name(name),
|
|
}
|
|
}
|
|
|
|
fn generate(&self, cmd: &clap::Command, buf: &mut dyn std::io::Write) {
|
|
match self {
|
|
Shell::Bash => shells::Bash.generate(cmd, buf),
|
|
Shell::Elvish => shells::Elvish.generate(cmd, buf),
|
|
Shell::Fish => shells::Fish.generate(cmd, buf),
|
|
Shell::PowerShell => shells::PowerShell.generate(cmd, buf),
|
|
Shell::Zsh => shells::Zsh.generate(cmd, buf),
|
|
}
|
|
}
|
|
}
|