Skip to content
Myr

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 file

Take it

Merge pull request #755 from stopthatcow/feature/bash_autocompletion_6.6Markus Unterwaditzerbefore adoption5f2ff4f

lines 1–3why, one step back
1import click
2import os3

Allow returning of user completions that don’t always begin with the incomplete term.Nicholas Wilesbefore adoption13eda7a

line 4why, one step back
4

Merge pull request #755 from stopthatcow/feature/bash_autocompletion_6.6Markus Unterwaditzerbefore adoption5f2ff4f

lines 5–8why, one step back
5@click.group()
6def cli():7 pass8

Allow returning of user completions that don’t always begin with the incomplete term.Nicholas Wilesbefore adoption13eda7a

line 9why, one step back
9

Merge pull request #755 from stopthatcow/feature/bash_autocompletion_6.6Markus Unterwaditzerbefore adoption5f2ff4f

line 10why, one step back
10def 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 back
11 for key in os.environ.keys():
12 if incomplete in key:13 yield key

Merge pull request #755 from stopthatcow/feature/bash_autocompletion_6.6Markus Unterwaditzerbefore adoption5f2ff4f

line 14why, one step back
14

Allow returning of user completions that don’t always begin with the incomplete term.Nicholas Wilesbefore adoption13eda7a

line 15why, one step back
15

Merge pull request #755 from stopthatcow/feature/bash_autocompletion_6.6Markus Unterwaditzerbefore adoption5f2ff4f

lines 16–21why, one step back
16@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])21

Allow returning of user completions that don’t always begin with the incomplete term.Nicholas Wilesbefore adoption13eda7a

line 22why, one step back
22

Merge pull request #755 from stopthatcow/feature/bash_autocompletion_6.6Markus Unterwaditzerbefore adoption5f2ff4f

lines 23–26why, one step back
23@click.group()
24def group():25 pass26

Allow returning of user completions that don’t always begin with the incomplete term.Nicholas Wilesbefore adoption13eda7a

line 27why, one step back
27

Merge pull request #755 from stopthatcow/feature/bash_autocompletion_6.6Markus Unterwaditzerbefore adoption5f2ff4f

lines 28–30why, one step back
28def 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 back
31 for user in users:
32 if user.startswith(incomplete):33 yield user34

Merge pull request #755 from stopthatcow/feature/bash_autocompletion_6.6Markus Unterwaditzerbefore adoption5f2ff4f

lines 35–41why, one step back
35
36@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)