-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathcompletions.rs
More file actions
35 lines (32 loc) · 1.01 KB
/
completions.rs
File metadata and controls
35 lines (32 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//! Completions command
use crate::err::Error;
use clap::{Args, Command as ClapCommand};
use clap_complete::{Generator, Shell, generate};
/// Completions command arguments
#[derive(Args)]
pub struct CompletionsArgs {
/// Shell type [possible values: bash, elvish, fish, powershell, zsh]
#[arg(value_parser = clap::value_parser!(Shell))]
pub shell: Option<Shell>,
}
fn get_completions_string<G: Generator>(
generator: G,
cmd: &mut ClapCommand,
) -> Result<String, Error> {
let mut v: Vec<u8> = Vec::new();
let name = cmd.get_name().to_string();
generate(generator, cmd, name, &mut v);
Ok(String::from_utf8(v)?)
}
impl CompletionsArgs {
/// Generate and print shell completions
pub fn run(&self, cmd: &mut ClapCommand) -> Result<(), Error> {
let shell = self
.shell
.or_else(Shell::from_env)
.ok_or(Error::MatchError)?;
let completions = get_completions_string(shell, cmd)?;
println!("{}", completions);
Ok(())
}
}