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
Provenance
- Commit
39a85b8- Tree
56e6f37- Author
David Peter <mail@david-peter.de>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
c4ae7f6
2 files changed+105 added−103 removed
modified:src/main.rs+6 added-102 removed
1-use std::cmp;1 use std::collections::BTreeMap;2 use std::env;3 5-use atty::Stream;4 use clap::ArgMatches;5 use colored::*;6 7 pub mod app;21 unmodified lines29 use benchmark_result::BenchmarkResult;30 use command::Command;31 use error::OptionsError;32 use export::{ExportManager, ExportType};35-use options::{CmdFailureAction, HyperfineOptions, OutputStyleOption, Shell};33+use options::{HyperfineOptions, OutputStyleOption};34 use parameter_range::get_parameterized_commands;35 use tokenize::tokenize;36 use types::ParameterValue;39-use units::Unit;37 38 use anyhow::{bail, Result};39 40 pub fn write_benchmark_comparison(results: &[BenchmarkResult]) {69 unmodified lines110 Ok(())111 }112 113 fn run() -> Result<()> {114+ // Enabled ANSI colors on Windows 10115+ #[cfg(windows)]116+ colored::control::set_virtual_terminal(true).unwrap();117+118 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)?;122 123 run_benchmarks_and_print_comparison(&commands, &options, &export_manager)8 unmodified lines132 }133 }134 }135 135-fn 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();141-142- 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- };152-153- options.warmup_count = param_to_u64("warmup")?.unwrap_or(options.warmup_count);154-155- let mut min_runs = param_to_u64("min-runs")?;156- let mut max_runs = param_to_u64("max-runs")?;157-158- if let Some(runs) = param_to_u64("runs")? {159- min_runs = Some(runs);160- max_runs = Some(runs);161- }162-163- 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- };181-182- options.setup_command = matches.value_of("setup").map(String::from);183-184- options.preparation_command = matches185- .values_of("prepare")186- .map(|values| values.map(String::from).collect::<Vec<String>>());187-188- options.cleanup_command = matches.value_of("cleanup").map(String::from);189-190- options.show_output = matches.is_present("show-output");191-192- 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- };206-207- 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- };214-215- if let Some(shell) = matches.value_of("shell") {216- options.shell = Shell::parse(shell)?;217- }218-219- if matches.is_present("ignore-failure") {220- options.failure_action = CmdFailureAction::Ignore;221- }222-223- options.time_unit = match matches.value_of("time-unit") {224- Some("millisecond") => Some(Unit::MilliSecond),225- Some("second") => Some(Unit::Second),226- _ => None,227- };228-229- Ok(options)230-}231-136 /// Build the ExportManager that will export the results specified137 /// in the given ArgMatches138 fn build_export_manager(matches: &ArgMatches) -> Result<ExportManager> {139 let mut export_manager = ExportManager::default();210 unmodified linesmodified:src/options.rs+99 added-1 removed
1-use std::fmt;1 use std::process::Command;2+use std::{cmp, fmt};3 4+use atty::Stream;5+use clap::ArgMatches;6+7 use crate::error::OptionsError;8 use crate::units::{Second, Unit};9 10 #[cfg(not(windows))]151 unmodified lines162 }163 }164 }165 166+impl 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+ };178+179+ options.warmup_count = param_to_u64("warmup")?.unwrap_or(options.warmup_count);180+181+ let mut min_runs = param_to_u64("min-runs")?;182+ let mut max_runs = param_to_u64("max-runs")?;183+184+ if let Some(runs) = param_to_u64("runs")? {185+ min_runs = Some(runs);186+ max_runs = Some(runs);187+ }188+189+ 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+ };207+208+ options.setup_command = matches.value_of("setup").map(String::from);209+210+ options.preparation_command = matches211+ .values_of("prepare")212+ .map(|values| values.map(String::from).collect::<Vec<String>>());213+214+ options.cleanup_command = matches.value_of("cleanup").map(String::from);215+216+ options.show_output = matches.is_present("show-output");217+218+ 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+ };232+233+ 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+ };242+243+ if let Some(shell) = matches.value_of("shell") {244+ options.shell = Shell::parse(shell)?;245+ }246+247+ if matches.is_present("ignore-failure") {248+ options.failure_action = CmdFailureAction::Ignore;249+ }250+251+ options.time_unit = match matches.value_of("time-unit") {252+ Some("millisecond") => Some(Unit::MilliSecond),253+ Some("second") => Some(Unit::Second),254+ _ => None,255+ };256+257+ Ok(options)258+ }259+}260+261 #[test]262 fn test_shell_default_command() {263 let shell = Shell::default();264 42 unmodified lines