#compdef hyperfine

autoload -U is-at-least

_hyperfine() {
    typeset -A opt_args
    typeset -a _arguments_options
    local ret=1

    if is-at-least 5.2; then
        _arguments_options=(-s -S -C)
    else
        _arguments_options=(-s -C)
    fi

    local context curcontext="$curcontext" state line
    _arguments "${_arguments_options[@]}" : \
'-w+[Perform NUM warmup runs before the actual benchmark. This can be used to fill (disk) caches for I/O-heavy programs.]:NUM:_default' \
'--warmup=[Perform NUM warmup runs before the actual benchmark. This can be used to fill (disk) caches for I/O-heavy programs.]:NUM:_default' \
'-m+[Perform at least NUM runs for each command (default\: 10).]:NUM:_default' \
'--min-runs=[Perform at least NUM runs for each command (default\: 10).]:NUM:_default' \
'-M+[Perform at most NUM runs for each command. By default, there is no limit.]:NUM:_default' \
'--max-runs=[Perform at most NUM runs for each command. By default, there is no limit.]:NUM:_default' \
'(-M --max-runs -m --min-runs)-r+[Perform exactly NUM runs for each command. If this option is not specified, hyperfine automatically determines the number of runs.]:NUM:_default' \
'(-M --max-runs -m --min-runs)--runs=[Perform exactly NUM runs for each command. If this option is not specified, hyperfine automatically determines the number of runs.]:NUM:_default' \
'-s+[Execute CMD before each set of timing runs. This is useful for compiling your software with the provided parameters, or to do any other work that should happen once before a series of benchmark runs, not every time as would happen with the --prepare option.]:CMD:_cmdstring' \
'--setup=[Execute CMD before each set of timing runs. This is useful for compiling your software with the provided parameters, or to do any other work that should happen once before a series of benchmark runs, not every time as would happen with the --prepare option.]:CMD:_cmdstring' \
'--reference=[The reference command for the relative comparison of results. If this is unset, results are compared with the fastest command as reference.]:CMD:_default' \
'--reference-name=[Give a meaningful name to the reference command.]:CMD:_default' \
'*-p+[Execute CMD before each timing run. This is useful for clearing disk caches, for example. The --prepare option can be specified once for all commands or multiple times, once for each command. In the latter case, each preparation command will be run prior to the corresponding benchmark command.]:CMD:_cmdstring' \
'*--prepare=[Execute CMD before each timing run. This is useful for clearing disk caches, for example. The --prepare option can be specified once for all commands or multiple times, once for each command. In the latter case, each preparation command will be run prior to the corresponding benchmark command.]:CMD:_cmdstring' \
'*-C+[Execute CMD after each timing run. This is useful for killing long-running processes started (e.g. a web server started in --prepare), for example. The --conclude option can be specified once for all commands or multiple times, once for each command. In the latter case, each conclude command will be run after the corresponding benchmark command.]:CMD:_cmdstring' \
'*--conclude=[Execute CMD after each timing run. This is useful for killing long-running processes started (e.g. a web server started in --prepare), for example. The --conclude option can be specified once for all commands or multiple times, once for each command. In the latter case, each conclude command will be run after the corresponding benchmark command.]:CMD:_cmdstring' \
'-c+[Execute CMD after the completion of all benchmarking runs for each individual command to be benchmarked. This is useful if the commands to be benchmarked produce artifacts that need to be cleaned up.]:CMD:_cmdstring' \
'--cleanup=[Execute CMD after the completion of all benchmarking runs for each individual command to be benchmarked. This is useful if the commands to be benchmarked produce artifacts that need to be cleaned up.]:CMD:_cmdstring' \
'-P+[Perform benchmark runs for each value in the range MIN..MAX. Replaces the string '\''{VAR}'\'' in each command by the current parameter value.    Example\:  hyperfine -P threads 1 8 '\''make -j {threads}'\''  This performs benchmarks for '\''make -j 1'\'', '\''make -j 2'\'', …, '\''make -j 8'\''.  To have the value increase following different patterns, use shell arithmetics.    Example\: hyperfine -P size 0 3 '\''sleep \$((2**{size}))'\''  This performs benchmarks with power of 2 increases\: '\''sleep 1'\'', '\''sleep 2'\'', '\''sleep 4'\'', … The exact syntax may vary depending on your shell and OS.]:VAR:_default:VAR:_default:VAR:_default' \
'--parameter-scan=[Perform benchmark runs for each value in the range MIN..MAX. Replaces the string '\''{VAR}'\'' in each command by the current parameter value.    Example\:  hyperfine -P threads 1 8 '\''make -j {threads}'\''  This performs benchmarks for '\''make -j 1'\'', '\''make -j 2'\'', …, '\''make -j 8'\''.  To have the value increase following different patterns, use shell arithmetics.    Example\: hyperfine -P size 0 3 '\''sleep \$((2**{size}))'\''  This performs benchmarks with power of 2 increases\: '\''sleep 1'\'', '\''sleep 2'\'', '\''sleep 4'\'', … The exact syntax may vary depending on your shell and OS.]:VAR:_default:VAR:_default:VAR:_default' \
'-D+[This argument requires --parameter-scan to be specified as well. Traverse the range MIN..MAX in steps of DELTA.    Example\:  hyperfine -P delay 0.3 0.7 -D 0.2 '\''sleep {delay}'\''  This performs benchmarks for '\''sleep 0.3'\'', '\''sleep 0.5'\'' and '\''sleep 0.7'\''.]:DELTA:_default' \
'--parameter-step-size=[This argument requires --parameter-scan to be specified as well. Traverse the range MIN..MAX in steps of DELTA.    Example\:  hyperfine -P delay 0.3 0.7 -D 0.2 '\''sleep {delay}'\''  This performs benchmarks for '\''sleep 0.3'\'', '\''sleep 0.5'\'' and '\''sleep 0.7'\''.]:DELTA:_default' \
'(-P --parameter-scan -D --parameter-step-size)*-L+[Perform benchmark runs for each value in the comma-separated list VALUES. Replaces the string '\''{VAR}'\'' in each command by the current parameter value.  Example\:  hyperfine -L compiler gcc,clang '\''{compiler} -O2 main.cpp'\''  This performs benchmarks for '\''gcc -O2 main.cpp'\'' and '\''clang -O2 main.cpp'\''.  The option can be specified multiple times to run benchmarks for all possible parameter combinations. ]:VAR:_default:VAR:_default' \
'(-P --parameter-scan -D --parameter-step-size)*--parameter-list=[Perform benchmark runs for each value in the comma-separated list VALUES. Replaces the string '\''{VAR}'\'' in each command by the current parameter value.  Example\:  hyperfine -L compiler gcc,clang '\''{compiler} -O2 main.cpp'\''  This performs benchmarks for '\''gcc -O2 main.cpp'\'' and '\''clang -O2 main.cpp'\''.  The option can be specified multiple times to run benchmarks for all possible parameter combinations. ]:VAR:_default:VAR:_default' \
'-S+[Set the shell to use for executing benchmarked commands. This can be the name or the path to the shell executable, or a full command line like "bash --norc". It can also be set to "default" to explicitly select the default shell on this platform. Finally, this can also be set to "none" to disable the shell. In this case, commands will be executed directly. They can still have arguments, but more complex things like "sleep 0.1; sleep 0.2" are not possible without a shell.]:SHELL:_cmdstring' \
'--shell=[Set the shell to use for executing benchmarked commands. This can be the name or the path to the shell executable, or a full command line like "bash --norc". It can also be set to "default" to explicitly select the default shell on this platform. Finally, this can also be set to "none" to disable the shell. In this case, commands will be executed directly. They can still have arguments, but more complex things like "sleep 0.1; sleep 0.2" are not possible without a shell.]:SHELL:_cmdstring' \
'-i+[Ignore failures of the benchmarked programs. Without a value or with '\''all-non-zero'\'', all non-zero exit codes are ignored. You can also provide a comma-separated list of exit codes to ignore (e.g., --ignore-failure=1,2).]' \
'--ignore-failure=[Ignore failures of the benchmarked programs. Without a value or with '\''all-non-zero'\'', all non-zero exit codes are ignored. You can also provide a comma-separated list of exit codes to ignore (e.g., --ignore-failure=1,2).]' \
'--style=[Set output style type (default\: auto). Set this to '\''basic'\'' to disable output coloring and interactive elements. Set it to '\''full'\'' to enable all effects even if no interactive terminal was detected. Set this to '\''nocolor'\'' to keep the interactive output without any colors. Set this to '\''color'\'' to keep the colors without any interactive output. Set this to '\''none'\'' to disable all the output of the tool.]:TYPE:(auto basic full nocolor color none)' \
'--sort=[Specify the sort order of the speed comparison summary and the exported tables for markup formats (Markdown, AsciiDoc, org-mode)\:   * '\''auto'\'' (default)\: the speed comparison will be ordered by time and     the markup tables will be ordered by command (input order).   * '\''command'\''\: order benchmarks in the way they were specified   * '\''mean-time'\''\: order benchmarks by mean runtime ]:METHOD:(auto command mean-time)' \
'-u+[Set the time unit to be used. Possible values\: microsecond, millisecond, second. If the option is not given, the time unit is determined automatically. This option affects the standard output as well as all export formats except for CSV and JSON.]:UNIT:(microsecond millisecond second)' \
'--time-unit=[Set the time unit to be used. Possible values\: microsecond, millisecond, second. If the option is not given, the time unit is determined automatically. This option affects the standard output as well as all export formats except for CSV and JSON.]:UNIT:(microsecond millisecond second)' \
'--export-asciidoc=[Export the timing summary statistics as an AsciiDoc table to the given FILE. The output time unit can be changed using the --time-unit option.]:FILE:_files' \
'--export-csv=[Export the timing summary statistics as CSV to the given FILE. If you need the timing results for each individual run, use the JSON export format. The output time unit is always seconds.]:FILE:_files' \
'--export-json=[Export the timing summary statistics and timings of individual runs as JSON to the given FILE. The output time unit is always seconds]:FILE:_files' \
'--export-markdown=[Export the timing summary statistics as a Markdown table to the given FILE. The output time unit can be changed using the --time-unit option.]:FILE:_files' \
'--export-orgmode=[Export the timing summary statistics as an Emacs org-mode table to the given FILE. The output time unit can be changed using the --time-unit option.]:FILE:_files' \
'(--show-output)*--output=[Control where the output of the benchmark is redirected. Note that some programs like '\''grep'\'' detect when standard output is /dev/null and apply certain optimizations. To avoid that, consider using '\''--output=pipe'\''.  <WHERE> can be\:    null\:     Redirect output to /dev/null (the default).    pipe\:     Feed the output through a pipe before discarding it.    inherit\:  Don'\''t redirect the output at all (same as '\''--show-output'\'').    <FILE>\:   Write the output to the given file.  This option can be specified once for all commands or multiple times, once for each command. Note\: If you want to log the output of each and every iteration, you can use a shell redirection and the '\''\$HYPERFINE_ITERATION'\'' environment variable\:     hyperfine '\''my-command > output-\${HYPERFINE_ITERATION}.log'\''  ]:WHERE:_default' \
'--input=[Control where the input of the benchmark comes from.  <WHERE> can be\:    null\:     Read from /dev/null (the default).    <FILE>\:   Read the input from the given file.]:WHERE:_default' \
'*-n+[Give a meaningful name to a command. This can be specified multiple times if several commands are benchmarked.]:NAME:_default' \
'*--command-name=[Give a meaningful name to a command. This can be specified multiple times if several commands are benchmarked.]:NAME:_default' \
'--min-benchmarking-time=[Set the minimum time (in seconds) to run benchmarks. Note that the number of benchmark runs is additionally influenced by the \`--min-runs\`, \`--max-runs\`, and \`--runs\` option.]: :_default' \
'(-S --shell --debug-mode)-N[An alias for '\''--shell=none'\''.]' \
'(--style)--show-output[Print the stdout and stderr of the benchmark instead of suppressing it. This will increase the time it takes for benchmarks to run, so it should only be used for debugging purposes or when trying to benchmark output speed.]' \
'--debug-mode[Enable debug mode which does not actually run commands, but returns fake times when the command is '\''sleep <time>'\''.]' \
'-h[Print help]' \
'--help[Print help]' \
'-V[Print version]' \
'--version[Print version]' \
':command -- The command to benchmark. This can be the name of an executable, a command line like "grep -i todo" or a shell command like "sleep 0.5 && echo test". The latter is only available if the shell is not explicitly disabled via '\''--shell=none'\''. If multiple commands are given, hyperfine will show a comparison of the respective runtimes.:_cmdstring' \
&& ret=0
}

(( $+functions[_hyperfine_commands] )) ||
_hyperfine_commands() {
    local commands; commands=()
    _describe -t commands 'hyperfine commands' commands "$@"
}

if [ "$funcstack[1]" = "_hyperfine" ]; then
    _hyperfine "$@"
else
    compdef _hyperfine hyperfine
fi
