Skip to content
Myr

cli.rs

This file

Path

src/cli.rs

341 lines · 15.0 KiB · Rust

Last changed by

Use max. term width, not term widthDavid Peterbefore adoptionfbe9ca6

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