Skip to main content

bw/platform/
completion.rs

1use clap::{Args, CommandFactory};
2use clap_complete::Shell;
3
4use crate::{
5    client_state::{AnyState, BwCommand},
6    command::Cli,
7    render::CommandResult,
8};
9
10#[derive(Args, Clone)]
11pub struct CompletionArgs {
12    #[arg(long, help = "The shell to generate completions for.")]
13    pub shell: Option<Shell>,
14}
15
16impl BwCommand for CompletionArgs {
17    type Client = AnyState;
18
19    async fn run(self, _: AnyState) -> CommandResult {
20        let Some(shell) = self.shell.or_else(Shell::from_env) else {
21            return Ok(
22                "Couldn't autodetect a valid shell. Run `bw completion --help` for more info."
23                    .into(),
24            );
25        };
26
27        let mut cmd = Cli::command();
28        let name = cmd.get_name().to_string();
29        clap_complete::generate(shell, &mut cmd, name, &mut std::io::stdout());
30        Ok(().into())
31    }
32}