mirror of
https://github.com/clap-rs/clap
synced 2024-09-20 06:22:06 +00:00
feat(complete): Add dynamic-support shell enum
This commit is contained in:
parent
278ae3ec63
commit
830dd740ef
3 changed files with 56 additions and 1 deletions
|
@ -1,6 +1,8 @@
|
|||
//! Complete commands within shells
|
||||
|
||||
pub mod bash;
|
||||
mod completer;
|
||||
|
||||
pub mod bash;
|
||||
pub mod shells;
|
||||
|
||||
pub use completer::*;
|
||||
|
|
5
clap_complete/src/dynamic/shells/mod.rs
Normal file
5
clap_complete/src/dynamic/shells/mod.rs
Normal file
|
@ -0,0 +1,5 @@
|
|||
//! Shell support
|
||||
|
||||
mod shell;
|
||||
|
||||
pub use shell::*;
|
48
clap_complete/src/dynamic/shells/shell.rs
Normal file
48
clap_complete/src/dynamic/shells/shell.rs
Normal file
|
@ -0,0 +1,48 @@
|
|||
use std::fmt::Display;
|
||||
use std::str::FromStr;
|
||||
|
||||
use clap::builder::PossibleValue;
|
||||
use clap::ValueEnum;
|
||||
|
||||
/// Shell with auto-generated completion script available.
|
||||
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
|
||||
#[non_exhaustive]
|
||||
pub enum Shell {
|
||||
/// Bourne Again SHell (bash)
|
||||
Bash,
|
||||
}
|
||||
|
||||
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]
|
||||
}
|
||||
|
||||
fn to_possible_value<'a>(&self) -> Option<PossibleValue> {
|
||||
Some(match self {
|
||||
Shell::Bash => PossibleValue::new("bash"),
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue