use clap::{value_parser, Arg, ArgAction, Command};
use runiq::Filters;
use std::ffi::OsString;
#[derive(Clone, Debug)]
pub struct Options {
pub filter: Filters,
pub inputs: Vec<String>,
pub inverted: bool,
pub statistics: bool,
}
impl Options {
pub fn from<I, T>(args: I) -> Options
where
I: IntoIterator<Item = T>,
T: Into<OsString> + Clone,
{
let parser = Options::create_parser();
let options = parser.get_matches_from(args);
let filter = options.get_one::<Filters>("filter");
Options {
statistics: options.get_flag("statistics"),
inverted: options.get_flag("invert"),
filter: filter.unwrap().to_owned(),
inputs: options
.get_many::<String>("inputs")
.unwrap()
.map(|s| s.to_owned())
.collect(),
}
}
fn create_parser() -> Command {
Command::new("")
.name(env!("CARGO_PKG_NAME"))
.about(env!("CARGO_PKG_DESCRIPTION"))
.version(env!("CARGO_PKG_VERSION"))
.args(&[
Arg::new("filter")
.help("Filter to use to determine uniqueness")
.short('f')
.long("filter")
.num_args(1)
.value_parser(value_parser!(Filters))
.hide_default_value(true)
.default_value("quick")
.ignore_case(true),
Arg::new("inputs")
.help("Input sources to filter")
.action(ArgAction::Append)
.hide_default_value(true)
.default_value("-"),
Arg::new("invert")
.help("Prints duplicates instead of uniques")
.short('i')
.long("invert")
.action(ArgAction::SetTrue),
Arg::new("statistics")
.help("Prints statistics instead of entries")
.short('s')
.long("statistics")
.action(ArgAction::SetTrue),
Arg::new("help")
.short('h')
.long("help")
.action(ArgAction::HelpLong)
.hide(true),
])
.disable_help_subcommand(true)
.disable_help_flag(true)
.trailing_var_arg(true)
}
}