bashcompletion.py
This file
Path
examples/bashcompletion/bashcompletion.py
45 lines · 1.2 KiB · Python
Last changed by
Merge pull request #1059 from stopthatcow/feature/1047David Lordbefore adoptiondf0f81d
History of this fileTake it
The walk stopped after 200 commits: lines older than 92532b5 point at it.
Merge branch '6.x-maintenance'Armin Ronacherbefore adoption92532b5
lines 1–10why, one step back1import click2import os345@click.group()6def cli():7 pass8910def get_env_vars(ctx, args, incomplete):Merge pull request #1059 from stopthatcow/feature/1047David Lordbefore adoptiondf0f81d
line 11why, one step back11 # Completions returned as strings do not have a description displayed.Merge branch '6.x-maintenance'Armin Ronacherbefore adoption92532b5
lines 12–16why, one step back12 for key in os.environ.keys():13 if incomplete in key:14 yield key1516Merge pull request #869 from stopthatcow/feature/866David Lordbefore adoptiond37ff75
line 17why, one step back17@cli.command(help='A command to print environment variables')Merge branch '6.x-maintenance'Armin Ronacherbefore adoption92532b5
lines 18–23why, one step back18@click.argument("envvar", type=click.STRING, autocompletion=get_env_vars)19def cmd1(envvar):20 click.echo('Environment variable: %s' % envvar)21 click.echo('Value: %s' % os.environ[envvar])2223Merge pull request #869 from stopthatcow/feature/866David Lordbefore adoptiond37ff75
line 24why, one step back24@click.group(help='A group that holds a subcommand')Merge branch '6.x-maintenance'Armin Ronacherbefore adoption92532b5
lines 25–29why, one step back25def group():26 pass272829def list_users(ctx, args, incomplete):Merge pull request #1059 from stopthatcow/feature/1047David Lordbefore adoptiondf0f81d
lines 30–36why, one step back30 # You can generate completions with descriptions by returning31 # tuples in the form (completion, description).32 users = [('bob', 'butcher'),33 ('alice', 'baker'),34 ('jerry', 'candlestick maker')]35 # Ths will allow completion matches based on matches within the description string too!36 return [user for user in users if incomplete in user[0] or incomplete in user[1]]Merge branch '6.x-maintenance'Armin Ronacherbefore adoption92532b5
lines 37–38why, one step back3738Merge pull request #869 from stopthatcow/feature/866David Lordbefore adoptiond37ff75
line 39why, one step back39@group.command(help='Choose a user')Merge branch '6.x-maintenance'Armin Ronacherbefore adoption92532b5
lines 40–42why, one step back40@click.argument("user", type=click.STRING, autocompletion=list_users)41def subcmd(user):42 click.echo('Chosen user is %s' % user)Merge pull request #1059 from stopthatcow/feature/1047David Lordbefore adoptiondf0f81d
line 43why, one step back43Merge branch '6.x-maintenance'Armin Ronacherbefore adoption92532b5
lines 44–45why, one step back4445cli.add_command(group)