use {
crate::units::Units,
argh::FromArgs,
std::path::PathBuf,
};
#[derive(FromArgs)]
pub struct Args {
#[argh(switch, short = 'v')]
pub version: bool,
#[argh(option, default = "Default::default()")]
pub color: BoolArg,
#[argh(switch, short = 'a')]
pub all: bool,
#[argh(switch, short = 'l')]
pub labels: bool,
#[argh(switch, short = 'i')]
pub inodes: bool,
#[argh(switch, short = 'j')]
pub json: bool,
#[argh(option, default = "Default::default()")]
pub units: Units,
#[argh(positional)]
pub path: Option<PathBuf>,
}
#[derive(Debug, Clone, Copy, Default)]
pub struct BoolArg(Option<bool>);
impl BoolArg {
pub fn value(self) -> Option<bool> {
self.0
}
}
impl argh::FromArgValue for BoolArg {
fn from_arg_value(value: &str) -> Result<Self, String> {
match value.to_lowercase().as_ref() {
"auto" => Ok(BoolArg(None)),
"yes" => Ok(BoolArg(Some(true))),
"no" => Ok(BoolArg(Some(false))),
_ => Err(format!("Illegal value: {:?}", value)),
}
}
}
impl argh::FromArgValue for Units {
fn from_arg_value(value: &str) -> Result<Self, String> {
match value.to_lowercase().as_ref() {
"si" => Ok(Self::Si),
"binary" => Ok(Self::Binary),
_ => Err(format!("Illegal value: {:?} - valid values are 'SI' and 'binary'", value)),
}
}
}