Skip to content

Commit 1ede3f5

Browse files
author
Gökhan Kocak
committed
feat: Add dynamic completions in clap library
Fixes #1432 This implements dynamic completions of clap with https://docs.rs/clap_complete/latest/clap_complete/env/index.html. It's an unstable-dynamic feature for clap_complete which has to be explicitly enabled.
1 parent 28247a8 commit 1ede3f5

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

Cargo.lock

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ base64 = "0.22"
2323
bytes = "^1.11"
2424
chrono = { version = "^0.4", default-features = false, features = ["clock", "serde"] }
2525
clap = { version = "^4.5", features = ["cargo", "color", "derive", "env"] }
26-
clap_complete = { version = "^4.5" }
26+
clap_complete = { version = "^4.5", features = ["unstable-dynamic"] }
2727
color-eyre = { version = "^0.6", features = ["default", "issue-url", "track-caller"] }
2828
config = { version = "^0.15", default-features = false }
2929
derive_builder = { version = "^0.20" }

openstack_cli/src/bin/osc.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
//! The binary of the CLI
1818
#![deny(missing_docs)]
1919

20+
use clap::CommandFactory;
21+
use clap_complete::CompleteEnv;
2022
use color_eyre::eyre::{Report, Result};
2123
use color_eyre::owo_colors::OwoColorize;
2224
use color_eyre::section::PanicMessage;
@@ -28,7 +30,17 @@ use openstack_cli::error::OpenStackCliError;
2830
#[tokio::main]
2931
async fn main() -> Result<(), Report> {
3032
initialize_panic_handler()?;
31-
openstack_cli::entry_point().await?;
33+
34+
let args = env::args_os();
35+
match CompleteEnv::with_factory(openstack_cli::Cli::command).try_complete(args, None) {
36+
Ok(true) => {
37+
return Ok(());
38+
}
39+
Ok(false) | Err(_) => {
40+
openstack_cli::entry_point().await?;
41+
}
42+
}
43+
3244
Ok(())
3345
}
3446

0 commit comments

Comments
 (0)