-
Notifications
You must be signed in to change notification settings - Fork 8
feat(cli): add shell completion for bash, zsh, fish, and PowerShell #100
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8f15396
feat(cli): add shell completion for bash, zsh, fish, and PowerShell
ruivieira 0c31da3
fix: Linting
ruivieira 0834d6c
fix(cli): implement working PowerShell completion and upgrade mypy to…
ruivieira f2eae94
fix: Linting
ruivieira 77c1a38
fix(cli): use dedicated file for PowerShell completions instead of ap…
ruivieira File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,93 @@ | ||
| """Shell completion script generation for the evalhub CLI.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import click | ||
|
|
||
| _POWERSHELL_SCRIPT = """\ | ||
| Register-ArgumentCompleter -Native -CommandName evalhub -ScriptBlock { | ||
| param($wordToComplete, $commandAst, $cursorPosition) | ||
| $env:_EVALHUB_COMPLETE = 'powershell_complete' | ||
| $env:COMP_WORDS = $commandAst.ToString() | ||
| $env:COMP_CWORD = $cursorPosition | ||
| evalhub | ForEach-Object { | ||
| $type, $value, $help = $_ -split '\\t', 3 | ||
| [System.Management.Automation.CompletionResult]::new( | ||
| $value, $value, | ||
| $(if ($type -eq 'dir') { 'ProviderContainer' } | ||
| elseif ($type -eq 'file') { 'ProviderItem' } | ||
| else { 'ParameterValue' }), | ||
| $(if ($help) { $help } else { $value }) | ||
| ) | ||
| } | ||
| Remove-Item Env:_EVALHUB_COMPLETE | ||
| Remove-Item Env:COMP_WORDS | ||
| Remove-Item Env:COMP_CWORD | ||
| } | ||
| """ | ||
|
|
||
|
|
||
| def _get_completion_script(shell: str) -> str: | ||
| """Generate a completion script for the given shell using Click internals.""" | ||
| from click.shell_completion import get_completion_class | ||
|
|
||
| from evalhub.cli.main import main as cli | ||
|
|
||
| cls = get_completion_class(shell) | ||
| if cls is None: | ||
| raise click.ClickException(f"Unsupported shell for Click completion: {shell}") | ||
| comp = cls(cli, {}, "evalhub", "_EVALHUB_COMPLETE") | ||
| return comp.source() | ||
|
|
||
|
|
||
| @click.group("completion") | ||
| def completion() -> None: | ||
| """Generate shell completion scripts. | ||
|
|
||
| \b | ||
| Supported shells: bash, zsh, fish, powershell. | ||
|
|
||
| \b | ||
| Quick setup: | ||
| eval "$(evalhub completion bash)" # bash (current session) | ||
| eval "$(evalhub completion zsh)" # zsh (current session) | ||
| evalhub completion fish | source # fish (current session) | ||
|
|
||
| \b | ||
| Persistent setup: | ||
| # bash | ||
| evalhub completion bash > ~/.local/share/bash-completion/completions/evalhub | ||
|
|
||
| # zsh (add the directory to fpath before compinit in ~/.zshrc) | ||
| evalhub completion zsh > ~/.zfunc/_evalhub | ||
|
|
||
| # fish | ||
| evalhub completion fish > ~/.config/fish/completions/evalhub.fish | ||
|
|
||
| # powershell (add to $PROFILE) | ||
| evalhub completion powershell >> $PROFILE | ||
| """ | ||
|
|
||
|
|
||
| @completion.command("bash") | ||
| def completion_bash() -> None: | ||
| """Generate bash completion script.""" | ||
| click.echo(_get_completion_script("bash")) | ||
|
|
||
|
|
||
| @completion.command("zsh") | ||
| def completion_zsh() -> None: | ||
| """Generate zsh completion script.""" | ||
| click.echo(_get_completion_script("zsh")) | ||
|
|
||
|
|
||
| @completion.command("fish") | ||
| def completion_fish() -> None: | ||
| """Generate fish completion script.""" | ||
| click.echo(_get_completion_script("fish")) | ||
|
|
||
|
|
||
| @completion.command("powershell") | ||
| def completion_powershell() -> None: | ||
| """Generate PowerShell completion script.""" | ||
| click.echo(_POWERSHELL_SCRIPT) | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.