From e8e1c1d6c95bf5b7f1b2cc75448a83756033b5d9 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Thu, 1 Sep 2022 19:03:25 -0500 Subject: [PATCH] refactor: Replace allow_invalid_utf8 with PathBuf --- src/bin/bat/app.rs | 34 ++++++++++++++++------------------ src/bin/bat/clap_app.rs | 10 ++++++---- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/src/bin/bat/app.rs b/src/bin/bat/app.rs index 80bb483a..87387782 100644 --- a/src/bin/bat/app.rs +++ b/src/bin/bat/app.rs @@ -1,6 +1,6 @@ use std::collections::HashSet; use std::env; -use std::path::Path; +use std::path::{Path, PathBuf}; use std::str::FromStr; use atty::{self, Stream}; @@ -245,30 +245,28 @@ impl App { } pub fn inputs(&self) -> Result> { - // verify equal length of file-names and input FILEs - match self.matches.values_of("file-name") { - Some(ref filenames) - if self.matches.values_of_os("FILE").is_some() - && filenames.len() != self.matches.values_of_os("FILE").unwrap().len() => - { - return Err("Must be one file name per input type.".into()); - } - _ => {} - } let filenames: Option> = self .matches - .values_of_os("file-name") - .map(|values| values.map(Path::new).collect()); + .get_many::("file-name") + .map(|vs| vs.map(|p| p.as_path()).collect::>()); + + let files: Option> = self + .matches + .get_many::("FILE") + .map(|vs| vs.map(|p| p.as_path()).collect::>()); + + // verify equal length of file-names and input FILEs + if filenames.is_some() + && files.is_some() + && filenames.as_ref().map(|v| v.len()) != files.as_ref().map(|v| v.len()) + { + return Err("Must be one file name per input type.".into()); + } let mut filenames_or_none: Box>> = match filenames { Some(filenames) => Box::new(filenames.into_iter().map(Some)), None => Box::new(std::iter::repeat(None)), }; - let files: Option> = self - .matches - .values_of_os("FILE") - .map(|vs| vs.map(Path::new).collect()); - if files.is_none() { return Ok(vec![new_stdin_input( filenames_or_none.next().unwrap_or(None), diff --git a/src/bin/bat/clap_app.rs b/src/bin/bat/clap_app.rs index fa3bba84..5297cfb4 100644 --- a/src/bin/bat/clap_app.rs +++ b/src/bin/bat/clap_app.rs @@ -1,7 +1,9 @@ -use clap::{crate_name, crate_version, AppSettings, Arg, ArgGroup, ColorChoice, Command}; +use clap::{ + crate_name, crate_version, value_parser, AppSettings, Arg, ArgGroup, ColorChoice, Command, +}; use once_cell::sync::Lazy; use std::env; -use std::path::Path; +use std::path::{Path, PathBuf}; static VERSION: Lazy = Lazy::new(|| { #[cfg(feature = "bugreport")] @@ -50,7 +52,7 @@ pub fn build_app(interactive_output: bool) -> Command<'static> { ) .takes_value(true) .multiple_values(true) - .allow_invalid_utf8(true), + .value_parser(value_parser!(PathBuf)), ) .arg( Arg::new("show-all") @@ -117,7 +119,7 @@ pub fn build_app(interactive_output: bool) -> Command<'static> { .takes_value(true) .multiple_occurrences(true) .value_name("name") - .allow_invalid_utf8(true) + .value_parser(value_parser!(PathBuf)) .help("Specify the name to display for a file.") .long_help( "Specify the name to display for a file. Useful when piping \