Skip to content
Myr

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 file

Take it

The walk stopped after 200 commits: lines older than 950463b point at it.

Fix typo (#817)scottbeldenbefore adoption950463b

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

Fix typo (#817)scottbeldenbefore adoption950463b

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

Fix typo (#817)scottbeldenbefore adoption950463b

line 10why, one step back
10def get_env_vars(ctx, args, incomplete):

Merge pull request #1059 from stopthatcow/feature/1047David Lordbefore adoptiondf0f81d

line 11why, one step back
11 # Completions returned as strings do not have a description displayed.

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

lines 12–14why, one step back
12 for key in os.environ.keys():
13 if incomplete in key:14 yield key

Fix typo (#817)scottbeldenbefore adoption950463b

line 15why, one step back
15

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

line 16why, one step back
16

Merge pull request #869 from stopthatcow/feature/866David Lordbefore adoptiond37ff75

line 17why, one step back
17@cli.command(help='A command to print environment variables')

Fix typo (#817)scottbeldenbefore adoption950463b

lines 18–22why, one step back
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])22

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

line 23why, one step back
23

Merge pull request #869 from stopthatcow/feature/866David Lordbefore adoptiond37ff75

line 24why, one step back
24@click.group(help='A group that holds a subcommand')

Fix typo (#817)scottbeldenbefore adoption950463b

lines 25–27why, one step back
25def group():
26 pass27

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

line 28why, one step back
28

Fix typo (#817)scottbeldenbefore adoption950463b

line 29why, one step back
29def list_users(ctx, args, incomplete):

Merge pull request #1059 from stopthatcow/feature/1047David Lordbefore adoptiondf0f81d

lines 30–36why, one step back
30 # You can generate completions with descriptions by returning
31 # 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 pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

line 37why, one step back
37

Fix typo (#817)scottbeldenbefore adoption950463b

line 38why, one step back
38

Merge pull request #869 from stopthatcow/feature/866David Lordbefore adoptiond37ff75

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

Fix typo (#817)scottbeldenbefore adoption950463b

lines 40–42why, one step back
40@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 back
43

Fix typo (#817)scottbeldenbefore adoption950463b

lines 44–45why, one step back
44
45cli.add_command(group)