leetcode_cli/cmds/
completions.rs1use super::Command;
4use crate::err::Error;
5use async_trait::async_trait;
6use clap::{Arg, ArgAction, ArgMatches, Command as ClapCommand};
7use clap_complete::{generate, Generator, Shell};
8
9pub struct CompletionCommand;
21
22#[async_trait]
23impl Command for CompletionCommand {
24 fn usage() -> ClapCommand {
26 ClapCommand::new("completions")
27 .about("Generate shell Completions")
28 .visible_alias("c")
29 .arg(
30 Arg::new("shell")
31 .action(ArgAction::Set)
32 .value_parser(clap::value_parser!(Shell)),
33 )
34 }
35
36 async fn handler(_m: &ArgMatches) -> Result<(), Error> {
37 println!("Don't use this handler. Does not implement the functionality to print completions. Use completions_handler() below.");
40 Ok(())
41 }
42}
43
44fn get_completions_string<G: Generator>(gen: G, cmd: &mut ClapCommand) -> Result<String, Error> {
45 let mut v: Vec<u8> = Vec::new();
46 let name = cmd.get_name().to_string();
47 generate(gen, cmd, name, &mut v);
48 Ok(String::from_utf8(v)?)
49}
50
51pub fn completion_handler(m: &ArgMatches, cmd: &mut ClapCommand) -> Result<(), Error> {
52 let shell = *m.get_one::<Shell>("shell").unwrap_or(
53 &Shell::from_env().ok_or(Error::MatchError)?,
55 );
56 let completions = get_completions_string(shell, cmd)?;
57 println!("{}", completions);
58 Ok(())
59}