forked from tiangolo/typer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use shell_complete when supplied on Arguments - fixes tiangolo#334
- Loading branch information
Showing
3 changed files
with
58 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,28 @@ | ||
import click | ||
import typer | ||
from typer._compat_utils import _get_click_major | ||
|
||
app = typer.Typer() | ||
|
||
|
||
def shell_complete(ctx: click.Context, param: click.Parameter, incomplete: str): | ||
typer.echo(f"ctx: {ctx.info_name}", err=True) | ||
typer.echo(f"arg is: {param.name}", err=True) | ||
typer.echo(f"incomplete is: {incomplete}", err=True) | ||
if _get_click_major() > 7: | ||
from click.shell_completion import CompletionItem | ||
|
||
return [CompletionItem("Emma", help="Emma is awesome.")] | ||
return ["Emma"] | ||
|
||
|
||
@app.command(context_settings={"auto_envvar_prefix": "TEST"}) | ||
def main(name: str = typer.Argument(shell_complete=shell_complete)): | ||
""" | ||
Say hello. | ||
""" | ||
print(f"Hello {name}") | ||
|
||
|
||
if __name__ == "__main__": | ||
app() |
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,29 @@ | ||
import os | ||
import subprocess | ||
import sys | ||
|
||
from typer._compat_utils import _get_click_major | ||
|
||
from tests.assets import compat_arg_complete_click7_8 as mod | ||
|
||
|
||
def test_arg_completion(): | ||
result = subprocess.run( | ||
[sys.executable, "-m", "coverage", "run", mod.__file__, "E"], | ||
stdout=subprocess.PIPE, | ||
stderr=subprocess.PIPE, | ||
encoding="utf-8", | ||
env={ | ||
**os.environ, | ||
"_COMPAT_ARG_COMPLETE_CLICK7_8.PY_COMPLETE": "complete_zsh", | ||
"_TYPER_COMPLETE_ARGS": "compat_arg_complete_click7_8.py E", | ||
"_TYPER_COMPLETE_TESTING": "True", | ||
}, | ||
) | ||
assert "Emma" in result.stdout | ||
if _get_click_major() > 7: | ||
assert "Emma is awesome." in result.stdout | ||
|
||
assert "ctx: compat_arg_complete_click7_8" in result.stderr | ||
assert "arg is: name" in result.stderr | ||
assert "incomplete is: E" in result.stderr |
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