Skip to content
Myr

bashcompletion.py

This file

Path

examples/bashcompletion/bashcompletion.py

41 lines · 925 bytes · Python

Last changed by

support descriptions for zsh completionsNicholas Wilesbefore adoption6c7276b

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

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

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

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

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):

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

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

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

line 15why, one step back
15

support descriptions for zsh completionsNicholas Wilesbefore adoption6c7276b

line 16why, one step back
16@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 back
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

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

line 22why, one step back
22

support descriptions for zsh completionsNicholas Wilesbefore adoption6c7276b

line 23why, one step back
23@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 back
24def group():
25 pass26

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

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']

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

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

line 35why, one step back
35

support descriptions for zsh completionsNicholas Wilesbefore adoption6c7276b

line 36why, one step back
36@group.command(help='Choose a user')

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

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