Skip to content

Commit 7e55f97

Browse files
authored
Merge pull request #105 from benthecarman/bash-completion
Add support for bash completions
2 parents c929bd5 + cb24752 commit 7e55f97

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
};
@@ -335,12 +336,26 @@ enum Commands {
335336
)]
336337
cltv_expiry_delta: Option<u32>,
337338
},
339+
#[command(about = "Generate shell completions for the CLI")]
340+
Completions {
341+
#[arg(
342+
value_enum,
343+
help = "The shell to generate completions for (bash, zsh, fish, powershell, elvish)"
344+
)]
345+
shell: Shell,
346+
},
338347
}
339348

340349
#[tokio::main]
341350
async fn main() {
342351
let cli = Cli::parse();
343352

353+
// short-circuit if generating completions
354+
if let Commands::Completions { shell } = cli.command {
355+
generate(shell, &mut Cli::command(), "ldk-server-cli", &mut std::io::stdout());
356+
return;
357+
}
358+
344359
let config_path = cli.config.map(PathBuf::from).or_else(get_default_config_path);
345360
let config = config_path.as_ref().and_then(|p| load_config(p).ok());
346361

@@ -658,6 +673,7 @@ async fn main() {
658673
.await,
659674
);
660675
},
676+
Commands::Completions { .. } => unreachable!("Handled above"),
661677
}
662678
}
663679

0 commit comments

Comments
 (0)