-
Notifications
You must be signed in to change notification settings - Fork 355
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add shell completion #515
Add shell completion #515
Conversation
Codecov Report
@@ Coverage Diff @@
## main #515 +/- ##
==========================================
- Coverage 60.91% 60.89% -0.02%
==========================================
Files 87 87
Lines 12658 12661 +3
==========================================
Hits 7710 7710
- Misses 4948 4951 +3 |
@creepinson |
You're right, I had used vscode's rust-analyzer formatter assuming that would work fine. |
use clap_generate::{generate, Generator, Shell}; | ||
use std::io; | ||
|
||
#[derive(Debug, Parser)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
youki -h currently does not have a description for this command
#[derive(Debug, Parser)] | |
/// Generate scripts for shell completion | |
#[derive(Debug, Parser)] |
pub generator: Shell, | ||
} | ||
|
||
pub fn print_completions<G: Generator>(gen: G, app: &mut App) -> Result<(), anyhow::Error> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about using anyhow::Result
?
crates/youki/src/main.rs
Outdated
SubCommand::Completion(parser) => { | ||
commands::completion::print_completions(parser.generator, &mut app) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SubCommand::Completion(parser) => { | |
commands::completion::print_completions(parser.generator, &mut app) | |
} | |
SubCommand::Completion(completion) => { | |
commands::completion::completion(completion, &mut app) | |
} |
pub struct CompletionParser { | ||
#[clap(long = "generator", short = 'g', arg_enum)] | ||
pub generator: Shell, | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pub struct CompletionParser { | |
#[clap(long = "generator", short = 'g', arg_enum)] | |
pub generator: Shell, | |
} | |
pub struct Completion { | |
#[clap(long = "shell", short = 's', arg_enum)] | |
pub shell: Shell, | |
} |
crates/youki/src/main.rs
Outdated
@@ -46,6 +46,7 @@ enum SubCommand { | |||
|
|||
// Youki specific extensions | |||
Info(info::Info), | |||
Completion(commands::completion::CompletionParser), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Completion(commands::completion::CompletionParser), | |
Completion(completion::Completion), |
pub fn print_completions<G: Generator>(gen: G, app: &mut App) -> Result<(), anyhow::Error> { | ||
generate(gen, app, app.get_name().to_string(), &mut io::stdout()); | ||
|
||
Ok(()) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pub fn print_completions<G: Generator>(gen: G, app: &mut App) -> Result<(), anyhow::Error> { | |
generate(gen, app, app.get_name().to_string(), &mut io::stdout()); | |
Ok(()) | |
} | |
pub fn print_completions(args: Completion, app: &mut App) -> Result<()> { | |
generate(args.shell, app, app.get_name().to_string(), &mut io::stdout()); | |
Ok(()) | |
} |
Minor stuff, the style should be consistent with the other commands. |
- Make completion command consistent with the style of the other commands - Rename the "generator" argument to "shell" - Use the anyhow::Result type
Looks like I messed up the order of the comment and the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
Great! |
Changes
I added shell completion for zsh, bash or any other shell that clap_generate supports.
This is how I used it in my setup with zsh: