cli.rs
This file
Path
src/cli.rs
386 lines · 17.1 KiB · Rust
Last changed by
Add --conclude option with testsJack O'Connorbefore adoptione13d0cb
History of this fileTake it
1use std::ffi::OsString;23use clap::{4 builder::NonEmptyStringValueParser, crate_version, Arg, ArgAction, ArgMatches, Command,5 ValueHint,6};78pub fn get_cli_arguments<'a, I, T>(args: I) -> ArgMatches9where10 I: IntoIterator<Item = T>,11 T: Into<OsString> + Clone + 'a,12{13 let command = build_command();14 command.get_matches_from(args)15}1617/// Build the clap command for parsing command line arguments18fn build_command() -> Command {19 Command::new("hyperfine")20 .version(crate_version!())21 .next_line_help(true)22 .hide_possible_values(true)23 .about("A command-line benchmarking tool.")24 .help_expected(true)25 .max_term_width(80)26 .arg(27 Arg::new("command")28 .help("The command to benchmark. This can be the name of an executable, a command \29 line like \"grep -i todo\" or a shell command like \"sleep 0.5 && echo test\". \30 The latter is only available if the shell is not explicitly disabled via \31 '--shell=none'. If multiple commands are given, hyperfine will show a \32 comparison of the respective runtimes.")33 .required(true)34 .action(ArgAction::Append)35 .value_hint(ValueHint::CommandString)36 .value_parser(NonEmptyStringValueParser::new()),37 )38 .arg(39 Arg::new("warmup")40 .long("warmup")41 .short('w')42 .value_name("NUM")43 .action(ArgAction::Set)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::new("min-runs")51 .long("min-runs")52 .short('m')53 .action(ArgAction::Set)54 .value_name("NUM")55 .help("Perform at least NUM runs for each command (default: 10)."),56 )57 .arg(58 Arg::new("max-runs")59 .long("max-runs")60 .short('M')61 .action(ArgAction::Set)62 .value_name("NUM")63 .help("Perform at most NUM runs for each command. By default, there is no limit."),64 )65 .arg(66 Arg::new("runs")67 .long("runs")68 .conflicts_with_all(["max-runs", "min-runs"])69 .short('r')70 .action(ArgAction::Set)71 .value_name("NUM")72 .help("Perform exactly NUM runs for each command. If this option is not specified, \73 hyperfine automatically determines the number of runs."),74 )75 .arg(76 Arg::new("setup")77 .long("setup")78 .short('s')79 .action(ArgAction::Set)80 .value_name("CMD")81 .value_hint(ValueHint::CommandString)82 .help(83 "Execute CMD before each set of timing runs. This is useful for \84 compiling your software with the provided parameters, or to do any \85 other work that should happen once before a series of benchmark runs, \86 not every time as would happen with the --prepare option."87 ),88 )89 .arg(90 Arg::new("prepare")91 .long("prepare")92 .short('p')93 .action(ArgAction::Append)94 .num_args(1)95 .value_name("CMD")96 .value_hint(ValueHint::CommandString)97 .help(98 "Execute CMD before each timing run. This is useful for \99 clearing disk caches, for example.\nThe --prepare option can \100 be specified once for all commands or multiple times, once for \101 each command. In the latter case, each preparation command will \102 be run prior to the corresponding benchmark command.",103 ),104 )105 .arg(106 Arg::new("conclude")107 .long("conclude")108 .short('C')109 .action(ArgAction::Append)110 .num_args(1)111 .value_name("CMD")112 .value_hint(ValueHint::CommandString)113 .help(114 "Execute CMD after each timing run. This is useful for killing \115 long-running processes started (e.g. a web server started in --prepare), \116 for example.\nThe --conclude option can be specified once for all \117 commands or multiple times, once for each command. In the latter case, \118 each conclude command will be run after the corresponding benchmark \119 command.",120 ),121 )122 .arg(123 Arg::new("cleanup")124 .long("cleanup")125 .short('c')126 .action(ArgAction::Set)127 .value_name("CMD")128 .value_hint(ValueHint::CommandString)129 .help(130 "Execute CMD after the completion of all benchmarking \131 runs for each individual command to be benchmarked. \132 This is useful if the commands to be benchmarked produce \133 artifacts that need to be cleaned up."134 ),135 )136 .arg(137 Arg::new("parameter-scan")138 .long("parameter-scan")139 .short('P')140 .action(ArgAction::Set)141 .allow_hyphen_values(true)142 .value_names(["VAR", "MIN", "MAX"])143 .help(144 "Perform benchmark runs for each value in the range MIN..MAX. Replaces the \145 string '{VAR}' in each command by the current parameter value.\n\n \146 Example: hyperfine -P threads 1 8 'make -j {threads}'\n\n\147 This performs benchmarks for 'make -j 1', 'make -j 2', …, 'make -j 8'.\n\n\148 To have the value increase following different patterns, use shell arithmetics.\n\n \149 Example: hyperfine -P size 0 3 'sleep $((2**{size}))'\n\n\150 This performs benchmarks with power of 2 increases: 'sleep 1', 'sleep 2', 'sleep 4', …\n\151 The exact syntax may vary depending on your shell and OS."152 ),153 )154 .arg(155 Arg::new("parameter-step-size")156 .long("parameter-step-size")157 .short('D')158 .action(ArgAction::Set)159 .value_names(["DELTA"])160 .requires("parameter-scan")161 .help(162 "This argument requires --parameter-scan to be specified as well. \163 Traverse the range MIN..MAX in steps of DELTA.\n\n \164 Example: hyperfine -P delay 0.3 0.7 -D 0.2 'sleep {delay}'\n\n\165 This performs benchmarks for 'sleep 0.3', 'sleep 0.5' and 'sleep 0.7'.",166 ),167 )168 .arg(169 Arg::new("parameter-list")170 .long("parameter-list")171 .short('L')172 .action(ArgAction::Append)173 .allow_hyphen_values(true)174 .value_names(["VAR", "VALUES"])175 .conflicts_with_all(["parameter-scan", "parameter-step-size"])176 .help(177 "Perform benchmark runs for each value in the comma-separated list VALUES. \178 Replaces the string '{VAR}' in each command by the current parameter value\179 .\n\nExample: hyperfine -L compiler gcc,clang '{compiler} -O2 main.cpp'\n\n\180 This performs benchmarks for 'gcc -O2 main.cpp' and 'clang -O2 main.cpp'.\n\n\181 The option can be specified multiple times to run benchmarks for all \182 possible parameter combinations.\n"183 ),184 )185 .arg(186 Arg::new("shell")187 .long("shell")188 .short('S')189 .action(ArgAction::Set)190 .value_name("SHELL")191 .overrides_with("shell")192 .value_hint(ValueHint::CommandString)193 .help("Set the shell to use for executing benchmarked commands. This can be the \194 name or the path to the shell executable, or a full command line \195 like \"bash --norc\". It can also be set to \"default\" to explicitly select \196 the default shell on this platform. Finally, this can also be set to \197 \"none\" to disable the shell. In this case, commands will be executed \198 directly. They can still have arguments, but more complex things like \199 \"sleep 0.1; sleep 0.2\" are not possible without a shell.")200 )201 .arg(202 Arg::new("no-shell")203 .short('N')204 .action(ArgAction::SetTrue)205 .conflicts_with_all(["shell", "debug-mode"])206 .help("An alias for '--shell=none'.")207 )208 .arg(209 Arg::new("ignore-failure")210 .long("ignore-failure")211 .action(ArgAction::SetTrue)212 .short('i')213 .help("Ignore non-zero exit codes of the benchmarked programs."),214 )215 .arg(216 Arg::new("style")217 .long("style")218 .action(ArgAction::Set)219 .value_name("TYPE")220 .value_parser(["auto", "basic", "full", "nocolor", "color", "none"])221 .help(222 "Set output style type (default: auto). Set this to 'basic' to disable output \223 coloring and interactive elements. Set it to 'full' to enable all effects \224 even if no interactive terminal was detected. Set this to 'nocolor' to \225 keep the interactive output without any colors. Set this to 'color' to keep \226 the colors without any interactive output. Set this to 'none' to disable all \227 the output of the tool.",228 ),229 )230 .arg(231 Arg::new("sort")232 .long("sort")233 .action(ArgAction::Set)234 .value_name("METHOD")235 .value_parser(["auto", "command", "mean-time"])236 .default_value("auto")237 .hide_default_value(true)238 .help(239 "Specify the sort order of the speed comparison summary and the exported tables for \240 markup formats (Markdown, AsciiDoc, org-mode):\n \241 * 'auto' (default): the speed comparison will be ordered by time and\n \242 the markup tables will be ordered by command (input order).\n \243 * 'command': order benchmarks in the way they were specified\n \244 * 'mean-time': order benchmarks by mean runtime\n"245 ),246 )247 .arg(248 Arg::new("time-unit")249 .long("time-unit")250 .short('u')251 .action(ArgAction::Set)252 .value_name("UNIT")253 .value_parser(["microsecond", "millisecond", "second"])254 .help("Set the time unit to be used. Possible values: microsecond, millisecond, second. \255 If the option is not given, the time unit is determined automatically. \256 This option affects the standard output as well as all export formats except for CSV and JSON."),257 )258 .arg(259 Arg::new("export-asciidoc")260 .long("export-asciidoc")261 .action(ArgAction::Set)262 .value_name("FILE")263 .value_hint(ValueHint::FilePath)264 .help("Export the timing summary statistics as an AsciiDoc table to the given FILE. \265 The output time unit can be changed using the --time-unit option."),266 )267 .arg(268 Arg::new("export-csv")269 .long("export-csv")270 .action(ArgAction::Set)271 .value_name("FILE")272 .value_hint(ValueHint::FilePath)273 .help("Export the timing summary statistics as CSV to the given FILE. If you need \274 the timing results for each individual run, use the JSON export format. \275 The output time unit is always seconds."),276 )277 .arg(278 Arg::new("export-json")279 .long("export-json")280 .action(ArgAction::Set)281 .value_name("FILE")282 .value_hint(ValueHint::FilePath)283 .help("Export the timing summary statistics and timings of individual runs as JSON to the given FILE. \284 The output time unit is always seconds"),285 )286 .arg(287 Arg::new("export-markdown")288 .long("export-markdown")289 .action(ArgAction::Set)290 .value_name("FILE")291 .value_hint(ValueHint::FilePath)292 .help("Export the timing summary statistics as a Markdown table to the given FILE. \293 The output time unit can be changed using the --time-unit option."),294 )295 .arg(296 Arg::new("export-orgmode")297 .long("export-orgmode")298 .action(ArgAction::Set)299 .value_name("FILE")300 .value_hint(ValueHint::FilePath)301 .help("Export the timing summary statistics as an Emacs org-mode table to the given FILE. \302 The output time unit can be changed using the --time-unit option."),303 )304 .arg(305 Arg::new("show-output")306 .long("show-output")307 .action(ArgAction::SetTrue)308 .conflicts_with("style")309 .help(310 "Print the stdout and stderr of the benchmark instead of suppressing it. \311 This will increase the time it takes for benchmarks to run, \312 so it should only be used for debugging purposes or \313 when trying to benchmark output speed.",314 ),315 )316 .arg(317 Arg::new("output")318 .long("output")319 .conflicts_with("show-output")320 .action(ArgAction::Set)321 .value_name("WHERE")322 .help(323 "Control where the output of the benchmark is redirected. Note \324 that some programs like 'grep' detect when standard output is \325 /dev/null and apply certain optimizations. To avoid that, consider \326 using '--output=pipe'.\n\327 \n\328 <WHERE> can be:\n\329 \n \330 null: Redirect output to /dev/null (the default).\n\331 \n \332 pipe: Feed the output through a pipe before discarding it.\n\333 \n \334 inherit: Don't redirect the output at all (same as '--show-output').\n\335 \n \336 <FILE>: Write the output to the given file.",337 ),338 )339 .arg(340 Arg::new("input")341 .long("input")342 .action(ArgAction::Set)343 .num_args(1)344 .value_name("WHERE")345 .help("Control where the input of the benchmark comes from.\n\346 \n\347 <WHERE> can be:\n\348 \n \349 null: Read from /dev/null (the default).\n\350 \n \351 <FILE>: Read the input from the given file."),352 )353 .arg(354 Arg::new("command-name")355 .long("command-name")356 .short('n')357 .action(ArgAction::Append)358 .num_args(1)359 .value_name("NAME")360 .help("Give a meaningful name to a command. This can be specified multiple times \361 if several commands are benchmarked."),362 )363 // This option is hidden for now, as it is not yet clear yet if we want to 'stabilize' this,364 // see discussion in https://github.com/sharkdp/hyperfine/issues/527365 .arg(366 Arg::new("min-benchmarking-time")367 .long("min-benchmarking-time")368 .action(ArgAction::Set)369 .hide(true)370 .help("Set the minimum time (in seconds) to run benchmarks. Note that the number of \371 benchmark runs is additionally influenced by the `--min-runs`, `--max-runs`, and \372 `--runs` option.")373 )374 .arg(375 Arg::new("debug-mode")376 .long("debug-mode")377 .action(ArgAction::SetTrue)378 .hide(true)379 .help("Enable debug mode which does not actually run commands, but returns fake times when the command is 'sleep <time>'.")380 )381}382383#[test]384fn verify_app() {385 build_command().debug_assert();386}