From 179ea53f4b127694c10884a5324f87275a2cfe65 Mon Sep 17 00:00:00 2001 From: lifegpc Date: Tue, 24 May 2022 13:29:51 +0000 Subject: [PATCH] --download-multiple-images support a optional value --- src/opthelper.rs | 5 ++-- src/opts.rs | 59 ++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 58 insertions(+), 6 deletions(-) diff --git a/src/opthelper.rs b/src/opthelper.rs index bc537a9..3ced2ee 100644 --- a/src/opthelper.rs +++ b/src/opthelper.rs @@ -149,8 +149,9 @@ impl OptHelper { /// Return whether to download multiple images at the same time. pub fn download_multiple_images(&self) -> bool { - if self.opt.download_multiple_images { - return true; + match self.opt.download_multiple_images { + Some(r) => { return r; } + None => {} } if self.settings.have_bool("download-multiple-images") { return self.settings.get_bool("download-multiple-images").unwrap(); diff --git a/src/opts.rs b/src/opts.rs index 48c46cc..61be3e2 100644 --- a/src/opts.rs +++ b/src/opts.rs @@ -7,6 +7,7 @@ use crate::pixiv_link::PixivID; use crate::retry_interval::parse_retry_interval_from_str; use crate::utils::check_file_exists; use crate::utils::get_exe_path_else_current; +use getopts::HasArg; use getopts::Options; use std::env; use std::str::FromStr; @@ -69,7 +70,7 @@ pub struct CommandOpts { /// Whether to enable progress bar pub use_progress_bar: Option, /// Whether to download multiple images at the same time - pub download_multiple_images: bool, + pub download_multiple_images: Option, } impl CommandOpts { @@ -89,7 +90,7 @@ impl CommandOpts { #[cfg(feature = "exif")] update_exif: false, use_progress_bar: None, - download_multiple_images: false, + download_multiple_images: None, } } @@ -135,6 +136,47 @@ pub fn print_usage(prog: &str, opts: &Options) { println!("{}", opts.usage(brief.as_str())); } +/// Prase bool string +pub fn parse_bool>(s: Option) -> Result, String> { + let tmp = match s { + Some(s) => { Some(s.as_ref().to_lowercase()) } + None => { None } + }; + match tmp { + Some(t) => { + if t == "true" { + Ok(Some(true)) + } else if t == "false" { + Ok(Some(false)) + } else if t == "yes" { + Ok(Some(true)) + } else if t == "no" { + Ok(Some(false)) + } else { + Err(format!("{} {}", gettext("Invalid boolean value:"), t)) + } + } + None => { Ok(None) } + } +} + +/// Parse optional option +/// * `opts` - The result of options. See [getopts::Matches]. +/// * `key` - The key of the option. +/// * `default` - The value if option is present but the data is not obtained. +/// * `callback` - The function to process the obtained data. +pub fn parse_optional_opt(opts: &getopts::Matches, key: &str, default: T, callback: F) -> Result, E> + where F: Fn(Option) -> Result, E> { + if !opts.opt_present(key) { + return Ok(None); + } + let s = opts.opt_str(key); + if s.is_none() { + return Ok(Some(default)); + } + callback(s) +} + pub fn parse_cmd() -> Option { let argv: Vec = env::args().collect(); let mut opts = Options::new(); @@ -185,10 +227,13 @@ pub fn parse_cmd() -> Option { gettext("Whether to enable progress bar."), "yes/no/auto", ); - opts.optflag( + opts.opt( "", "download-multiple-images", gettext("Download multiple images at the same time."), + "yes/no", + HasArg::Maybe, + getopts::Occur::Optional, ); let result = match opts.parse(&argv[1..]) { Ok(m) => m, @@ -312,6 +357,12 @@ pub fn parse_cmd() -> Option { } re.as_mut().unwrap().use_progress_bar = Some(r.unwrap()); } - re.as_mut().unwrap().download_multiple_images = result.opt_present("download-multiple-images"); + match parse_optional_opt(&result, "download-multiple-images", true, parse_bool) { + Ok(b) => re.as_mut().unwrap().download_multiple_images = b, + Err(e) => { + println!("{} {}", gettext("Failed to parse :").replace("", "download-multiple-images").as_str(), e); + return None; + } + } re }