Accept the same flag several times

This commit is contained in:
Peltoche 2018-12-14 10:45:13 +01:00 committed by Pierre Peltier
parent 68dcbe4af4
commit 377f0c950f
2 changed files with 19 additions and 4 deletions

View file

@ -9,6 +9,7 @@ pub fn build_app() -> App<'static, 'static> {
Arg::with_name("all")
.short("a")
.long("all")
.multiple(true)
.help("Do not ignore entries starting with ."),
)
.arg(
@ -18,6 +19,7 @@ pub fn build_app() -> App<'static, 'static> {
.possible_value("auto")
.possible_value("never")
.default_value("auto")
.multiple(true)
.help("When to use terminal colours"),
)
.arg(
@ -27,35 +29,41 @@ pub fn build_app() -> App<'static, 'static> {
.possible_value("auto")
.possible_value("never")
.default_value("auto")
.multiple(true)
.help("When to print the icons"),
)
.arg(
Arg::with_name("indicators")
.short("F")
.long("classify")
.multiple(true)
.help("Append indicator (one of */=>@|) at the end of the file names"),
)
.arg(
Arg::with_name("long")
.short("l")
.long("long")
.multiple(true)
.help("Display extended file metadata as a table"),
)
.arg(
Arg::with_name("oneline")
.short("1")
.long("oneline")
.multiple(true)
.help("Display one entry per line"),
)
.arg(
Arg::with_name("recursive")
.short("R")
.long("recursive")
.multiple(true)
.help("Recurse into directories"),
)
.arg(
Arg::with_name("tree")
.long("tree")
.multiple(true)
.help("Recurse into directories and present the result as a tree"),
)
.arg(
@ -64,6 +72,7 @@ pub fn build_app() -> App<'static, 'static> {
.possible_value("date")
.possible_value("relative")
.default_value("date")
.multiple(true)
.help("How to display date"),
)
}

View file

@ -15,16 +15,21 @@ pub struct Flags {
impl<'a> From<ArgMatches<'a>> for Flags {
fn from(matches: ArgMatches) -> Self {
Flags {
let color_inputs: Vec<&str> = matches.values_of("color").unwrap().collect();
let icon_inputs: Vec<&str> = matches.values_of("icon").unwrap().collect();
let date_inputs: Vec<&str> = matches.values_of("date").unwrap().collect();
Self {
display_all: matches.is_present("all"),
display_long: matches.is_present("long"),
display_online: matches.is_present("oneline"),
display_tree: matches.is_present("tree"),
display_indicators: matches.is_present("indicators"),
recursive: matches.is_present("recursive"),
date: DateFlag::from(matches.value_of("date").unwrap()),
color: WhenFlag::from(matches.value_of("color").unwrap()),
icon: WhenFlag::from(matches.value_of("icon").unwrap()),
// Take only the last value
date: DateFlag::from(date_inputs[date_inputs.len() - 1]),
color: WhenFlag::from(color_inputs[color_inputs.len() - 1]),
icon: WhenFlag::from(icon_inputs[icon_inputs.len() - 1]),
}
}
}
@ -54,6 +59,7 @@ pub enum WhenFlag {
impl<'a> From<&'a str> for WhenFlag {
fn from(when: &'a str) -> Self {
println!("foobar: {}", when);
match when {
"always" => WhenFlag::Always,
"auto" => WhenFlag::Auto,