Skip to content
Myr

cli.rs

This file

Path

src/cli.rs

340 lines · 15.0 KiB · Rust

Last changed by

Fix clippy suggestionsDavid Peterbefore adoptionc44aa1d

History of this file

Take it

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