Skip to content

Commit df0f81d

Browse files
authored
Merge pull request #1059 from stopthatcow/feature/1047
Allow sorting of completions
2 parents be28b6c + b073abe commit df0f81d

File tree

3 files changed

+30
-9
lines changed

3 files changed

+30
-9
lines changed

CHANGES.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ Unreleased
8181
- Fix formatting for short help. (`#1008`_)
8282
- Document how ``auto_envar_prefix`` works with command groups. (`#1011`_)
8383
- Don't add newlines by default for progress bars. (`#1013`_)
84+
- Use Python sorting order for ZSH completions. (`#1047`_, `#1059`_)
8485
- Document that parameter names are lowercased by default. (`#1055`_)
8586
- Subcommands that are named by the function now automatically have the underscore replaced with a dash. If you register a function named ``my_command`` it becomes ``my-command`` in the command line interface.
8687

@@ -190,7 +191,9 @@ Unreleased
190191
.. _#1020: https://github.com/pallets/click/pull/1020
191192
.. _#1022: https://github.com/pallets/click/pull/1022
192193
.. _#1027: https://github.com/pallets/click/pull/1027
194+
.. _#1047: https://github.com/pallets/click/pull/1047
193195
.. _#1055: https://github.com/pallets/click/pull/1055
196+
.. _#1059: https://github.com/pallets/click/pull/1059
194197

195198

196199
Version 6.7

click/_bashcomplete.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
WORDBREAK = '='
1212

13+
# Note, only BASH version 4.4 and later have the nosort option.
1314
COMPLETION_SCRIPT_BASH = '''
1415
%(complete_func)s() {
1516
local IFS=$'\n'
@@ -19,7 +20,17 @@
1920
return 0
2021
}
2122
22-
complete -F %(complete_func)s %(script_names)s
23+
%(complete_func)setup() {
24+
local COMPLETION_OPTIONS=""
25+
local BASH_VERSION_ARR=(${BASH_VERSION//./ })
26+
if [ ${BASH_VERSION_ARR[0]} -ge 4 ] && [ ${BASH_VERSION_ARR[1]} -ge 4 ];then
27+
COMPLETION_OPTIONS="-o nosort"
28+
fi
29+
30+
complete $COMPLETION_OPTIONS -F %(complete_func)s %(script_names)s
31+
}
32+
33+
%(complete_func)setup
2334
'''
2435

2536
COMPLETION_SCRIPT_ZSH = '''
@@ -41,11 +52,13 @@
4152
done
4253
4354
if [ -n "$completions_with_descriptions" ]; then
44-
_describe '' completions_with_descriptions
55+
_describe -V unsorted completions_with_descriptions -U -Q
4556
fi
57+
4658
if [ -n "$completions" ]; then
47-
compadd -M 'r:|=* l:|=* r:|=*' -a completions
59+
compadd -U -V unsorted -Q -a completions
4860
fi
61+
compstate[insert]="automenu"
4962
}
5063
5164
compdef %(complete_func)s %(script_names)s
@@ -232,7 +245,8 @@ def get_choices(cli, prog_name, args, incomplete):
232245
return get_user_autocompletions(ctx, all_args, incomplete, param)
233246

234247
add_subcommand_completions(ctx, incomplete, completions)
235-
return completions
248+
# Sort before returning so that proper ordering can be enforced in custom types.
249+
return sorted(completions)
236250

237251

238252
def do_complete(cli, prog_name, include_descriptions):

examples/bashcompletion/bashcompletion.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ def cli():
88

99

1010
def get_env_vars(ctx, args, incomplete):
11+
# Completions returned as strings do not have a description displayed.
1112
for key in os.environ.keys():
1213
if incomplete in key:
1314
yield key
@@ -26,16 +27,19 @@ def group():
2627

2728

2829
def list_users(ctx, args, incomplete):
29-
# Here you can generate completions dynamically
30-
users = ['bob', 'alice']
31-
for user in users:
32-
if user.startswith(incomplete):
33-
yield user
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]]
3437

3538

3639
@group.command(help='Choose a user')
3740
@click.argument("user", type=click.STRING, autocompletion=list_users)
3841
def subcmd(user):
3942
click.echo('Chosen user is %s' % user)
4043

44+
4145
cli.add_command(group)

0 commit comments

Comments
 (0)