-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rename autocompletion to shell_complete
new function takes additional param arg, must return a homogeneous list of strings or CompletionItem, and must perform matching on results
- Loading branch information
Showing
10 changed files
with
166 additions
and
85 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
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,28 @@ | ||
$ completion | ||
============ | ||
|
||
Demonstrates Click's shell completion support. | ||
|
||
.. code-block:: bash | ||
|
||
pip install --editable . | ||
|
||
For Bash: | ||
|
||
.. code-block:: bash | ||
|
||
eval "$(_COMPLETION_COMPLETE=source_bash completion)" | ||
|
||
For Zsh: | ||
|
||
.. code-block:: zsh | ||
|
||
eval "$(_COMPLETION_COMPLETE=source_zsh completion)" | ||
|
||
For Fish: | ||
|
||
.. code-block:: fish | ||
|
||
eval (env _COMPLETION_COMPLETE=source_fish completion) | ||
|
||
Now press tab (maybe twice) after typing something to see completions. |
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,53 @@ | ||
import os | ||
|
||
import click | ||
from click.shell_completion import CompletionItem | ||
|
||
|
||
@click.group() | ||
def cli(): | ||
pass | ||
|
||
|
||
@cli.command() | ||
@click.option("--dir", type=click.Path(file_okay=False)) | ||
def ls(dir): | ||
click.echo("\n".join(os.listdir(dir))) | ||
|
||
|
||
def get_env_vars(ctx, param, args, incomplete): | ||
# Returning a list of values is a shortcut to returning a list of | ||
# CompletionItem(value). | ||
return [k for k in os.environ if incomplete in k] | ||
|
||
|
||
@cli.command(help="A command to print environment variables") | ||
@click.argument("envvar", shell_complete=get_env_vars) | ||
def show_env(envvar): | ||
click.echo(f"Environment variable: {envvar}") | ||
click.echo(f"Value: {os.environ[envvar]}") | ||
|
||
|
||
@cli.group(help="A group that holds a subcommand") | ||
def group(): | ||
pass | ||
|
||
|
||
def list_users(ctx, args, incomplete): | ||
# You can generate completions with help strings by returning a list | ||
# of CompletionItem. You can match on whatever you want, including | ||
# the help. | ||
items = [("bob", "butcher"), ("alice", "baker"), ("jerry", "candlestick maker")] | ||
|
||
for value, help in items: | ||
if incomplete in value or incomplete in help: | ||
yield CompletionItem(value, help=help) | ||
|
||
|
||
@group.command(help="Choose a user") | ||
@click.argument("user", type=click.STRING, autocompletion=list_users) | ||
def select_user(user): | ||
click.echo(f"Chosen user is {user}") | ||
|
||
|
||
cli.add_command(group) |
6 changes: 3 additions & 3 deletions
6
examples/bashcompletion/setup.py → examples/completion/setup.py
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,13 +1,13 @@ | ||
from setuptools import setup | ||
|
||
setup( | ||
name="click-example-bashcompletion", | ||
name="click-example-completion", | ||
version="1.0", | ||
py_modules=["bashcompletion"], | ||
py_modules=["completion"], | ||
include_package_data=True, | ||
install_requires=["click"], | ||
entry_points=""" | ||
[console_scripts] | ||
bashcompletion=bashcompletion:cli | ||
completion=completion:cli | ||
""", | ||
) |
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
Oops, something went wrong.