use crate::filters::FilterKind;
use clap::{value_t, App, AppSettings, Arg, ArgSettings};
use std::ffi::OsString;
#[derive(Clone, Debug)]
pub struct Options {
pub filter: FilterKind,
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 = value_t!(options.value_of("filter"), FilterKind);
Options {
statistics: options.is_present("statistics"),
inverted: options.is_present("invert"),
filter: filter.unwrap_or(FilterKind::Digest),
inputs: options
.values_of("inputs")
.unwrap()
.map(|s| s.to_owned())
.collect(),
}
}
fn create_parser<'a, 'b>() -> App<'a, 'b> {
App::new("")
.name(env!("CARGO_PKG_NAME"))
.about(env!("CARGO_PKG_DESCRIPTION"))
.version(env!("CARGO_PKG_VERSION"))
.args(&[
Arg::with_name("filter")
.help("Filter to use to determine uniqueness")
.short("f")
.long("filter")
.takes_value(true)
.possible_values(&FilterKind::variants())
.set(ArgSettings::CaseInsensitive)
.set(ArgSettings::HideDefaultValue),
Arg::with_name("inputs")
.help("Input sources to filter")
.multiple(true)
.required(true),
Arg::with_name("invert")
.help("Prints duplicates instead of uniques")
.short("i")
.long("invert"),
Arg::with_name("statistics")
.help("Prints statistics instead of entries")
.short("s")
.long("statistics"),
])
.settings(&[
AppSettings::ArgRequiredElseHelp,
AppSettings::HidePossibleValuesInHelp,
AppSettings::TrailingVarArg,
])
}
}