Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ features = [
"error-context",
]

[dependencies.clap_complete]
version = "4.2.1"

[dev-dependencies]
approx = "0.5"
assert_cmd = "2.0"
Expand Down
19 changes: 15 additions & 4 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use std::ffi::OsString;

use clap::{
builder::NonEmptyStringValueParser, crate_version, Arg, ArgAction, ArgMatches, Command,
ValueHint,
builder::{NonEmptyStringValueParser, PossibleValuesParser},
crate_version, Arg, ArgAction, ArgMatches, Command, ValueHint,
};

const SHELLS: [&str; 5] = ["bash", "zsh", "fish", "powershell", "elvish"];

pub fn get_cli_arguments<'a, I, T>(args: I) -> ArgMatches
where
I: IntoIterator<Item = T>,
Expand All @@ -15,7 +17,7 @@ where
}

/// Build the clap command for parsing command line arguments
fn build_command() -> Command {
pub fn build_command() -> Command {
Command::new("hyperfine")
.version(crate_version!())
.next_line_help(true)
Expand All @@ -30,7 +32,7 @@ fn build_command() -> Command {
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.")
.required(true)
.required_unless_present("generate-completions")
.action(ArgAction::Append)
.value_hint(ValueHint::CommandString)
.value_parser(NonEmptyStringValueParser::new()),
Expand Down Expand Up @@ -406,6 +408,15 @@ fn build_command() -> Command {
.hide(true)
.help("Enable debug mode which does not actually run commands, but returns fake times when the command is 'sleep <time>'.")
)
.arg(
Arg::new("generate-completions")
.long("generate-completions")
.action(ArgAction::Set)
.value_name("SHELL")
.value_parser(PossibleValuesParser::new(SHELLS))
.hide(true)
.help("Generate a shell completions script for the given shell and print it to stdout.")
)
}

#[test]
Expand Down
33 changes: 29 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
use std::env;

use benchmark::scheduler::Scheduler;
use cli::get_cli_arguments;
use clap::ArgMatches;
use clap_complete::Shell;
use cli::{build_command, get_cli_arguments};
use command::Commands;
use export::ExportManager;
use options::Options;
Expand All @@ -32,10 +34,22 @@ fn run() -> Result<()> {
colored::control::set_virtual_terminal(true).unwrap();

let cli_arguments = get_cli_arguments(env::args_os());
let mut options = Options::from_cli_arguments(&cli_arguments)?;
let commands = Commands::from_cli_arguments(&cli_arguments)?;

if let Some(shell) = cli_arguments.get_one::<String>("generate-completions") {
let shell = parse_shell(shell)?;
let mut command = build_command();
clap_complete::generate(shell, &mut command, "hyperfine", &mut std::io::stdout());
return Ok(());
}

run_benchmarks(&cli_arguments)
}

fn run_benchmarks(cli_arguments: &ArgMatches) -> Result<()> {
let mut options = Options::from_cli_arguments(cli_arguments)?;
let commands = Commands::from_cli_arguments(cli_arguments)?;
let export_manager = ExportManager::from_cli_arguments(
&cli_arguments,
cli_arguments,
options.time_unit,
options.sort_order_exports,
)?;
Expand All @@ -50,6 +64,17 @@ fn run() -> Result<()> {
Ok(())
}

fn parse_shell(name: &str) -> Result<Shell> {
match name {
"bash" => Ok(Shell::Bash),
"zsh" => Ok(Shell::Zsh),
"fish" => Ok(Shell::Fish),
"powershell" => Ok(Shell::PowerShell),
"elvish" => Ok(Shell::Elvish),
other => Err(anyhow::anyhow!("unsupported shell: {other}")),
}
}

fn main() {
match run() {
Ok(_) => {}
Expand Down
42 changes: 42 additions & 0 deletions tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -722,3 +722,45 @@ fn windows_quote_before_quote_args() {
.assert()
.success();
}

#[test]
fn generates_fish_completions() {
hyperfine()
.arg("--generate-completions")
.arg("fish")
.assert()
.success()
.stdout(predicate::str::starts_with("complete -c hyperfine"));
}

#[test]
fn generates_bash_completions() {
hyperfine()
.arg("--generate-completions")
.arg("bash")
.assert()
.success()
.stdout(predicate::str::contains("_hyperfine"));
}

#[test]
fn generates_zsh_completions() {
hyperfine()
.arg("--generate-completions")
.arg("zsh")
.assert()
.success()
.stdout(predicate::str::contains("#compdef"));
}

#[test]
fn rejects_unknown_shell() {
hyperfine()
.arg("--generate-completions")
.arg("csh")
.assert()
.failure()
.stderr(predicate::str::contains(
"invalid value 'csh' for '--generate-completions <SHELL>'",
));
}