-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(completion): add completion command
- Loading branch information
1 parent
f32e46d
commit 417309a
Showing
5 changed files
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
use crate::cli::Cli; | ||
use clap::{Command, CommandFactory}; | ||
use clap_complete::{generator::generate, Generator, Shell}; | ||
|
||
#[derive(Debug, clap::Args)] | ||
pub struct CompletionArgs { | ||
#[arg(name = "shell", long = "shell", short)] | ||
shell: String, | ||
} | ||
|
||
fn get_command() -> Command { | ||
Cli::command() | ||
} | ||
|
||
fn print_completions<G: Generator>(gen: G, cmd: &mut Command) { | ||
generate(gen, cmd, cmd.get_name().to_string(), &mut std::io::stdout()); | ||
} | ||
|
||
fn get_shell_by_name(shell_name: &str) -> Option<Shell> { | ||
match shell_name { | ||
"zsh" => Some(Shell::Zsh), | ||
"bash" => Some(Shell::Bash), | ||
"fish" => Some(Shell::Fish), | ||
"powershell" => Some(Shell::PowerShell), | ||
"elvish" => Some(Shell::Elvish), | ||
_ => None, | ||
} | ||
} | ||
|
||
pub fn completion(args: CompletionArgs) { | ||
if let Some(shell) = get_shell_by_name(&args.shell) { | ||
let mut cmd = get_command(); | ||
print_completions(shell, &mut cmd); | ||
} else { | ||
eprintln!("No such shell was found: {}", &args.shell); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
pub mod action; | ||
pub mod cli; | ||
pub mod completion; | ||
pub mod config; | ||
pub mod cookie; | ||
pub mod endpoint; | ||
|