Skip to content

Commit 5db2be7

Browse files
committed
Merge #237: feat: add completions subcommand
f32fc68 docs(readme): add note about updating completions (va-an) e173b47 docs(completions): edit help output (Vadim Anufriev) ba0bef1 docs(readme): add completions block to readme (Vadim Anufriev) c447159 chore(clippy): fix clippy warnings again (Vadim Anufriev) 7c1303a chore(clippy): resolve clippy warngins (Vadim Anufriev) 82e6628 feat: add command for completions (Vadim Anufriev) Pull request description: ### Description Resolves #234. Added `completions` subcommand to generate shell completions. ### How to test Install bdk-cli: ```bash cargo install --path . --all-features ``` See how to install completions for your shell: ```bash bdk-cli completions --help ``` For example, to install completions for zsh: ```bash bdk-cli completions zsh > ~/.zfunc/_bdk-cli ``` After restarting the shell, completions should work with Tab. ### Checklists #### All Submissions: * [x] I've signed all my commits * [x] I followed the [contribution guidelines](https://github.com/bitcoindevkit/bdk-cli/blob/master/CONTRIBUTING.md) * [x] I ran `cargo fmt` and `cargo clippy` before committing #### New Features: * [ ] I've added tests for the new feature * [x] I've added docs for the new feature * [x] I've updated `CHANGELOG.md` ACKs for top commit: notmandatory: utACK f32fc68 Tree-SHA512: f033cb9624a9613356de8ef8f51576257f7f5ddd661d54853f3005294cd449d64810592317f3f62085adaccbc963f69792943bb2450b282cc13881b382855b1a
2 parents a501434 + f32fc68 commit 5db2be7

File tree

6 files changed

+90
-1
lines changed

6 files changed

+90
-1
lines changed

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"

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,16 @@ cargo run -- --pretty -n signet wallet -w {wallet_name} balance
238238
```
239239
This is available for wallet, key, repl and compile features. When ommitted, outputs default to `JSON`.
240240

241+
## Shell Completions
242+
243+
`bdk-cli` supports generating shell completions for Bash, Zsh, Fish, Elvish, and PowerShell. For setup instructions, run:
244+
245+
```shell
246+
bdk-cli completions --help
247+
```
248+
249+
Completion files are generated from the currently installed binary. After upgrading `bdk-cli`, re-run the `completions` command to keep them in sync with the new version.
250+
241251
## Saving and using wallet configurations
242252

243253
The `wallet config` sub-command allows you to save wallet settings to a `config.toml` file in the default directory (`~/.bdk-bitcoin/`) or custom directory specified with the `--datadir` flag. This eliminate the need to repeatedly specify descriptors, client types, and other parameters for each command. Once configured, you can use any wallet command by simply specifying the wallet name. All other parameters are automatically loaded from the saved configuration.

src/commands.rs

Lines changed: 56 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,62 @@ pub enum CliSubCommand {
127128
},
128129
/// List all saved wallet configurations.
129130
Wallets,
131+
/// Generate tab-completion scripts for your shell.
132+
///
133+
/// The completion script is output on stdout, allowing you to redirect
134+
/// it to a file of your choosing. Where you place the file will depend
135+
/// on your shell and operating system.
136+
///
137+
/// Here are common setups for supported shells:
138+
///
139+
/// Bash:
140+
///
141+
/// Completion files are commonly stored in
142+
/// `~/.local/share/bash-completion/completions` for user-specific commands.
143+
/// Run the commands:
144+
///
145+
/// $ mkdir -p ~/.local/share/bash-completion/completions
146+
/// $ bdk-cli completions bash > ~/.local/share/bash-completion/completions/bdk-cli
147+
///
148+
/// Zsh:
149+
///
150+
/// Completion files are commonly stored in a directory listed in your `fpath`.
151+
/// Run the commands:
152+
///
153+
/// $ mkdir -p ~/.zfunc
154+
/// $ bdk-cli completions zsh > ~/.zfunc/_bdk-cli
155+
///
156+
/// Make sure `~/.zfunc` is in your fpath by adding to your `.zshrc`:
157+
///
158+
/// fpath=(~/.zfunc $fpath)
159+
/// autoload -Uz compinit && compinit
160+
///
161+
/// Fish:
162+
///
163+
/// Completion files are commonly stored in
164+
/// `~/.config/fish/completions`. Run the commands:
165+
///
166+
/// $ mkdir -p ~/.config/fish/completions
167+
/// $ bdk-cli completions fish > ~/.config/fish/completions/bdk-cli.fish
168+
///
169+
/// PowerShell:
170+
///
171+
/// $ bdk-cli completions powershell >> $PROFILE
172+
///
173+
/// Elvish:
174+
///
175+
/// $ bdk-cli completions elvish >> ~/.elvish/rc.elv
176+
///
177+
/// After installing the completion script, restart your shell or source
178+
/// the configuration file for the changes to take effect.
179+
#[command(verbatim_doc_comment)]
180+
Completions {
181+
/// Target shell syntax
182+
#[arg(value_enum)]
183+
shell: Shell,
184+
},
130185
}
186+
131187
/// Wallet operation subcommands.
132188
#[derive(Debug, Subcommand, Clone, PartialEq)]
133189
pub enum WalletSubCommand {

src/handlers.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ use bdk_wallet::{
4646
descriptor::{Descriptor, Legacy, Miniscript},
4747
miniscript::{Tap, descriptor::TapTree, policy::Concrete},
4848
};
49+
use clap::CommandFactory;
4950
use cli_table::{Cell, CellStruct, Style, Table, format::Justify};
5051
use serde_json::json;
5152

@@ -1423,6 +1424,16 @@ pub(crate) async fn handle_command(cli_opts: CliOpts) -> Result<String, Error> {
14231424
let descriptor = handle_descriptor_command(cli_opts.network, desc_type, key, pretty)?;
14241425
Ok(descriptor)
14251426
}
1427+
CliSubCommand::Completions { shell } => {
1428+
clap_complete::generate(
1429+
shell,
1430+
&mut CliOpts::command(),
1431+
"bdk-cli",
1432+
&mut std::io::stdout(),
1433+
);
1434+
1435+
Ok("".to_string())
1436+
}
14261437
};
14271438
result
14281439
}

0 commit comments

Comments
 (0)