refactor: Replace allow_invalid_utf8 with PathBuf

This commit is contained in:
Ed Page 2022-09-01 19:03:25 -05:00 committed by David Peter
parent 542b3d2317
commit e8e1c1d6c9
2 changed files with 22 additions and 22 deletions

View File

@ -1,6 +1,6 @@
use std::collections::HashSet; use std::collections::HashSet;
use std::env; use std::env;
use std::path::Path; use std::path::{Path, PathBuf};
use std::str::FromStr; use std::str::FromStr;
use atty::{self, Stream}; use atty::{self, Stream};
@ -245,30 +245,28 @@ impl App {
} }
pub fn inputs(&self) -> Result<Vec<Input>> { pub fn inputs(&self) -> Result<Vec<Input>> {
let filenames: Option<Vec<&Path>> = self
.matches
.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 // verify equal length of file-names and input FILEs
match self.matches.values_of("file-name") { if filenames.is_some()
Some(ref filenames) && files.is_some()
if self.matches.values_of_os("FILE").is_some() && filenames.as_ref().map(|v| v.len()) != files.as_ref().map(|v| v.len())
&& filenames.len() != self.matches.values_of_os("FILE").unwrap().len() =>
{ {
return Err("Must be one file name per input type.".into()); 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());
let mut filenames_or_none: Box<dyn Iterator<Item = Option<&Path>>> = match filenames { let mut filenames_or_none: Box<dyn Iterator<Item = Option<&Path>>> = match filenames {
Some(filenames) => Box::new(filenames.into_iter().map(Some)), Some(filenames) => Box::new(filenames.into_iter().map(Some)),
None => Box::new(std::iter::repeat(None)), 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() { if files.is_none() {
return Ok(vec![new_stdin_input( return Ok(vec![new_stdin_input(
filenames_or_none.next().unwrap_or(None), filenames_or_none.next().unwrap_or(None),

View File

@ -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 once_cell::sync::Lazy;
use std::env; use std::env;
use std::path::Path; use std::path::{Path, PathBuf};
static VERSION: Lazy<String> = Lazy::new(|| { static VERSION: Lazy<String> = Lazy::new(|| {
#[cfg(feature = "bugreport")] #[cfg(feature = "bugreport")]
@ -50,7 +52,7 @@ pub fn build_app(interactive_output: bool) -> Command<'static> {
) )
.takes_value(true) .takes_value(true)
.multiple_values(true) .multiple_values(true)
.allow_invalid_utf8(true), .value_parser(value_parser!(PathBuf)),
) )
.arg( .arg(
Arg::new("show-all") Arg::new("show-all")
@ -117,7 +119,7 @@ pub fn build_app(interactive_output: bool) -> Command<'static> {
.takes_value(true) .takes_value(true)
.multiple_occurrences(true) .multiple_occurrences(true)
.value_name("name") .value_name("name")
.allow_invalid_utf8(true) .value_parser(value_parser!(PathBuf))
.help("Specify the name to display for a file.") .help("Specify the name to display for a file.")
.long_help( .long_help(
"Specify the name to display for a file. Useful when piping \ "Specify the name to display for a file. Useful when piping \