Skip to content
Myr

All parsing of arguments now split out.

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

authored by Steve Pentland · committed by David Peter · parent 6063b40 · Browse files · 3 files+172 added−140 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+172 added−140 removed

  • other:+172 added−140 removed

3 files+172 added−140 removed

added:src/hyperfine/app.rs+123 added
1use clap::{App, AppSettings, Arg, ArgMatches};2use std::ffi::OsString;3use atty;4use atty::Stream;56pub fn get_arg_matches<T>(args: T) -> ArgMatches<'static>7where8 T: Iterator<Item = OsString>,9{10 let app = build_app();11 app.get_matches_from(args)12}1314/// Build the clap app for parsing command line arguments15fn build_app() -> App<'static, 'static> {16 let clap_color_setting = if atty::is(Stream::Stdout) {17 AppSettings::ColoredHelp18 } else {19 AppSettings::ColorNever20 };2122 App::new("hyperfine")23 .version(crate_version!())24 .setting(clap_color_setting)25 .setting(AppSettings::DeriveDisplayOrder)26 .setting(AppSettings::UnifiedHelpMessage)27 .setting(AppSettings::NextLineHelp)28 .setting(AppSettings::HidePossibleValuesInHelp)29 .max_term_width(90)30 .about("A command-line benchmarking tool.")31 .arg(32 Arg::with_name("command")33 .help("Command to benchmark")34 .required(true)35 .multiple(true)36 .empty_values(false),37 )38 .arg(39 Arg::with_name("warmup")40 .long("warmup")41 .short("w")42 .takes_value(true)43 .value_name("NUM")44 .help(45 "Perform NUM warmup runs before the actual benchmark. This can be used \46 to fill (disk) caches for I/O-heavy programs.",47 ),48 )49 .arg(50 Arg::with_name("min-runs")51 .long("min-runs")52 .short("m")53 .takes_value(true)54 .value_name("NUM")55 .help("Perform at least NUM runs for each command (default: 10)."),56 )57 .arg(58 Arg::with_name("prepare")59 .long("prepare")60 .short("p")61 .takes_value(true)62 .value_name("CMD")63 .help(64 "Execute CMD before each timing run. This is useful for \65 clearing disk caches, for example.",66 ),67 )68 .arg(69 Arg::with_name("parameter-scan")70 .long("parameter-scan")71 .short("P")72 .help(73 "Perform benchmark runs for each value in the range MIN..MAX. Replaces the \74 string '{VAR}' in each command by the current parameter value.",75 )76 .takes_value(true)77 .allow_hyphen_values(true)78 .value_names(&["VAR", "MIN", "MAX"]),79 )80 .arg(81 Arg::with_name("style")82 .long("style")83 .short("s")84 .takes_value(true)85 .value_name("TYPE")86 .possible_values(&["auto", "basic", "full", "nocolor"])87 .help(88 "Set output style type (default: auto). Set this to 'basic' to disable output \89 coloring and interactive elements. Set it to 'full' to enable all effects \90 even if no interactive terminal was detected. Set this to 'nocolor' to \91 keep the interactive output without any colors.",92 ),93 )94 .arg(95 Arg::with_name("ignore-failure")96 .long("ignore-failure")97 .short("i")98 .help("Ignore non-zero exit codes."),99 )100 .arg(101 Arg::with_name("export-csv")102 .long("export-csv")103 .takes_value(true)104 .value_name("FILE")105 .help("Export the timing results as CSV to the given FILE."),106 )107 .arg(108 Arg::with_name("export-json")109 .long("export-json")110 .takes_value(true)111 .value_name("FILE")112 .help("Export the timing results as JSON to the given FILE."),113 )114 .arg(115 Arg::with_name("export-markdown")116 .long("export-markdown")117 .takes_value(true)118 .value_name("FILE")119 .help("Export the timing results as a Markdown table to the given FILE."),120 )121 .help_message("Print this help message.")122 .version_message("Show version information.")123}
modified:src/hyperfine/mod.rs+1 added
6 unmodified lines
7pub mod timer;8pub mod types;9pub mod warnings;10pub mod error;11pub mod app;
modified:src/main.rs+48 added-140 removed
13 unmodified lines
14extern crate serde_derive;15extern crate serde_json;16extern crate statistical;1718// Os-specific dependencies19cfg_if! {20 if #[cfg(windows)] {21 extern crate winapi;22 } else {
6 unmodified lines
29extern crate approx;3031use std::cmp;32use std::error::Error;33use std::env;34use std::io;35use std::ops::Range;3637use atty::Stream;38use colored::*;37use clap::{App, AppSettings, Arg};39use clap::ArgMatches;4041mod hyperfine;4243use hyperfine::internal::write_benchmark_comparison;
1 unmodified line
45 OutputStyleOption};46use hyperfine::benchmark::{mean_shell_spawning_time, run_benchmark};47use hyperfine::export::{ExportManager, ExportType};48use hyperfine::error::ParameterScanError;49use hyperfine::app::get_arg_matches;5051/// Print error message to stderr and terminate52pub fn error(message: &str) -> ! {53 eprintln!("{} {}", "Error:".red(), message);
34 unmodified lines
88 return Ok((param_name, param_min..(param_max + 1)));89}9091fn main() {89 // Process command line options90 let mut options = HyperfineOptions::default();92 let matches = get_arg_matches(env::args_os());93 let options = build_hyperfine_options(&matches);94 let export_manager = build_export_manager(&matches);95 let commands = build_commands(&matches);9692 let clap_color_setting = if atty::is(Stream::Stdout) {93 AppSettings::ColoredHelp94 } else {95 AppSettings::ColorNever96 };97 let res = run(&commands, &options);9898 let matches = App::new("hyperfine")99 .version(crate_version!())100 .setting(clap_color_setting)101 .setting(AppSettings::DeriveDisplayOrder)102 .setting(AppSettings::UnifiedHelpMessage)103 .setting(AppSettings::NextLineHelp)104 .setting(AppSettings::HidePossibleValuesInHelp)105 .max_term_width(90)106 .about("A command-line benchmarking tool.")107 .arg(108 Arg::with_name("command")109 .help("Command to benchmark")110 .required(true)111 .multiple(true)112 .empty_values(false),113 )114 .arg(115 Arg::with_name("warmup")116 .long("warmup")117 .short("w")118 .takes_value(true)119 .value_name("NUM")120 .help(121 "Perform NUM warmup runs before the actual benchmark. This can be used \122 to fill (disk) caches for I/O-heavy programs.",123 ),124 )125 .arg(126 Arg::with_name("min-runs")127 .long("min-runs")128 .short("m")129 .takes_value(true)130 .value_name("NUM")131 .help(&format!(132 "Perform at least NUM runs for each command (default: {}).",133 options.min_runs134 )),135 )136 .arg(137 Arg::with_name("prepare")138 .long("prepare")139 .short("p")140 .takes_value(true)141 .value_name("CMD")142 .help(143 "Execute CMD before each timing run. This is useful for \144 clearing disk caches, for example.",145 ),146 )147 .arg(148 Arg::with_name("parameter-scan")149 .long("parameter-scan")150 .short("P")151 .help(152 "Perform benchmark runs for each value in the range MIN..MAX. Replaces the \153 string '{VAR}' in each command by the current parameter value.",154 )155 .takes_value(true)156 .allow_hyphen_values(true)157 .value_names(&["VAR", "MIN", "MAX"]),158 )159 .arg(160 Arg::with_name("style")161 .long("style")162 .short("s")163 .takes_value(true)164 .value_name("TYPE")165 .possible_values(&["auto", "basic", "full", "nocolor"])166 .help(167 "Set output style type (default: auto). Set this to 'basic' to disable output \168 coloring and interactive elements. Set it to 'full' to enable all effects \169 even if no interactive terminal was detected. Set this to 'nocolor' to \170 keep the interactive output without any colors.",171 ),172 )173 .arg(174 Arg::with_name("ignore-failure")175 .long("ignore-failure")176 .short("i")177 .help("Ignore non-zero exit codes."),178 )179 .arg(180 Arg::with_name("export-csv")181 .long("export-csv")182 .takes_value(true)183 .value_name("FILE")184 .help("Export the timing results as CSV to the given FILE."),185 )186 .arg(187 Arg::with_name("export-json")188 .long("export-json")189 .takes_value(true)190 .value_name("FILE")191 .help("Export the timing results as JSON to the given FILE."),192 )193 .arg(194 Arg::with_name("export-markdown")195 .long("export-markdown")196 .takes_value(true)197 .value_name("FILE")198 .help("Export the timing results as a Markdown table to the given FILE."),199 )200 .help_message("Print this help message.")201 .version_message("Show version information.")202 .get_matches();99 match res {100 Ok(timing_results) => {101 write_benchmark_comparison(&timing_results);102 let ans = export_manager.write_results(timing_results);103 if let Err(e) = ans {104 error(&format!(105 "The following error occured while exporting: {}",106 e.description()107 ));108 }109 }110 Err(e) => error(e.description()),111 }112}113114/// Build the HyperfineOptions that correspond to the given ArgMatches115fn build_hyperfine_options(matches: &ArgMatches) -> HyperfineOptions {116 let mut options = HyperfineOptions::default();117 let str_to_u64 = |n| u64::from_str_radix(n, 10).ok();118119 options.warmup_count = matches120 .value_of("warmup")
17 unmodified lines
138 OutputStyleOption::Basic139 },140 };141229 let mut export_manager = ExportManager::new();230 if let Some(filename) = matches.value_of("export-json") {231 export_manager.add_exporter(ExportType::Json, filename);232 }233 if let Some(filename) = matches.value_of("export-csv") {234 export_manager.add_exporter(ExportType::Csv, filename);235 }236 if let Some(filename) = matches.value_of("export-markdown") {237 export_manager.add_exporter(ExportType::Markdown, filename);238 }239142 // We default Windows to NoColor if full had been specified.143 if cfg!(windows) && options.output_style == OutputStyleOption::Full {144 options.output_style = OutputStyleOption::NoColor;145 }
5 unmodified lines
151 if matches.is_present("ignore-failure") {152 options.failure_action = CmdFailureAction::Ignore;153 }154155 options156}157158/// Build the ExportManager that will export the results specified159/// in the given ArgMatches160fn build_export_manager(matches: &ArgMatches) -> ExportManager {161 let mut export_manager = ExportManager::new();162 if let Some(filename) = matches.value_of("export-json") {163 export_manager.add_exporter(ExportType::Json, filename);164 }165 if let Some(filename) = matches.value_of("export-csv") {166 export_manager.add_exporter(ExportType::Csv, filename);167 }168 if let Some(filename) = matches.value_of("export-markdown") {169 export_manager.add_exporter(ExportType::Markdown, filename);170 }171 export_manager172}173174/// Build the commands to benchmark175fn build_commands<'a>(matches: &'a ArgMatches) -> Vec<Command<'a>> {176 let command_strings = matches.values_of("command").unwrap();177178 let commands = if let Some(args) = matches.values_of("parameter-scan") {179 match parse_parameter_scan_args(args) {
11 unmodified lines
191 }192 } else {193 command_strings.map(|c| Command::new(c)).collect()194 };272273 let res = run(&commands, &options);274275 match res {276 Ok(timing_results) => {277 write_benchmark_comparison(&timing_results);278 let ans = export_manager.write_results(timing_results);279 if let Err(e) = ans {280 error(&format!(281 "The following error occured while exporting: {}",282 e.description()283 ));284 }285 }286 Err(e) => error(e.description()),287 }195 commands196}