use std::path::PathBuf;
use std::str::FromStr;
use clap::{ArgMatches, Shell};
use super::Matcher;
pub struct CompletionsMatcher<'a> {
matches: &'a ArgMatches<'a>,
}
impl<'a: 'b, 'b> CompletionsMatcher<'a> {
pub fn shells(&'a self) -> Vec<Shell> {
let raw = self
.matches
.values_of("SHELL")
.expect("no shells were given");
let mut shells: Vec<_> = raw
.into_iter()
.map(|name| name.trim().to_lowercase())
.map(|name| {
if name == "all" {
Shell::variants()
.iter()
.map(|name| name.to_string())
.collect()
} else {
vec![name]
}
})
.flatten()
.collect();
shells.sort_unstable();
shells.dedup();
shells
.into_iter()
.map(|name| Shell::from_str(&name).expect("failed to parse shell name"))
.collect()
}
pub fn output(&'a self) -> PathBuf {
self.matches
.value_of("output")
.map(PathBuf::from)
.unwrap_or_else(|| PathBuf::from("./"))
}
}
impl<'a> Matcher<'a> for CompletionsMatcher<'a> {
fn with(matches: &'a ArgMatches) -> Option<Self> {
matches
.subcommand_matches("generate")?
.subcommand_matches("completions")
.map(|matches| CompletionsMatcher { matches })
}
}