Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bigfix] fix panics from clap internal type casting #1237

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 12 additions & 21 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,8 @@ impl Cli {
});

let num_format_style: NumberFormatStyle = matches
.get_one::<NumberFormatStyle>("num_format_style")
.cloned()
.get_one::<String>("num_format_style")
.map(parse_or_exit)
.unwrap_or_default();

let number_format = match num_format_style.get_format() {
Expand All @@ -268,30 +268,21 @@ 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 mut sort_reverse = false;
let sort = matches.get_one::<String>("sort");
let rsort = matches.get_one::<String>("rsort");

if rsort.is_some() {
sort_reverse = true;
}

let sort = sort.or(rsort).map(parse_or_exit);

// 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);

Expand Down
4 changes: 2 additions & 2 deletions src/cli_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
})
Expand Down
Loading