Skip to content

Commit b770a41

Browse files
feat: Implement shell completion using clap_complete
feat: Add shell completion instructions to README fix: Make completion field public and remove unused import refactor: Remove unused `Shell` import from `clap_complete` fix: Add missing completion field to simple_speedtest example
1 parent 45d41e2 commit b770a41

File tree

5 files changed

+48
-1
lines changed

5 files changed

+48
-1
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ serde = { version = "1.0.219", features = ["derive"] }
2020
csv = "1.3.0"
2121
serde_json = "1.0"
2222
indexmap = "2.7.0"
23+
clap_complete = "4.5"

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,32 @@ Example usage:
5858
Example with json-pretty output:
5959
[![asciicast](https://asciinema.org/a/P6IUAADtaCq3bT18GbYVHmksA.svg)](https://asciinema.org/a/P6IUAADtaCq3bT18GbYVHmksA)
6060

61+
### Shell Completion
62+
63+
`cfspeedtest` supports generating shell completion scripts. Use the `--generate-completion` flag followed by your shell name (e.g., `bash`, `zsh`, `fish`, `powershell`, `elvish`).
64+
65+
Example for bash (add to `~/.bashrc` or similar):
66+
```sh
67+
cfspeedtest --generate-completion bash > ~/.local/share/bash-completion/completions/cfspeedtest
68+
# Or, if you don't have a completions directory set up:
69+
# source <(cfspeedtest --generate-completion bash)
70+
```
71+
72+
Example for zsh (add to `~/.zshrc` or similar):
73+
```sh
74+
# Ensure your fpath includes a directory for completions, e.g., ~/.zfunc
75+
# mkdir -p ~/.zfunc
76+
# echo 'fpath=(~/.zfunc $fpath)' >> ~/.zshrc
77+
cfspeedtest --generate-completion zsh > ~/.zfunc/_cfspeedtest
78+
# You may need to run compinit:
79+
# autoload -U compinit && compinit
80+
```
81+
82+
Example for fish:
83+
```sh
84+
cfspeedtest --generate-completion fish > ~/.config/fish/completions/cfspeedtest.fish
85+
```
86+
6187

6288
## Development
6389

examples/simple_speedtest.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ fn main() {
1616
nr_latency_tests: 20,
1717
max_payload_size: PayloadSize::M10,
1818
disable_dynamic_max_payload_size: false,
19+
completion: None,
1920
};
2021

2122
let measurements = speed_test(reqwest::blocking::Client::new(), options);

src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::fmt;
66
use std::fmt::Display;
77

88
use clap::Parser;
9+
use clap_complete::Shell;
910
use speedtest::PayloadSize;
1011

1112
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
@@ -80,6 +81,10 @@ pub struct SpeedTestCLIOptions {
8081
/// Test upload speed only
8182
#[arg(long, conflicts_with = "download_only")]
8283
pub upload_only: bool,
84+
85+
/// Generate shell completion script for the specified shell
86+
#[arg(long = "generate-completion", value_enum)]
87+
pub completion: Option<Shell>,
8388
}
8489

8590
impl SpeedTestCLIOptions {

src/main.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,28 @@
11
use cfspeedtest::speedtest;
22
use cfspeedtest::OutputFormat;
33
use cfspeedtest::SpeedTestCLIOptions;
4-
use clap::Parser;
4+
use clap::{CommandFactory, Parser};
5+
use clap_complete::generate;
6+
use std::io;
57
use std::net::IpAddr;
68

79
use speedtest::speed_test;
810

11+
fn print_completions<G: clap_complete::Generator>(gen: G, cmd: &mut clap::Command) {
12+
generate(gen, cmd, cmd.get_name().to_string(), &mut io::stdout());
13+
}
14+
915
fn main() {
1016
env_logger::init();
1117
let options = SpeedTestCLIOptions::parse();
18+
19+
if let Some(generator) = options.completion {
20+
let mut cmd = SpeedTestCLIOptions::command();
21+
eprintln!("Generating completion script for {generator}...");
22+
print_completions(generator, &mut cmd);
23+
return;
24+
}
25+
1226
if options.output_format == OutputFormat::StdOut {
1327
println!("Starting Cloudflare speed test");
1428
}

0 commit comments

Comments
 (0)