use clap::{ArgEnum, Parser};
#[derive(ArgEnum, Clone, Debug, PartialEq)]
pub enum Coloring {
Automatic,
Always,
Never,
}
impl core::str::FromStr for Coloring {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(match s.to_lowercase().as_str() {
"never" => Coloring::Never,
"always" => Coloring::Always,
_ => Coloring::Automatic,
})
}
}
#[derive(Debug, Clone, Parser)]
#[clap(author, version, about)]
pub struct Interface {
#[clap(long, short, parse(from_flag))]
pub all: bool,
#[cfg(not(target_os = "windows"))]
#[clap(long, short, parse(from_flag))]
pub simple: bool,
#[clap(long, short, parse(from_flag))]
pub directories: bool,
#[clap(long, short, value_name = "COMMAND")]
pub editor: Option<Option<String>>,
#[clap(long, short, parse(from_flag))]
pub json: bool,
#[clap(long, short)]
pub limit: Option<usize>,
#[clap(long, short = 'E', value_name = "PATTERN")]
pub exclude: Vec<String>,
#[clap(
arg_enum,
long,
short,
value_name = "WHEN",
default_value = "automatic"
)]
pub color: Coloring,
#[clap(short, long)]
pub portable: bool,
#[clap(default_value = ".")]
pub path: String,
}
#[cfg(test)]
mod test {
use super::Coloring;
use core::str::FromStr;
#[test]
fn coloring_parsing() {
assert_eq!(Coloring::Always, Coloring::from_str("always").unwrap());
assert_eq!(Coloring::Never, Coloring::from_str("never").unwrap());
assert_eq!(
Coloring::Automatic,
Coloring::from_str("automatic").unwrap()
);
assert_eq!(Coloring::Always, Coloring::from_str("AlwAys").unwrap());
assert_eq!(Coloring::Never, Coloring::from_str("NeveR").unwrap());
assert_eq!(
Coloring::Automatic,
Coloring::from_str("AutoMATic").unwrap()
);
assert_eq!(Coloring::Automatic, Coloring::from_str("xxx").unwrap());
}
}