Skip to content
Myr

Show error if numeric arguments can not be parsed

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Shows an error message if the argument to options like '--warmup <N>' can not be parsed: > hyperfine --warmup xxx "sleep 1" Error: Could not read numeric argument to '--warmup': invalid digit found in string closes #337

authored by sharkdp · committed by David Peter · parent f67763f · Browse files · 3 files+23 added−9 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+23 added−9 removed

  • other:+23 added−9 removed

3 files+23 added−9 removed

modified:src/hyperfine/error.rs+11 added-3 removed
1use rust_decimal::Error as DecimalError;2use std::error::Error;3use std::fmt;4use std::num;5use std::num::ParseIntError;67#[derive(Debug)]8pub enum ParameterScanError {9 ParseIntError(num::ParseIntError),
36 unmodified lines
4647impl Error for ParameterScanError {}4849#[derive(Debug)]49pub enum OptionsError {50pub enum OptionsError<'a> {51 RunsBelowTwo,52 EmptyRunsRange,53 TooManyCommandNames(usize),54 NumericParsingError(&'a str, ParseIntError),55}5655impl fmt::Display for OptionsError {57impl<'a> fmt::Display for OptionsError<'a> {58 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {59 match *self {60 OptionsError::EmptyRunsRange => write!(f, "Empty runs range"),61 OptionsError::RunsBelowTwo => write!(f, "Number of runs below two"),62 OptionsError::TooManyCommandNames(n) => {63 write!(f, "Too many --command-name options: expected {} at most", n)64 }65 OptionsError::NumericParsingError(cmd, ref err) => write!(66 f,67 "Could not read numeric argument to '--{cmd}': {err}",68 cmd = cmd,69 err = err70 ),71 }72 }73}7467impl Error for OptionsError {}75impl<'a> Error for OptionsError<'a> {}
modified:src/main.rs+11 added-6 removed
84 unmodified lines
85 }86}8788/// Build the HyperfineOptions that correspond to the given ArgMatches89fn build_hyperfine_options(matches: &ArgMatches<'_>) -> Result<HyperfineOptions, OptionsError> {89fn build_hyperfine_options<'a>(90 matches: &ArgMatches<'a>,91) -> Result<HyperfineOptions, OptionsError<'a>> {92 let mut options = HyperfineOptions::default();93 let param_to_u64 = |param| {94 matches95 .value_of(param)94 .and_then(|n| u64::from_str_radix(n, 10).ok())96 .map(|n| {97 u64::from_str_radix(n, 10).map_err(|e| OptionsError::NumericParsingError(param, e))98 })99 .transpose()100 };10197 options.warmup_count = param_to_u64("warmup").unwrap_or(options.warmup_count);102 options.warmup_count = param_to_u64("warmup")?.unwrap_or(options.warmup_count);10399 let mut min_runs = param_to_u64("min-runs");100 let mut max_runs = param_to_u64("max-runs");104 let mut min_runs = param_to_u64("min-runs")?;105 let mut max_runs = param_to_u64("max-runs")?;106102 if let Some(runs) = param_to_u64("runs") {107 if let Some(runs) = param_to_u64("runs")? {108 min_runs = Some(runs);109 max_runs = Some(runs);110 }111
229 unmodified lines
modified:CHANGELOG.md+1 added
24 unmodified lines
2526- Fix a bug in the outlier detection which would only detect "slow outliers" but not the fast ones (runs that are much faster than the rest of the benchmarking runs), see #32927- Better error messages for very fast commands that would lead to inf/nan results in the relative28 speed comparison, see #31929- Show error message if `--warmup` or `--*runs` arguments can not be parsed, see #33730- Keep output colorized when the output is not interactive and `--style=full` or `--style=color` is used.3132## Other33## Packaging
208 unmodified lines