Skip to content

Commit f262403

Browse files
Generate Shell completions (#388)
* feat: ✨ Add shell completions for espflash * docs: 📝 Update usage * feat: ✨ Use crate_name! macro from clap instead of hardcoded name * feat: ✨ Remove log level * build: ⚡️ Remove cargo feature from clap * feat: ✨ Add shell completions * docs: 📝 Add docstring for completions * docs: 📝 Document cargo-espflash completions * feat: ⚡️ Avoid duplicating code
1 parent 327f8d4 commit f262403

File tree

7 files changed

+48
-15
lines changed

7 files changed

+48
-15
lines changed

Cargo.lock

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cargo-espflash/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Usage: cargo espflash <COMMAND>
5454
5555
Commands:
5656
board-info Display information about the connected board and exit without flashing
57+
completions Generate completions for the given shell. Resulting completions have to be appended to cargo's completions
5758
flash Flash an application to a target device
5859
monitor Open the serial monitor without flashing
5960
partition-table Operations for partitions tables

cargo-espflash/src/main.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ use std::{
55
};
66

77
use cargo_metadata::Message;
8-
use clap::{Args, Parser, Subcommand};
8+
use clap::{Args, CommandFactory, Parser, Subcommand};
99
use espflash::{
1010
cli::{
11-
self, board_info, config::Config, connect, erase_partitions, flash_elf_image,
11+
self, board_info, completions, config::Config, connect, erase_partitions, flash_elf_image,
1212
monitor::monitor, parse_partition_table, partition_table, print_board_info,
13-
save_elf_as_image, serial_monitor, ConnectArgs, EspflashProgress, FlashConfigArgs,
14-
MonitorArgs, PartitionTableArgs,
13+
save_elf_as_image, serial_monitor, CompletionsArgs, ConnectArgs, EspflashProgress,
14+
FlashConfigArgs, MonitorArgs, PartitionTableArgs,
1515
},
1616
image_format::ImageFormatKind,
1717
logging::initialize_logger,
@@ -32,7 +32,7 @@ mod error;
3232
mod package_metadata;
3333

3434
#[derive(Debug, Parser)]
35-
#[clap(version, bin_name = "cargo", propagate_version = true)]
35+
#[clap(version, propagate_version = true)]
3636
struct Cli {
3737
#[clap(subcommand)]
3838
subcommand: CargoSubcommand,
@@ -50,6 +50,7 @@ enum CargoSubcommand {
5050
#[derive(Debug, Subcommand)]
5151
enum Commands {
5252
BoardInfo(ConnectArgs),
53+
Completions(CompletionsArgs),
5354
Flash(FlashArgs),
5455
Monitor(MonitorArgs),
5556
PartitionTable(PartitionTableArgs),
@@ -137,6 +138,7 @@ fn main() -> Result<()> {
137138
// associated arguments.
138139
match args {
139140
Commands::BoardInfo(args) => board_info(&args, &config),
141+
Commands::Completions(args) => completions(&args, &mut Cli::command(), "cargo"),
140142
Commands::Flash(args) => flash(args, &config),
141143
Commands::Monitor(args) => serial_monitor(args, &config),
142144
Commands::PartitionTable(args) => partition_table(args),

espflash/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ base64 = "0.21.0"
2929
binread = "2.2.0"
3030
bytemuck = { version = "1.12.3", features = ["derive"] }
3131
clap = { version = "4.0.32", features = ["derive", "env"], optional = true }
32+
clap_complete = { version = "4.1.5", optional = true }
3233
comfy-table = { version = "6.1.4", optional = true }
3334
crossterm = { version = "0.26.1", optional = true }
3435
ctrlc = { version = "3.2.5", optional = true }
@@ -60,6 +61,7 @@ default = ["cli"]
6061
cli = [
6162
"dep:addr2line",
6263
"dep:clap",
64+
"dep:clap_complete",
6365
"dep:comfy-table",
6466
"dep:crossterm",
6567
"dep:ctrlc",

espflash/README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ A command-line tool for flashing Espressif devices over serial
5454
Usage: espflash <COMMAND>
5555
5656
Commands:
57-
board-info Display information about the connected board and exit without flashing
57+
board-info Establish a connection with a target device
58+
completions Generate completions for the given shell
5859
flash Flash an application to a target device
5960
monitor Open the serial monitor without flashing
6061
partition-table Operations for partitions tables
@@ -63,8 +64,8 @@ Commands:
6364
help Print this message or the help of the given subcommand(s)
6465
6566
Options:
66-
-h, --help Print help information
67-
-V, --version Print version information
67+
-h, --help Print help
68+
-V, --version Print version
6869
```
6970

7071
### Cargo Runner

espflash/src/bin/espflash.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ use std::{
55
path::PathBuf,
66
};
77

8-
use clap::{Args, Parser, Subcommand};
8+
use clap::{Args, CommandFactory, Parser, Subcommand};
99
use espflash::{
1010
cli::{
11-
self, board_info, config::Config, connect, erase_partitions, flash_elf_image,
11+
self, board_info, completions, config::Config, connect, erase_partitions, flash_elf_image,
1212
monitor::monitor, parse_partition_table, partition_table, print_board_info,
13-
save_elf_as_image, serial_monitor, ConnectArgs, EspflashProgress, FlashConfigArgs,
14-
MonitorArgs, PartitionTableArgs,
13+
save_elf_as_image, serial_monitor, CompletionsArgs, ConnectArgs, EspflashProgress,
14+
FlashConfigArgs, MonitorArgs, PartitionTableArgs,
1515
},
1616
image_format::ImageFormatKind,
1717
logging::initialize_logger,
@@ -22,15 +22,16 @@ use log::{debug, LevelFilter};
2222
use miette::{IntoDiagnostic, Result, WrapErr};
2323

2424
#[derive(Debug, Parser)]
25-
#[clap(about, version, propagate_version = true)]
26-
struct Cli {
27-
#[clap(subcommand)]
25+
#[command(about, version, propagate_version = true)]
26+
pub struct Cli {
27+
#[command(subcommand)]
2828
subcommand: Commands,
2929
}
3030

3131
#[derive(Debug, Subcommand)]
3232
enum Commands {
3333
BoardInfo(ConnectArgs),
34+
Completions(CompletionsArgs),
3435
Flash(FlashArgs),
3536
Monitor(MonitorArgs),
3637
PartitionTable(PartitionTableArgs),
@@ -107,6 +108,7 @@ fn main() -> Result<()> {
107108
// associated arguments.
108109
match args {
109110
Commands::BoardInfo(args) => board_info(&args, &config),
111+
Commands::Completions(args) => completions(&args, &mut Cli::command(), "espflash"),
110112
Commands::Flash(args) => flash(args, &config),
111113
Commands::Monitor(args) => serial_monitor(args, &config),
112114
Commands::PartitionTable(args) => partition_table(args),

espflash/src/cli/mod.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use std::{
1515
};
1616

1717
use clap::Args;
18+
use clap_complete::Shell;
1819
use comfy_table::{modifiers, presets::UTF8_FULL, Attribute, Cell, Color, Table};
1920
use esp_idf_part::{DataType, Partition, PartitionTable};
2021
use indicatif::{style::ProgressStyle, HumanCount, ProgressBar};
@@ -59,6 +60,13 @@ pub struct ConnectArgs {
5960
pub no_stub: bool,
6061
}
6162

63+
/// Generate completions for the given shell
64+
#[derive(Debug, Args)]
65+
pub struct CompletionsArgs {
66+
/// Shell to generate completions for.
67+
pub shell: Shell,
68+
}
69+
6270
/// Configure communication with the target device's flash
6371
#[derive(Debug, Args)]
6472
pub struct FlashConfigArgs {
@@ -217,6 +225,13 @@ pub fn board_info(args: &ConnectArgs, config: &Config) -> Result<()> {
217225
Ok(())
218226
}
219227

228+
/// Generate shell completions for the given shell
229+
pub fn completions(args: &CompletionsArgs, app: &mut clap::Command, bin_name: &str) -> Result<()> {
230+
clap_complete::generate(args.shell, app, bin_name, &mut std::io::stdout());
231+
232+
Ok(())
233+
}
234+
220235
/// Print information about a chip
221236
pub fn print_board_info(flasher: &mut Flasher) -> Result<()> {
222237
let info = flasher.device_info()?;

0 commit comments

Comments
 (0)