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
1import click2import os345@click.group()6def cli():7 pass8910def get_env_vars(ctx, args, incomplete):11 # Completions returned as strings do not have a description displayed.12 for key in os.environ.keys():13 if incomplete in key:14 yield key151617@cli.command(help='A command to print environment variables')18@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])222324@click.group(help='A group that holds a subcommand')25def group():26 pass272829def list_users(ctx, args, incomplete):30 # 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]]373839@group.command(help='Choose a user')40@click.argument("user", type=click.STRING, autocompletion=list_users)41def subcmd(user):42 click.echo('Chosen user is %s' % user)434445cli.add_command(group)