diff options
author | Yiyu Lin <linyiyu1992@gmail.com> | 2023-01-15 12:06:46 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-01-15 09:36:46 +0530 |
commit | fd85f840cd707c31d08fa836562127e249c9ff62 (patch) | |
tree | c4847881976069d41d1b53616bdeaf5d073f3af6 /cli/args/flags.rs | |
parent | 05ef925eb095ae603f694fe8172ddcbd5b19b9b2 (diff) |
refactor: clean up `unwrap` and `clone` (#17282)
Co-authored-by: Divy Srivastava <dj.srivastava23@gmail.com>
Diffstat (limited to 'cli/args/flags.rs')
-rw-r--r-- | cli/args/flags.rs | 74 |
1 files changed, 31 insertions, 43 deletions
diff --git a/cli/args/flags.rs b/cli/args/flags.rs index 84c1d083b..ef1aedce4 100644 --- a/cli/args/flags.rs +++ b/cli/args/flags.rs @@ -465,30 +465,30 @@ impl Flags { /// If it returns None, the config file shouldn't be discovered at all. pub fn config_path_args(&self) -> Option<Vec<PathBuf>> { use DenoSubcommand::*; - if let Fmt(FmtFlags { files, .. }) = &self.subcommand { - Some(files.include.clone()) - } else if let Lint(LintFlags { files, .. }) = &self.subcommand { - Some(files.include.clone()) - } else if let Run(RunFlags { script }) = &self.subcommand { - if let Ok(module_specifier) = deno_core::resolve_url_or_path(script) { - if module_specifier.scheme() == "file" - || module_specifier.scheme() == "npm" - { - if let Ok(p) = module_specifier.to_file_path() { - Some(vec![p]) + + match &self.subcommand { + Fmt(FmtFlags { files, .. }) => Some(files.include.clone()), + Lint(LintFlags { files, .. }) => Some(files.include.clone()), + Run(RunFlags { script }) => { + if let Ok(module_specifier) = deno_core::resolve_url_or_path(script) { + if module_specifier.scheme() == "file" + || module_specifier.scheme() == "npm" + { + if let Ok(p) = module_specifier.to_file_path() { + Some(vec![p]) + } else { + Some(vec![]) + } } else { - Some(vec![]) + // When the entrypoint doesn't have file: scheme (it's the remote + // script), then we don't auto discover config file. + None } } else { - // When the entrypoint doesn't have file: scheme (it's the remote - // script), then we don't auto discover config file. - None + Some(vec![]) } - } else { - Some(vec![]) } - } else { - Some(vec![]) + _ => Some(vec![]), } } @@ -583,15 +583,15 @@ pub fn flags_from_vec(args: Vec<String>) -> clap::Result<Flags> { if matches.is_present("unstable") { flags.unstable = true; } - if matches.is_present("log-level") { - flags.log_level = match matches.value_of("log-level").unwrap() { - "debug" => Some(Level::Debug), - "info" => Some(Level::Info), - _ => unreachable!(), - }; - } + if matches.is_present("quiet") { flags.log_level = Some(Level::Error); + } else { + match matches.value_of("log-level") { + Some("debug") => flags.log_level = Some(Level::Debug), + Some("info") => flags.log_level = Some(Level::Info), + _ => {} + } } match matches.subcommand() { @@ -2556,11 +2556,9 @@ fn fmt_parse(flags: &mut Flags, matches: &clap::ArgMatches) { } else { None }; - let prose_wrap = if matches.is_present("options-prose-wrap") { - Some(matches.value_of("options-prose-wrap").unwrap().to_string()) - } else { - None - }; + let prose_wrap = matches + .value_of("options-prose-wrap") + .map(ToString::to_string); flags.subcommand = DenoSubcommand::Fmt(FmtFlags { check: matches.is_present("check"), @@ -2597,12 +2595,7 @@ fn info_parse(flags: &mut Flags, matches: &clap::ArgMatches) { fn install_parse(flags: &mut Flags, matches: &clap::ArgMatches) { runtime_args_parse(flags, matches, true, true); - let root = if matches.is_present("root") { - let install_root = matches.value_of("root").unwrap(); - Some(PathBuf::from(install_root)) - } else { - None - }; + let root = matches.value_of("root").map(PathBuf::from); let force = matches.is_present("force"); let name = matches.value_of("name").map(|s| s.to_string()); @@ -2625,12 +2618,7 @@ fn install_parse(flags: &mut Flags, matches: &clap::ArgMatches) { } fn uninstall_parse(flags: &mut Flags, matches: &clap::ArgMatches) { - let root = if matches.is_present("root") { - let install_root = matches.value_of("root").unwrap(); - Some(PathBuf::from(install_root)) - } else { - None - }; + let root = matches.value_of("root").map(PathBuf::from); let name = matches.value_of("name").unwrap().to_string(); flags.subcommand = DenoSubcommand::Uninstall(UninstallFlags { name, root }); |