add the -s parameter, it was default, but it shouldn't be

This commit is contained in:
runebaas 2019-10-28 15:00:50 +01:00
parent 34dd7f7609
commit 4d6ce91d51
No known key found for this signature in database
GPG key ID: 2677AF508D0300D6

View file

@ -7,6 +7,7 @@ struct Options {
filename: String, filename: String,
length: usize, length: usize,
long_fortunes: bool, long_fortunes: bool,
short_fortunes: bool,
wait: bool wait: bool
} }
@ -34,7 +35,14 @@ fn main() {
.short("l") .short("l")
.long("long") .long("long")
.help("only return long fortunes") .help("only return long fortunes")
.takes_value(false)) .takes_value(false)
.conflicts_with("short"))
.arg(Arg::with_name("short")
.short("s")
.long("short")
.help("only return short fortunes")
.takes_value(false)
.conflicts_with("long"))
.arg(Arg::with_name("wait") .arg(Arg::with_name("wait")
.short("w") .short("w")
.long("wait") .long("wait")
@ -42,17 +50,19 @@ fn main() {
let options = parse_options(app); let options = parse_options(app);
// get fortunes // get fortunes
let fortunes = get_fortunes(options.filename.clone()); let mut fortunes = get_fortunes(options.filename.clone());
// filter by max length // filter by max length
if options.short_fortunes || options.long_fortunes {
let filter_fn = if options.long_fortunes { filter_short } else { filter_long }; let filter_fn = if options.long_fortunes { filter_short } else { filter_long };
let filtered = fortunes fortunes = fortunes
.into_iter() .into_iter()
.filter(|x| filter_fn(x, options.length)) .filter(|x| filter_fn(x, options.length))
.collect::<Vec<String>>(); .collect::<Vec<String>>();
}
// get a random one // get a random one
let the_fortune = get_random_fortune(filtered); let the_fortune = get_random_fortune(fortunes);
println!("{}", the_fortune); println!("{}", the_fortune);
if options.wait { if options.wait {
@ -73,6 +83,7 @@ fn parse_options(app: App) -> Options {
let options: Options = Options { let options: Options = Options {
filename: matches.value_of("file").unwrap().to_owned(), filename: matches.value_of("file").unwrap().to_owned(),
length: matches.value_of("length").unwrap().parse::<usize>().expect("Length is not a valid number"), length: matches.value_of("length").unwrap().parse::<usize>().expect("Length is not a valid number"),
short_fortunes: matches.is_present("short"),
long_fortunes: matches.is_present("long"), long_fortunes: matches.is_present("long"),
wait: matches.is_present("wait") wait: matches.is_present("wait")
}; };