bashcompletion.py
This file
Path
examples/bashcompletion/bashcompletion.py
41 lines · 925 bytes · Python
Last changed by
Merge pull request #869 from stopthatcow/feature/866David Lordbefore adoptiond37ff75
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 os3Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849
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 pass8Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849
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):Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849
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 back14Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849
line 15why, one step back15Merge pull request #869 from stopthatcow/feature/866David Lordbefore adoptiond37ff75
line 16why, one step back16@cli.command(help='A command to print environment variables')Merge pull request #755 from stopthatcow/feature/bash_autocompletion_6.6Markus Unterwaditzerbefore adoption5f2ff4f
lines 17–21why, one step back17@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])21Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849
line 22why, one step back22Merge pull request #869 from stopthatcow/feature/866David Lordbefore adoptiond37ff75
line 23why, one step back23@click.group(help='A group that holds a subcommand')Merge pull request #755 from stopthatcow/feature/bash_autocompletion_6.6Markus Unterwaditzerbefore adoption5f2ff4f
lines 24–26why, one step back24def group():25 pass26Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849
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']Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849
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
line 35why, one step back35Merge pull request #869 from stopthatcow/feature/866David Lordbefore adoptiond37ff75
line 36why, one step back36@group.command(help='Choose a user')Merge pull request #755 from stopthatcow/feature/bash_autocompletion_6.6Markus Unterwaditzerbefore adoption5f2ff4f
lines 37–41why, one step back37@click.argument("user", type=click.STRING, autocompletion=list_users)38def subcmd(user):39 click.echo('Chosen user is %s' % user)4041cli.add_command(group)