use {
crate::{
cols::Cols,
filter::Filter,
units::Units,
sorting::Sorting,
},
crossterm::tty::IsTty,
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)]
pub list_cols: bool,
#[argh(option, default = "Default::default()")]
pub remote_stats: BoolArg,
#[argh(option, default = "Default::default()", short = 'c')]
pub cols: Cols,
#[argh(option, default = "Default::default()", short = 'f')]
pub filter: Filter,
#[argh(option, default = "Default::default()", short = 's')]
pub sort: Sorting,
#[argh(switch)]
pub csv: bool,
#[argh(option, default = "','")]
pub csv_separator: char,
#[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),
"bytes" => Ok(Self::Bytes),
_ => Err(format!("Illegal value: {:?} - valid values are 'SI', 'binary', and 'bytes'", value)),
}
}
}
impl Args {
pub fn color(&self) -> bool {
self.color.value()
.unwrap_or_else(|| std::io::stdout().is_tty())
}
}