From b073abe1cbc67d2a29f13ac757774b49257c494b Mon Sep 17 00:00:00 2001 From: Nicholas Wiles Date: Mon, 9 Oct 2017 18:14:00 -0700 Subject: Use Python sorting order for ZSH completions. Fixes #1047. --- examples/bashcompletion/bashcompletion.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'examples') diff --git a/examples/bashcompletion/bashcompletion.py b/examples/bashcompletion/bashcompletion.py index 4d9fdde..1072840 100644 --- a/examples/bashcompletion/bashcompletion.py +++ b/examples/bashcompletion/bashcompletion.py @@ -8,6 +8,7 @@ def cli(): def get_env_vars(ctx, args, incomplete): + # Completions returned as strings do not have a description displayed. for key in os.environ.keys(): if incomplete in key: yield key @@ -26,11 +27,13 @@ def group(): def list_users(ctx, args, incomplete): - # Here you can generate completions dynamically - users = ['bob', 'alice'] - for user in users: - if user.startswith(incomplete): - yield user + # You can generate completions with descriptions by returning + # tuples in the form (completion, description). + users = [('bob', 'butcher'), + ('alice', 'baker'), + ('jerry', 'candlestick maker')] + # Ths will allow completion matches based on matches within the description string too! + return [user for user in users if incomplete in user[0] or incomplete in user[1]] @group.command(help='Choose a user') @@ -38,4 +41,5 @@ def list_users(ctx, args, incomplete): def subcmd(user): click.echo('Chosen user is %s' % user) + cli.add_command(group) -- cgit v1.2.1