Skip to content

Commit 509807b

Browse files
committed
Add support for bash completions
1 parent 00e3c57 commit 509807b

File tree

4 files changed

+52
-3
lines changed

4 files changed

+52
-3
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.

README.md

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,27 @@ cargo run --bin ldk-server ./ldk-server/ldk-server-config.toml
5151

5252
Interact with the node using CLI:
5353
```
54-
./target/debug/ldk-server-cli -b localhost:3002 --api-key your-secret-api-key onchain-receive # To generate onchain-receive address.
55-
./target/debug/ldk-server-cli -b localhost:3002 --api-key your-secret-api-key help # To print help/available commands.
54+
./target/debug/ldk-server-cli -b localhost:3002 --api-key your-secret-api-key --tls-cert /path/to/tls_cert.pem onchain-receive # To generate onchain-receive address.
55+
./target/debug/ldk-server-cli -b localhost:3002 --api-key your-secret-api-key --tls-cert /path/to/tls_cert.pem help # To print help/available commands.
56+
```
57+
58+
### Shell Completions
59+
60+
The CLI supports generating shell completions for Bash, Zsh, Fish, Elvish, and PowerShell.
61+
62+
Generate completions for your shell:
63+
```bash
64+
# Bash
65+
ldk-server-cli completions bash > ~/.local/share/bash-completion/completions/ldk-server-cli
66+
67+
# Zsh (add to your fpath)
68+
ldk-server-cli completions zsh > ~/.zfunc/_ldk-server-cli
69+
70+
# Fish
71+
ldk-server-cli completions fish > ~/.config/fish/completions/ldk-server-cli.fish
72+
```
73+
74+
Or source directly for the current session:
75+
```bash
76+
source <(ldk-server-cli completions bash)
5677
```

ldk-server-cli/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ edition = "2021"
66
[dependencies]
77
ldk-server-client = { path = "../ldk-server-client", features = ["serde"] }
88
clap = { version = "4.0.5", default-features = false, features = ["derive", "std", "error-context", "suggestions", "help"] }
9+
clap_complete = "4.0"
910
hex-conservative = { version = "0.2", default-features = false, features = ["std"] }
1011
tokio = { version = "1.38.0", default-features = false, features = ["rt-multi-thread", "macros"] }
1112
serde = "1.0"

ldk-server-cli/src/main.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99

1010
use std::path::PathBuf;
1111

12-
use clap::{Parser, Subcommand};
12+
use clap::{CommandFactory, Parser, Subcommand};
13+
use clap_complete::{generate, Shell};
1314
use config::{
1415
get_default_api_key_path, get_default_cert_path, get_default_config_path, load_config,
1516
};
@@ -319,12 +320,27 @@ enum Commands {
319320
)]
320321
cltv_expiry_delta: Option<u32>,
321322
},
323+
#[command(about = "Generate shell completions for the CLI")]
324+
Completions {
325+
/// The shell to generate completions for.
326+
#[arg(
327+
value_enum,
328+
help = "The shell to generate completions for (bash, zsh, fish, powershell, elvish)"
329+
)]
330+
shell: Shell,
331+
},
322332
}
323333

324334
#[tokio::main]
325335
async fn main() {
326336
let cli = Cli::parse();
327337

338+
// short-circuit if generating completions
339+
if let Commands::Completions { shell } = cli.command {
340+
generate(shell, &mut Cli::command(), "ldk-server-cli", &mut std::io::stdout());
341+
return;
342+
}
343+
328344
let config_path = cli.config.map(PathBuf::from).or_else(get_default_config_path);
329345
let config = config_path.as_ref().and_then(|p| load_config(p).ok());
330346

@@ -613,6 +629,7 @@ async fn main() {
613629
.await,
614630
);
615631
},
632+
Commands::Completions { .. } => unreachable!("Handled above"),
616633
}
617634
}
618635

0 commit comments

Comments
 (0)