Fix downcast type mismatches in clap_builder (#1310)

* Fix downcast type mismatches in clap_builder

* Fix downcast type mismatch for --num-format

* Apply changes from #1233 and #1237
This commit is contained in:
CordlessCoder
2025-12-26 12:31:50 +00:00
committed by GitHub
parent efd5a1db82
commit cba46d528b
2 changed files with 12 additions and 26 deletions
+10 -24
View File
@@ -256,9 +256,9 @@ impl Cli {
.collect()
});
let num_format_style: NumberFormatStyle = matches
.get_one::<NumberFormatStyle>("num_format_style")
.cloned()
let num_format_style = matches
.get_one::<String>("num_format_style")
.map(parse_or_exit::<NumberFormatStyle>)
.unwrap_or_default();
let number_format = match num_format_style.get_format() {
@@ -269,30 +269,16 @@ impl Cli {
}
};
// Sorting category should be restricted by clap but parse before we do
// work just in case.
let (sort, sort_reverse) = if let Some(sort) = matches.get_one::<String>("sort") {
(Some(sort.clone()), false)
} else {
let sort = matches.get_one::<String>("rsort");
(sort.cloned(), sort.is_some())
};
let sort = sort.map(|x| match Sort::from_str(&x) {
Ok(sort) => sort,
Err(e) => {
eprintln!("Error:\n{}", e);
process::exit(1);
}
});
let sort = matches.get_one::<String>("sort");
let rsort = matches.get_one::<String>("rsort");
let sort = sort.or(rsort).map(parse_or_exit);
let sort_reverse = rsort.is_some();
// Format category is overly accepting by clap (so the user knows what
// is supported) but this will fail if support is not compiled in and
// give a useful error to the user.
let output = matches.get_one("output").cloned();
let streaming = matches
.get_one("streaming")
.cloned()
.map(parse_or_exit::<Streaming>);
let streaming = matches.get_one::<String>("streaming").map(parse_or_exit);
crate::cli_utils::setup_logger(verbose);
@@ -320,8 +306,8 @@ impl Cli {
cli
}
pub fn file_input(&self) -> Option<&str> {
self.matches.get_one("file_input").cloned()
pub fn file_input(&self) -> Option<&String> {
self.matches.get_one("file_input")
}
pub fn ignored_directories(&self) -> Vec<&str> {
+2 -2
View File
@@ -54,12 +54,12 @@ pub fn setup_logger(verbose_option: u64) {
builder.init();
}
pub fn parse_or_exit<T>(s: &str) -> T
pub fn parse_or_exit<T>(s: impl AsRef<str>) -> T
where
T: FromStr,
T::Err: fmt::Display,
{
T::from_str(s).unwrap_or_else(|e| {
T::from_str(s.as_ref()).unwrap_or_else(|e| {
eprintln!("Error:\n{}", e);
process::exit(1);
})