Add command for shell completions

This commit is contained in:
kpcyrd 2024-03-15 11:40:39 +01:00
parent 8b4f3afa42
commit 0f1ba8f5fe
3 changed files with 30 additions and 2 deletions

10
Cargo.lock generated
View file

@ -389,6 +389,15 @@ dependencies = [
"strsim",
]
[[package]]
name = "clap_complete"
version = "4.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a51919c5608a32e34ea1d6be321ad070065e17613e168c5b6977024290f2630b"
dependencies = [
"clap",
]
[[package]]
name = "clap_derive"
version = "4.4.7"
@ -2054,6 +2063,7 @@ dependencies = [
"anyhow",
"bytes",
"clap",
"clap_complete",
"console-subscriber",
"futures",
"librqbit",

View file

@ -29,6 +29,7 @@ tokio = {version = "1", features = ["macros", "rt-multi-thread"]}
console-subscriber = {version = "0.2", optional = true}
anyhow = "1"
clap = {version = "4", features = ["derive", "deprecated"]}
clap_complete = "4.4.5"
tracing = "0.1"
tracing-subscriber = {version = "0.3", features = ["env-filter"]}
regex = "1"

View file

@ -1,7 +1,8 @@
use std::{net::SocketAddr, path::PathBuf, sync::Arc, time::Duration};
use std::{io, net::SocketAddr, path::PathBuf, sync::Arc, time::Duration};
use anyhow::Context;
use clap::{Parser, ValueEnum};
use clap::{CommandFactory, Parser, ValueEnum};
use clap_complete::Shell;
use librqbit::{
api::ApiAddTorrentResponse,
http_api::{HttpApi, HttpApiOptions},
@ -172,6 +173,12 @@ impl From<&str> for InitialPeers {
}
}
#[derive(Parser)]
struct CompletionsOpts {
/// The shell to generate completions for
shell: Shell,
}
// server start
// download [--connect-to-existing] --output-folder(required) [file1] [file2]
@ -179,6 +186,7 @@ impl From<&str> for InitialPeers {
enum SubCommand {
Server(ServerOpts),
Download(DownloadOpts),
Completions(CompletionsOpts),
}
fn _start_deadlock_detector_thread() {
@ -519,5 +527,14 @@ async fn async_main(opts: Opts) -> anyhow::Result<()> {
}
}
}
SubCommand::Completions(completions_opts) => {
clap_complete::generate(
completions_opts.shell,
&mut Opts::command(),
"rqbit",
&mut io::stdout(),
);
Ok(())
}
}
}