diff options
author | Ed Page <eopage@gmail.com> | 2022-09-01 19:03:25 -0500 |
---|---|---|
committer | David Peter <sharkdp@users.noreply.github.com> | 2022-09-03 14:01:56 +0200 |
commit | e8e1c1d6c95bf5b7f1b2cc75448a83756033b5d9 (patch) | |
tree | 6e9c0d3a2fed9c972414e4d5ce9d5542af18c55f | |
parent | 542b3d23178be9a302a407b4e2f5a38f894176c8 (diff) |
refactor: Replace allow_invalid_utf8 with PathBuf
-rw-r--r-- | src/bin/bat/app.rs | 34 | ||||
-rw-r--r-- | 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<Vec<Input>> { - // 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<Vec<&Path>> = self .matches - .values_of_os("file-name") - .map(|values| values.map(Path::new).collect()); + .get_many::<PathBuf>("file-name") + .map(|vs| vs.map(|p| p.as_path()).collect::<Vec<_>>()); + + let files: Option<Vec<&Path>> = self + .matches + .get_many::<PathBuf>("FILE") + .map(|vs| vs.map(|p| p.as_path()).collect::<Vec<_>>()); + + // 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<dyn Iterator<Item = Option<&Path>>> = match filenames { Some(filenames) => Box::new(filenames.into_iter().map(Some)), None => Box::new(std::iter::repeat(None)), }; - let files: Option<Vec<&Path>> = 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<String> = 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 \ |