Skip to content

Commit

Permalink
add CLI option to identify a command with a custom name
Browse files Browse the repository at this point in the history
CLI option `--name` can be used to define the name to identify a
command. If not passed, then the command itself is used. Commands
and names are paired in the same order: The first command executed gets
the first name passed as option.

Close sharkdp#326
  • Loading branch information
scampi committed Oct 14, 2020
1 parent 8973778 commit 4183b8c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/hyperfine/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,16 @@ fn build_app() -> App<'static, 'static> {
when trying to benchmark output speed.",
),
)
.arg(
Arg::with_name("command-name")
.long("name")
.short("n")
.takes_value(true)
.multiple(true)
.number_of_values(1)
.value_name("NAME")
.help("Give a meaningful name to a command"),
)
.help_message("Print this help message.")
.version_message("Show version information.")
}
12 changes: 10 additions & 2 deletions src/hyperfine/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,20 @@ pub fn run_benchmark(
shell_spawning_time: TimingResult,
options: &HyperfineOptions,
) -> io::Result<BenchmarkResult> {
let shell_cmd = cmd.get_shell_command();
let command_name = if let Some(names) = &options.names {
names.get(num).unwrap_or(&shell_cmd)
} else {
&shell_cmd
};
let command_name = command_name.to_string();

if options.output_style != OutputStyleOption::Disabled {
println!(
"{}{}: {}",
"Benchmark #".bold(),
(num + 1).to_string().bold(),
cmd
&command_name
);
}

Expand Down Expand Up @@ -415,7 +423,7 @@ pub fn run_benchmark(
run_cleanup_command(&options.shell, &cleanup_cmd, options.show_output)?;

Ok(BenchmarkResult::new(
cmd.get_shell_command(),
command_name,
t_mean,
t_stddev,
t_median,
Expand Down
3 changes: 3 additions & 0 deletions src/hyperfine/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,14 @@ pub struct HyperfineOptions {

/// Which time unit to use for CLI & Markdown output
pub time_unit: Option<Unit>,

pub names: Option<Vec<String>>,
}

impl Default for HyperfineOptions {
fn default() -> HyperfineOptions {
HyperfineOptions {
names: None,
warmup_count: 0,
runs: Runs::default(),
min_time_sec: 3.0,
Expand Down
4 changes: 4 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ fn build_hyperfine_options(matches: &ArgMatches<'_>) -> Result<HyperfineOptions,
(None, None) => {}
};

options.names = matches
.values_of("command-name")
.map(|values| values.map(String::from).collect::<Vec<String>>());

options.preparation_command = matches
.values_of("prepare")
.map(|values| values.map(String::from).collect::<Vec<String>>());
Expand Down

0 comments on commit 4183b8c

Please sign in to comment.