bashcompletion.py
This file
Path
examples/bashcompletion/bashcompletion.py
41 lines · 820 bytes · Python
Last changed by
Allow returning of user completions that don’t always begin with the incomplete term.Nicholas Wilesbefore adoption13eda7a
History of this fileTake it
Merge pull request #755 from stopthatcow/feature/bash_autocompletion_6.6Markus Unterwaditzerbefore adoption5f2ff4f
lines 1–3why, one step back1import click2import os3Allow returning of user completions that don’t always begin with the incomplete term.Nicholas Wilesbefore adoption13eda7a
line 4why, one step back4Merge pull request #755 from stopthatcow/feature/bash_autocompletion_6.6Markus Unterwaditzerbefore adoption5f2ff4f
lines 5–8why, one step back5@click.group()6def cli():7 pass8Allow returning of user completions that don’t always begin with the incomplete term.Nicholas Wilesbefore adoption13eda7a
line 9why, one step back9Merge pull request #755 from stopthatcow/feature/bash_autocompletion_6.6Markus Unterwaditzerbefore adoption5f2ff4f
line 10why, one step back10def get_env_vars(ctx, args, incomplete):Allow returning of user completions that don’t always begin with the incomplete term.Nicholas Wilesbefore adoption13eda7a
lines 11–13why, one step back11 for key in os.environ.keys():12 if incomplete in key:13 yield keyMerge pull request #755 from stopthatcow/feature/bash_autocompletion_6.6Markus Unterwaditzerbefore adoption5f2ff4f
line 14why, one step back14Allow returning of user completions that don’t always begin with the incomplete term.Nicholas Wilesbefore adoption13eda7a
line 15why, one step back15Merge pull request #755 from stopthatcow/feature/bash_autocompletion_6.6Markus Unterwaditzerbefore adoption5f2ff4f
lines 16–21why, one step back16@cli.command()17@click.argument("envvar", type=click.STRING, autocompletion=get_env_vars)18def cmd1(envvar):19 click.echo('Environment variable: %s' % envvar)20 click.echo('Value: %s' % os.environ[envvar])21Allow returning of user completions that don’t always begin with the incomplete term.Nicholas Wilesbefore adoption13eda7a
line 22why, one step back22Merge pull request #755 from stopthatcow/feature/bash_autocompletion_6.6Markus Unterwaditzerbefore adoption5f2ff4f
lines 23–26why, one step back23@click.group()24def group():25 pass26Allow returning of user completions that don’t always begin with the incomplete term.Nicholas Wilesbefore adoption13eda7a
line 27why, one step back27Merge pull request #755 from stopthatcow/feature/bash_autocompletion_6.6Markus Unterwaditzerbefore adoption5f2ff4f
lines 28–30why, one step back28def list_users(ctx, args, incomplete):29 # Here you can generate completions dynamically30 users = ['bob', 'alice']Allow returning of user completions that don’t always begin with the incomplete term.Nicholas Wilesbefore adoption13eda7a
lines 31–34why, one step back31 for user in users:32 if user.startswith(incomplete):33 yield user34Merge pull request #755 from stopthatcow/feature/bash_autocompletion_6.6Markus Unterwaditzerbefore adoption5f2ff4f
lines 35–41why, one step back3536@group.command()37@click.argument("user", type=click.STRING, autocompletion=list_users)38def subcmd(user):39 click.echo('Chosen user is %s' % user)4041cli.add_command(group)