diff --git a/Cargo.lock b/Cargo.lock index bb849fd..538c977 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -648,6 +648,15 @@ dependencies = [ "strsim", ] +[[package]] +name = "clap_complete" +version = "4.5.65" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "430b4dc2b5e3861848de79627b2bedc9f3342c7da5173a14eaa5d0f8dc18ae5d" +dependencies = [ + "clap", +] + [[package]] name = "clap_derive" version = "4.5.49" @@ -1760,6 +1769,7 @@ name = "ldk-server-cli" version = "0.1.0" dependencies = [ "clap", + "clap_complete", "hex-conservative 0.2.1", "ldk-server-client", "serde", diff --git a/README.md b/README.md index e3358c4..8497da5 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,22 @@ cargo run --bin ldk-server ./ldk-server/ldk-server-config.toml Interact with the node using CLI: ``` -./target/debug/ldk-server-cli -b localhost:3002 --api-key your-secret-api-key onchain-receive # To generate onchain-receive address. -./target/debug/ldk-server-cli -b localhost:3002 --api-key your-secret-api-key help # To print help/available commands. +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. +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. +``` + +### Shell Completions + +The CLI supports generating shell completions for Bash, Zsh, Fish, Elvish, and PowerShell. + +Add completions to your shell config: +```bash +# Bash (add to ~/.bashrc) +eval "$(ldk-server-cli completions bash)" + +# Zsh (add to ~/.zshrc) +eval "$(ldk-server-cli completions zsh)" + +# Fish (add to ~/.config/fish/config.fish) +ldk-server-cli completions fish | source ``` diff --git a/ldk-server-cli/Cargo.toml b/ldk-server-cli/Cargo.toml index 2ac0c4c..5d00600 100644 --- a/ldk-server-cli/Cargo.toml +++ b/ldk-server-cli/Cargo.toml @@ -6,6 +6,7 @@ edition = "2021" [dependencies] ldk-server-client = { path = "../ldk-server-client", features = ["serde"] } clap = { version = "4.0.5", default-features = false, features = ["derive", "std", "error-context", "suggestions", "help"] } +clap_complete = { version = "4.0", default-features = false } hex-conservative = { version = "0.2", default-features = false, features = ["std"] } tokio = { version = "1.38.0", default-features = false, features = ["rt-multi-thread", "macros"] } serde = "1.0" diff --git a/ldk-server-cli/src/main.rs b/ldk-server-cli/src/main.rs index e77cf83..00b10d7 100644 --- a/ldk-server-cli/src/main.rs +++ b/ldk-server-cli/src/main.rs @@ -9,7 +9,8 @@ use std::path::PathBuf; -use clap::{Parser, Subcommand}; +use clap::{CommandFactory, Parser, Subcommand}; +use clap_complete::{generate, Shell}; use config::{ get_default_api_key_path, get_default_cert_path, get_default_config_path, load_config, }; @@ -319,12 +320,26 @@ enum Commands { )] cltv_expiry_delta: Option, }, + #[command(about = "Generate shell completions for the CLI")] + Completions { + #[arg( + value_enum, + help = "The shell to generate completions for (bash, zsh, fish, powershell, elvish)" + )] + shell: Shell, + }, } #[tokio::main] async fn main() { let cli = Cli::parse(); + // short-circuit if generating completions + if let Commands::Completions { shell } = cli.command { + generate(shell, &mut Cli::command(), "ldk-server-cli", &mut std::io::stdout()); + return; + } + let config_path = cli.config.map(PathBuf::from).or_else(get_default_config_path); let config = config_path.as_ref().and_then(|p| load_config(p).ok()); @@ -613,6 +628,7 @@ async fn main() { .await, ); }, + Commands::Completions { .. } => unreachable!("Handled above"), } }