Skip to content
Myr

Move convert_cli_arguments_to_options to HyperfineOptions

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

authored by David Peter · committed by David Peter · parent c4ae7f6 · Browse files · 2 files+105 added−103 removed
This commit

State

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

Diff stats

2 files+105 added−103 removed

  • other:+105 added−103 removed

2 files+105 added−103 removed

modified:src/main.rs+6 added-102 removed
1use std::cmp;1use std::collections::BTreeMap;2use std::env;35use atty::Stream;4use clap::ArgMatches;5use colored::*;67pub mod app;
21 unmodified lines
29use benchmark_result::BenchmarkResult;30use command::Command;31use error::OptionsError;32use export::{ExportManager, ExportType};35use options::{CmdFailureAction, HyperfineOptions, OutputStyleOption, Shell};33use options::{HyperfineOptions, OutputStyleOption};34use parameter_range::get_parameterized_commands;35use tokenize::tokenize;36use types::ParameterValue;39use units::Unit;3738use anyhow::{bail, Result};3940pub fn write_benchmark_comparison(results: &[BenchmarkResult]) {
69 unmodified lines
110 Ok(())111}112113fn run() -> Result<()> {114 // Enabled ANSI colors on Windows 10115 #[cfg(windows)]116 colored::control::set_virtual_terminal(true).unwrap();117118 let matches = get_arg_matches(env::args_os());118 let options = convert_cli_arguments_to_options(&matches)?;119 let options = HyperfineOptions::from_cli_arguments(&matches)?;120 let commands = build_commands(&matches)?;121 let export_manager = build_export_manager(&matches)?;122123 run_benchmarks_and_print_comparison(&commands, &options, &export_manager)
8 unmodified lines
132 }133 }134}135135fn convert_cli_arguments_to_options<'a>(136 matches: &ArgMatches,137) -> Result<HyperfineOptions, OptionsError<'a>> {138 // Enabled ANSI colors on Windows 10139 #[cfg(windows)]140 colored::control::set_virtual_terminal(true).unwrap();141142 let mut options = HyperfineOptions::default();143 let param_to_u64 = |param| {144 matches145 .value_of(param)146 .map(|n| {147 n.parse::<u64>()148 .map_err(|e| OptionsError::NumericParsingError(param, e))149 })150 .transpose()151 };152153 options.warmup_count = param_to_u64("warmup")?.unwrap_or(options.warmup_count);154155 let mut min_runs = param_to_u64("min-runs")?;156 let mut max_runs = param_to_u64("max-runs")?;157158 if let Some(runs) = param_to_u64("runs")? {159 min_runs = Some(runs);160 max_runs = Some(runs);161 }162163 match (min_runs, max_runs) {164 (Some(min), None) => {165 options.runs.min = min;166 }167 (None, Some(max)) => {168 // Since the minimum was not explicit we lower it if max is below the default min.169 options.runs.min = cmp::min(options.runs.min, max);170 options.runs.max = Some(max);171 }172 (Some(min), Some(max)) if min > max => {173 return Err(OptionsError::EmptyRunsRange);174 }175 (Some(min), Some(max)) => {176 options.runs.min = min;177 options.runs.max = Some(max);178 }179 (None, None) => {}180 };181182 options.setup_command = matches.value_of("setup").map(String::from);183184 options.preparation_command = matches185 .values_of("prepare")186 .map(|values| values.map(String::from).collect::<Vec<String>>());187188 options.cleanup_command = matches.value_of("cleanup").map(String::from);189190 options.show_output = matches.is_present("show-output");191192 options.output_style = match matches.value_of("style") {193 Some("full") => OutputStyleOption::Full,194 Some("basic") => OutputStyleOption::Basic,195 Some("nocolor") => OutputStyleOption::NoColor,196 Some("color") => OutputStyleOption::Color,197 Some("none") => OutputStyleOption::Disabled,198 _ => {199 if !options.show_output && atty::is(Stream::Stdout) {200 OutputStyleOption::Full201 } else {202 OutputStyleOption::Basic203 }204 }205 };206207 match options.output_style {208 OutputStyleOption::Basic | OutputStyleOption::NoColor => {209 colored::control::set_override(false)210 }211 OutputStyleOption::Full | OutputStyleOption::Color => colored::control::set_override(true),212 OutputStyleOption::Disabled => {}213 };214215 if let Some(shell) = matches.value_of("shell") {216 options.shell = Shell::parse(shell)?;217 }218219 if matches.is_present("ignore-failure") {220 options.failure_action = CmdFailureAction::Ignore;221 }222223 options.time_unit = match matches.value_of("time-unit") {224 Some("millisecond") => Some(Unit::MilliSecond),225 Some("second") => Some(Unit::Second),226 _ => None,227 };228229 Ok(options)230}231136/// Build the ExportManager that will export the results specified137/// in the given ArgMatches138fn build_export_manager(matches: &ArgMatches) -> Result<ExportManager> {139 let mut export_manager = ExportManager::default();
210 unmodified lines
modified:src/options.rs+99 added-1 removed
1use std::fmt;1use std::process::Command;2use std::{cmp, fmt};34use atty::Stream;5use clap::ArgMatches;67use crate::error::OptionsError;8use crate::units::{Second, Unit};910#[cfg(not(windows))]
151 unmodified lines
162 }163 }164}165166impl HyperfineOptions {167 pub fn from_cli_arguments<'a>(matches: &ArgMatches) -> Result<Self, OptionsError<'a>> {168 let mut options = Self::default();169 let param_to_u64 = |param| {170 matches171 .value_of(param)172 .map(|n| {173 n.parse::<u64>()174 .map_err(|e| OptionsError::NumericParsingError(param, e))175 })176 .transpose()177 };178179 options.warmup_count = param_to_u64("warmup")?.unwrap_or(options.warmup_count);180181 let mut min_runs = param_to_u64("min-runs")?;182 let mut max_runs = param_to_u64("max-runs")?;183184 if let Some(runs) = param_to_u64("runs")? {185 min_runs = Some(runs);186 max_runs = Some(runs);187 }188189 match (min_runs, max_runs) {190 (Some(min), None) => {191 options.runs.min = min;192 }193 (None, Some(max)) => {194 // Since the minimum was not explicit we lower it if max is below the default min.195 options.runs.min = cmp::min(options.runs.min, max);196 options.runs.max = Some(max);197 }198 (Some(min), Some(max)) if min > max => {199 return Err(OptionsError::EmptyRunsRange);200 }201 (Some(min), Some(max)) => {202 options.runs.min = min;203 options.runs.max = Some(max);204 }205 (None, None) => {}206 };207208 options.setup_command = matches.value_of("setup").map(String::from);209210 options.preparation_command = matches211 .values_of("prepare")212 .map(|values| values.map(String::from).collect::<Vec<String>>());213214 options.cleanup_command = matches.value_of("cleanup").map(String::from);215216 options.show_output = matches.is_present("show-output");217218 options.output_style = match matches.value_of("style") {219 Some("full") => OutputStyleOption::Full,220 Some("basic") => OutputStyleOption::Basic,221 Some("nocolor") => OutputStyleOption::NoColor,222 Some("color") => OutputStyleOption::Color,223 Some("none") => OutputStyleOption::Disabled,224 _ => {225 if !options.show_output && atty::is(Stream::Stdout) {226 OutputStyleOption::Full227 } else {228 OutputStyleOption::Basic229 }230 }231 };232233 match options.output_style {234 OutputStyleOption::Basic | OutputStyleOption::NoColor => {235 colored::control::set_override(false)236 }237 OutputStyleOption::Full | OutputStyleOption::Color => {238 colored::control::set_override(true)239 }240 OutputStyleOption::Disabled => {}241 };242243 if let Some(shell) = matches.value_of("shell") {244 options.shell = Shell::parse(shell)?;245 }246247 if matches.is_present("ignore-failure") {248 options.failure_action = CmdFailureAction::Ignore;249 }250251 options.time_unit = match matches.value_of("time-unit") {252 Some("millisecond") => Some(Unit::MilliSecond),253 Some("second") => Some(Unit::Second),254 _ => None,255 };256257 Ok(options)258 }259}260261#[test]262fn test_shell_default_command() {263 let shell = Shell::default();264
42 unmodified lines