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        clap_complete::generate(
28            shell,
29            &mut Cli::command(),
30            env!("CARGO_BIN_NAME"),
31            &mut std::io::stdout(),
32        );
33        Ok(().into())
34    }
35}