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 #1490 from pallets/styleDavid Lordbefore adoption38ba2e6

History of this file

Take it

The walk stopped after 200 commits: lines older than 92532b5 point at it.

Merge branch '6.x-maintenance'Armin Ronacherbefore adoption92532b5

lines 1–2why, one step back
1import os
2

Merge pull request #1490 from pallets/styleDavid Lordbefore adoption38ba2e6

line 3why, one step back
3import click

Merge branch '6.x-maintenance'Armin Ronacherbefore adoption92532b5

line 4why, one step back
4

Merge pull request #1490 from pallets/styleDavid Lordbefore adoption38ba2e6

line 5why, one step back
5

Merge branch '6.x-maintenance'Armin Ronacherbefore adoption92532b5

lines 6–11why, one step back
6@click.group()
7def cli():8 pass91011def get_env_vars(ctx, args, incomplete):

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

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

Merge branch '6.x-maintenance'Armin Ronacherbefore adoption92532b5

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

Merge pull request #1490 from pallets/styleDavid Lordbefore adoption38ba2e6

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

Merge branch '6.x-maintenance'Armin Ronacherbefore adoption92532b5

lines 19–20why, one step back
19@click.argument("envvar", type=click.STRING, autocompletion=get_env_vars)
20def cmd1(envvar):

Merge pull request #1490 from pallets/styleDavid Lordbefore adoption38ba2e6

lines 21–22why, one step back
21 click.echo("Environment variable: {}".format(envvar))
22 click.echo("Value: {}".format(os.environ[envvar]))

Merge branch '6.x-maintenance'Armin Ronacherbefore adoption92532b5

lines 23–24why, one step back
23
24

Merge pull request #1490 from pallets/styleDavid Lordbefore adoption38ba2e6

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

Merge branch '6.x-maintenance'Armin Ronacherbefore adoption92532b5

lines 26–30why, one step back
26def group():
27 pass282930def list_users(ctx, args, incomplete):

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

lines 31–32why, one step back
31 # You can generate completions with descriptions by returning
32 # tuples in the form (completion, description).

Merge pull request #1490 from pallets/styleDavid Lordbefore adoption38ba2e6

lines 33–35why, one step back
33 users = [("bob", "butcher"), ("alice", "baker"), ("jerry", "candlestick maker")]
34 # Ths will allow completion matches based on matches within the35 # description string too!

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

line 36why, one step back
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 back
37
38

Merge pull request #1490 from pallets/styleDavid Lordbefore adoption38ba2e6

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

Merge branch '6.x-maintenance'Armin Ronacherbefore adoption92532b5

lines 40–41why, one step back
40@click.argument("user", type=click.STRING, autocompletion=list_users)
41def subcmd(user):

Merge pull request #1490 from pallets/styleDavid Lordbefore adoption38ba2e6

line 42why, one step back
42 click.echo("Chosen user is {}".format(user))

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

line 43why, one step back
43

Merge branch '6.x-maintenance'Armin Ronacherbefore adoption92532b5

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