Skip to content
Myr

completion.py

This file

Path

examples/completion/completion.py

56 lines · 1.4 KiB · Python

Last changed by

Not known.

History of this file

Take it

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

Bump actions/cache from 3.0.9 to 3.0.11 (#2391)dependabot[bot]before adoptionc65c6ad

lines 1–56why, one step back
1import os
23import click4from click.shell_completion import CompletionItem567@click.group()8def cli():9 pass101112@cli.command()13@click.option("--dir", type=click.Path(file_okay=False))14def ls(dir):15 click.echo("\n".join(os.listdir(dir)))161718def get_env_vars(ctx, param, incomplete):19 # Returning a list of values is a shortcut to returning a list of20 # CompletionItem(value).21 return [k for k in os.environ if incomplete in k]222324@cli.command(help="A command to print environment variables")25@click.argument("envvar", shell_complete=get_env_vars)26def show_env(envvar):27 click.echo(f"Environment variable: {envvar}")28 click.echo(f"Value: {os.environ[envvar]}")293031@cli.group(help="A group that holds a subcommand")32def group():33 pass343536def list_users(ctx, param, incomplete):37 # You can generate completions with help strings by returning a list38 # of CompletionItem. You can match on whatever you want, including39 # the help.40 items = [("bob", "butcher"), ("alice", "baker"), ("jerry", "candlestick maker")]41 out = []4243 for value, help in items:44 if incomplete in value or incomplete in help:45 out.append(CompletionItem(value, help=help))4647 return out484950@group.command(help="Choose a user")51@click.argument("user", shell_complete=list_users)52def select_user(user):53 click.echo(f"Chosen user is {user}")545556cli.add_command(group)