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