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