Skip to content

Commit 82e6628

Browse files
committed
feat: add command for completions
1 parent b9cf2ac commit 82e6628

5 files changed

Lines changed: 57 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ page. See [DEVELOPMENT_CYCLE.md](DEVELOPMENT_CYCLE.md) for more details.
66
## [Unreleased]
77
- Add wallet subcommand `config` to save wallet configs
88
- Add `wallets` command to list all wallets saved configs
9+
- Added `completions` subcommand to generate shell completions
910

1011
## [2.0.0]
1112

1213
- Removed MSRV and bumped Rust Edition to 2024
13-
- Add `--pretty` flag for formatting outputs in human-readable form
14+
- Added `--pretty` flag for formatting outputs in human-readable form
1415
- Updated `bdk_wallet ` to `2.1.0`, `bdk_bitcoind_rpc` to `0.21.0`, `bdk_esplora` to `0.22.1`, `bdk_kyoto` to `0.13.1`
1516
- Updated `tracing-subscriber` to 0.3.20
1617
- Added `tr` script type to `compile` command to support creating taproot descriptors

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.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ license = "MIT"
1414
[dependencies]
1515
bdk_wallet = { version = "2.1.0", features = ["rusqlite", "keys-bip39", "compiler", "std"] }
1616
clap = { version = "4.5", features = ["derive","env"] }
17+
clap_complete = "4.5"
1718
dirs = { version = "6.0.0" }
1819
env_logger = "0.11.6"
1920
log = "0.4"

src/commands.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use bdk_wallet::bitcoin::{
1818
bip32::{DerivationPath, Xpriv},
1919
};
2020
use clap::{Args, Parser, Subcommand, ValueEnum, value_parser};
21+
use clap_complete::Shell;
2122

2223
#[cfg(any(feature = "electrum", feature = "esplora", feature = "rpc"))]
2324
use crate::utils::parse_proxy_auth;
@@ -127,7 +128,39 @@ pub enum CliSubCommand {
127128
},
128129
/// List all saved wallet configurations.
129130
Wallets,
131+
/// Generate tab-completion scripts for your shell.
132+
///
133+
/// Outputs a shell-specific completion script to stdout.
134+
/// To enable completions you need to redirect this output into the appropriate location for your shell.
135+
///
136+
/// Bash:
137+
/// bdk-cli completions bash > ~/.local/share/bash-completion/completions/bdk-cli
138+
///
139+
/// Zsh:
140+
/// bdk-cli completions zsh > ~/.zfunc/_bdk-cli
141+
/// # Make sure ~/.zfunc is in your fpath (add to .zshrc):
142+
/// # fpath=(~/.zfunc $fpath)
143+
/// # autoload -Uz compinit && compinit
144+
///
145+
/// Fish:
146+
/// bdk-cli completions fish > ~/.config/fish/completions/bdk-cli.fish
147+
///
148+
/// PowerShell:
149+
/// bdk-cli completions powershell >> $PROFILE
150+
///
151+
/// Elvish:
152+
/// bdk-cli completions elvish >> ~/.elvish/rc.elv
153+
///
154+
/// After installing the completion script, restart your shell or source
155+
/// the configuration file for the changes to take effect.
156+
#[command(verbatim_doc_comment)]
157+
Completions {
158+
/// Target shell syntax
159+
#[arg(value_enum)]
160+
shell: Shell,
161+
},
130162
}
163+
131164
/// Wallet operation subcommands.
132165
#[derive(Debug, Subcommand, Clone, PartialEq)]
133166
pub enum WalletSubCommand {

src/handlers.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ use bdk_wallet::{
4444
descriptor::{Descriptor, Legacy, Miniscript},
4545
miniscript::{Tap, descriptor::TapTree, policy::Concrete},
4646
};
47+
use clap::CommandFactory;
4748
use cli_table::{Cell, CellStruct, Style, Table, format::Justify};
4849
use serde_json::json;
4950
#[cfg(feature = "cbf")]
@@ -1422,6 +1423,16 @@ pub(crate) async fn handle_command(cli_opts: CliOpts) -> Result<String, Error> {
14221423
let descriptor = handle_descriptor_command(cli_opts.network, desc_type, key, pretty)?;
14231424
Ok(descriptor)
14241425
}
1426+
CliSubCommand::Completions { shell } => {
1427+
clap_complete::generate(
1428+
shell,
1429+
&mut CliOpts::command(),
1430+
"bdk-cli",
1431+
&mut std::io::stdout(),
1432+
);
1433+
1434+
Ok("".to_string())
1435+
}
14251436
};
14261437
result
14271438
}

0 commit comments

Comments
 (0)