mirror of
https://github.com/sharkdp/bat.git
synced 2024-10-31 20:11:01 +01:00
refactor: Replace allow_invalid_utf8 with PathBuf
This commit is contained in:
parent
542b3d2317
commit
e8e1c1d6c9
@ -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>> {
|
||||
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
|
||||
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() =>
|
||||
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 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 {
|
||||
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),
|
||||
|
@ -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 \
|
||||
|
Loading…
Reference in New Issue
Block a user