Skip to content

Commit cb24752

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

File tree

4 files changed

+46
-3
lines changed

4 files changed

+46
-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: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,22 @@ 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+
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+
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+
Add completions to your shell config:
63+
```bash
64+
# Bash (add to ~/.bashrc)
65+
eval "$(ldk-server-cli completions bash)"
66+
67+
# Zsh (add to ~/.zshrc)
68+
eval "$(ldk-server-cli completions zsh)"
69+
70+
# Fish (add to ~/.config/fish/config.fish)
71+
ldk-server-cli completions fish | source
5672
```

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 = { version = "4.0", default-features = false }
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: 17 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,26 @@ enum Commands {
319320
)]
320321
cltv_expiry_delta: Option<u32>,
321322
},
323+
#[command(about = "Generate shell completions for the CLI")]
324+
Completions {
325+
#[arg(
326+
value_enum,
327+
help = "The shell to generate completions for (bash, zsh, fish, powershell, elvish)"
328+
)]
329+
shell: Shell,
330+
},
322331
}
323332

324333
#[tokio::main]
325334
async fn main() {
326335
let cli = Cli::parse();
327336

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

@@ -613,6 +628,7 @@ async fn main() {
613628
.await,
614629
);
615630
},
631+
Commands::Completions { .. } => unreachable!("Handled above"),
616632
}
617633
}
618634

0 commit comments

Comments
 (0)