cli.rs
This file
Path
src/cli.rs
408 lines · 18.2 KiB · Rust
Last changed by
Add option to name reference commandNiklas Dewallybefore adoption2166c9f
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("reference")91 .long("reference")92 .action(ArgAction::Set)93 .value_name("CMD")94 .help(95 "The reference command for the relative comparison of results. \96 If this is unset, results are compared with the fastest command as reference."97 )98 )99 .arg(100 Arg::new("reference-name")101 .long("reference-name")102 .action(ArgAction::Set)103 .value_name("CMD")104 .help("Give a meaningful name to the reference command.")105 .requires("reference")106 )107 .arg(108 Arg::new("prepare")109 .long("prepare")110 .short('p')111 .action(ArgAction::Append)112 .num_args(1)113 .value_name("CMD")114 .value_hint(ValueHint::CommandString)115 .help(116 "Execute CMD before each timing run. This is useful for \117 clearing disk caches, for example.\nThe --prepare option can \118 be specified once for all commands or multiple times, once for \119 each command. In the latter case, each preparation command will \120 be run prior to the corresponding benchmark command.",121 ),122 )123 .arg(124 Arg::new("conclude")125 .long("conclude")126 .short('C')127 .action(ArgAction::Append)128 .num_args(1)129 .value_name("CMD")130 .value_hint(ValueHint::CommandString)131 .help(132 "Execute CMD after each timing run. This is useful for killing \133 long-running processes started (e.g. a web server started in --prepare), \134 for example.\nThe --conclude option can be specified once for all \135 commands or multiple times, once for each command. In the latter case, \136 each conclude command will be run after the corresponding benchmark \137 command.",138 ),139 )140 .arg(141 Arg::new("cleanup")142 .long("cleanup")143 .short('c')144 .action(ArgAction::Set)145 .value_name("CMD")146 .value_hint(ValueHint::CommandString)147 .help(148 "Execute CMD after the completion of all benchmarking \149 runs for each individual command to be benchmarked. \150 This is useful if the commands to be benchmarked produce \151 artifacts that need to be cleaned up."152 ),153 )154 .arg(155 Arg::new("parameter-scan")156 .long("parameter-scan")157 .short('P')158 .action(ArgAction::Set)159 .allow_hyphen_values(true)160 .value_names(["VAR", "MIN", "MAX"])161 .help(162 "Perform benchmark runs for each value in the range MIN..MAX. Replaces the \163 string '{VAR}' in each command by the current parameter value.\n\n \164 Example: hyperfine -P threads 1 8 'make -j {threads}'\n\n\165 This performs benchmarks for 'make -j 1', 'make -j 2', …, 'make -j 8'.\n\n\166 To have the value increase following different patterns, use shell arithmetics.\n\n \167 Example: hyperfine -P size 0 3 'sleep $((2**{size}))'\n\n\168 This performs benchmarks with power of 2 increases: 'sleep 1', 'sleep 2', 'sleep 4', …\n\169 The exact syntax may vary depending on your shell and OS."170 ),171 )172 .arg(173 Arg::new("parameter-step-size")174 .long("parameter-step-size")175 .short('D')176 .action(ArgAction::Set)177 .value_names(["DELTA"])178 .requires("parameter-scan")179 .help(180 "This argument requires --parameter-scan to be specified as well. \181 Traverse the range MIN..MAX in steps of DELTA.\n\n \182 Example: hyperfine -P delay 0.3 0.7 -D 0.2 'sleep {delay}'\n\n\183 This performs benchmarks for 'sleep 0.3', 'sleep 0.5' and 'sleep 0.7'.",184 ),185 )186 .arg(187 Arg::new("parameter-list")188 .long("parameter-list")189 .short('L')190 .action(ArgAction::Append)191 .allow_hyphen_values(true)192 .value_names(["VAR", "VALUES"])193 .conflicts_with_all(["parameter-scan", "parameter-step-size"])194 .help(195 "Perform benchmark runs for each value in the comma-separated list VALUES. \196 Replaces the string '{VAR}' in each command by the current parameter value\197 .\n\nExample: hyperfine -L compiler gcc,clang '{compiler} -O2 main.cpp'\n\n\198 This performs benchmarks for 'gcc -O2 main.cpp' and 'clang -O2 main.cpp'.\n\n\199 The option can be specified multiple times to run benchmarks for all \200 possible parameter combinations.\n"201 ),202 )203 .arg(204 Arg::new("shell")205 .long("shell")206 .short('S')207 .action(ArgAction::Set)208 .value_name("SHELL")209 .overrides_with("shell")210 .value_hint(ValueHint::CommandString)211 .help("Set the shell to use for executing benchmarked commands. This can be the \212 name or the path to the shell executable, or a full command line \213 like \"bash --norc\". It can also be set to \"default\" to explicitly select \214 the default shell on this platform. Finally, this can also be set to \215 \"none\" to disable the shell. In this case, commands will be executed \216 directly. They can still have arguments, but more complex things like \217 \"sleep 0.1; sleep 0.2\" are not possible without a shell.")218 )219 .arg(220 Arg::new("no-shell")221 .short('N')222 .action(ArgAction::SetTrue)223 .conflicts_with_all(["shell", "debug-mode"])224 .help("An alias for '--shell=none'.")225 )226 .arg(227 Arg::new("ignore-failure")228 .long("ignore-failure")229 .action(ArgAction::SetTrue)230 .short('i')231 .help("Ignore non-zero exit codes of the benchmarked programs."),232 )233 .arg(234 Arg::new("style")235 .long("style")236 .action(ArgAction::Set)237 .value_name("TYPE")238 .value_parser(["auto", "basic", "full", "nocolor", "color", "none"])239 .help(240 "Set output style type (default: auto). Set this to 'basic' to disable output \241 coloring and interactive elements. Set it to 'full' to enable all effects \242 even if no interactive terminal was detected. Set this to 'nocolor' to \243 keep the interactive output without any colors. Set this to 'color' to keep \244 the colors without any interactive output. Set this to 'none' to disable all \245 the output of the tool.",246 ),247 )248 .arg(249 Arg::new("sort")250 .long("sort")251 .action(ArgAction::Set)252 .value_name("METHOD")253 .value_parser(["auto", "command", "mean-time"])254 .default_value("auto")255 .hide_default_value(true)256 .help(257 "Specify the sort order of the speed comparison summary and the exported tables for \258 markup formats (Markdown, AsciiDoc, org-mode):\n \259 * 'auto' (default): the speed comparison will be ordered by time and\n \260 the markup tables will be ordered by command (input order).\n \261 * 'command': order benchmarks in the way they were specified\n \262 * 'mean-time': order benchmarks by mean runtime\n"263 ),264 )265 .arg(266 Arg::new("time-unit")267 .long("time-unit")268 .short('u')269 .action(ArgAction::Set)270 .value_name("UNIT")271 .value_parser(["microsecond", "millisecond", "second"])272 .help("Set the time unit to be used. Possible values: microsecond, millisecond, second. \273 If the option is not given, the time unit is determined automatically. \274 This option affects the standard output as well as all export formats except for CSV and JSON."),275 )276 .arg(277 Arg::new("export-asciidoc")278 .long("export-asciidoc")279 .action(ArgAction::Set)280 .value_name("FILE")281 .value_hint(ValueHint::FilePath)282 .help("Export the timing summary statistics as an AsciiDoc table to the given FILE. \283 The output time unit can be changed using the --time-unit option."),284 )285 .arg(286 Arg::new("export-csv")287 .long("export-csv")288 .action(ArgAction::Set)289 .value_name("FILE")290 .value_hint(ValueHint::FilePath)291 .help("Export the timing summary statistics as CSV to the given FILE. If you need \292 the timing results for each individual run, use the JSON export format. \293 The output time unit is always seconds."),294 )295 .arg(296 Arg::new("export-json")297 .long("export-json")298 .action(ArgAction::Set)299 .value_name("FILE")300 .value_hint(ValueHint::FilePath)301 .help("Export the timing summary statistics and timings of individual runs as JSON to the given FILE. \302 The output time unit is always seconds"),303 )304 .arg(305 Arg::new("export-markdown")306 .long("export-markdown")307 .action(ArgAction::Set)308 .value_name("FILE")309 .value_hint(ValueHint::FilePath)310 .help("Export the timing summary statistics as a Markdown table to the given FILE. \311 The output time unit can be changed using the --time-unit option."),312 )313 .arg(314 Arg::new("export-orgmode")315 .long("export-orgmode")316 .action(ArgAction::Set)317 .value_name("FILE")318 .value_hint(ValueHint::FilePath)319 .help("Export the timing summary statistics as an Emacs org-mode table to the given FILE. \320 The output time unit can be changed using the --time-unit option."),321 )322 .arg(323 Arg::new("show-output")324 .long("show-output")325 .action(ArgAction::SetTrue)326 .conflicts_with("style")327 .help(328 "Print the stdout and stderr of the benchmark instead of suppressing it. \329 This will increase the time it takes for benchmarks to run, \330 so it should only be used for debugging purposes or \331 when trying to benchmark output speed.",332 ),333 )334 .arg(335 Arg::new("output")336 .long("output")337 .conflicts_with("show-output")338 .action(ArgAction::Append)339 .value_name("WHERE")340 .help(341 "Control where the output of the benchmark is redirected. Note \342 that some programs like 'grep' detect when standard output is \343 /dev/null and apply certain optimizations. To avoid that, consider \344 using '--output=pipe'.\n\345 \n\346 <WHERE> can be:\n\347 \n \348 null: Redirect output to /dev/null (the default).\n\349 \n \350 pipe: Feed the output through a pipe before discarding it.\n\351 \n \352 inherit: Don't redirect the output at all (same as '--show-output').\n\353 \n \354 <FILE>: Write the output to the given file.\n\n\355 This option can be specified once for all commands or multiple times, once for \356 each command. Note: If you want to log the output of each and every iteration, \357 you can use a shell redirection and the '$HYPERFINE_ITERATION' environment variable:\n \358 hyperfine 'my-command > output-${HYPERFINE_ITERATION}.log'\n\n",359 ),360 )361 .arg(362 Arg::new("input")363 .long("input")364 .action(ArgAction::Set)365 .num_args(1)366 .value_name("WHERE")367 .help("Control where the input of the benchmark comes from.\n\368 \n\369 <WHERE> can be:\n\370 \n \371 null: Read from /dev/null (the default).\n\372 \n \373 <FILE>: Read the input from the given file."),374 )375 .arg(376 Arg::new("command-name")377 .long("command-name")378 .short('n')379 .action(ArgAction::Append)380 .num_args(1)381 .value_name("NAME")382 .help("Give a meaningful name to a command. This can be specified multiple times \383 if several commands are benchmarked."),384 )385 // This option is hidden for now, as it is not yet clear yet if we want to 'stabilize' this,386 // see discussion in https://github.com/sharkdp/hyperfine/issues/527387 .arg(388 Arg::new("min-benchmarking-time")389 .long("min-benchmarking-time")390 .action(ArgAction::Set)391 .hide(true)392 .help("Set the minimum time (in seconds) to run benchmarks. Note that the number of \393 benchmark runs is additionally influenced by the `--min-runs`, `--max-runs`, and \394 `--runs` option.")395 )396 .arg(397 Arg::new("debug-mode")398 .long("debug-mode")399 .action(ArgAction::SetTrue)400 .hide(true)401 .help("Enable debug mode which does not actually run commands, but returns fake times when the command is 'sleep <time>'.")402 )403}404405#[test]406fn verify_app() {407 build_command().debug_assert();408}