Skip to content

Commit c79e56d

Browse files
committed
Updated argparse_completer to automatically set the matches_sorted flag for tabular results. Updated the example to demonstrate pre-sorted tabular results.
1 parent 47f963e commit c79e56d

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

cmd2/argparse_completer.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,7 @@ def _format_completions(self, action, completions: List[Union[str, CompletionIte
492492

493493
self._cmd2_app.completion_header = header
494494
self._cmd2_app.display_matches = completions_with_desc
495+
self._cmd2_app.matches_sorted = True
495496

496497
return completions
497498

examples/tab_autocompletion.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def __init__(self):
3838
static_list_directors = ['J. J. Abrams', 'Irvin Kershner', 'George Lucas', 'Richard Marquand',
3939
'Rian Johnson', 'Gareth Edwards']
4040
USER_MOVIE_LIBRARY = ['ROGUE1', 'SW_EP04', 'SW_EP05']
41-
MOVIE_DATABASE_IDS = ['SW_EP01', 'SW_EP02', 'SW_EP03', 'ROGUE1', 'SW_EP04',
41+
MOVIE_DATABASE_IDS = ['SW_EP1', 'SW_EP02', 'SW_EP03', 'ROGUE1', 'SW_EP04',
4242
'SW_EP05', 'SW_EP06', 'SW_EP07', 'SW_EP08', 'SW_EP09']
4343
MOVIE_DATABASE = {'SW_EP04': {'title': 'Star Wars: Episode IV - A New Hope',
4444
'rating': 'PG',
@@ -52,13 +52,13 @@ def __init__(self):
5252
'actor': ['Mark Hamill', 'Harrison Ford', 'Carrie Fisher',
5353
'Alec Guinness', 'Peter Mayhew', 'Anthony Daniels']
5454
},
55-
'SW_EP06': {'title': 'Star Wars: Episode IV - A New Hope',
55+
'SW_EP06': {'title': 'Star Wars: Episode VI - Return of the Jedi',
5656
'rating': 'PG',
5757
'director': ['Richard Marquand'],
5858
'actor': ['Mark Hamill', 'Harrison Ford', 'Carrie Fisher',
5959
'Alec Guinness', 'Peter Mayhew', 'Anthony Daniels']
6060
},
61-
'SW_EP01': {'title': 'Star Wars: Episode I - The Phantom Menace',
61+
'SW_EP1': {'title': 'Star Wars: Episode I - The Phantom Menace',
6262
'rating': 'PG',
6363
'director': ['George Lucas'],
6464
'actor': ['Liam Neeson', 'Ewan McGregor', 'Natalie Portman', 'Jake Lloyd']
@@ -113,8 +113,10 @@ def instance_query_movie_ids(self) -> List[str]:
113113
"""Demonstrates showing tabular hinting of tab completion information"""
114114
completions_with_desc = []
115115

116-
for movie_id, movie_entry in self.MOVIE_DATABASE.items():
117-
completions_with_desc.append(argparse_completer.CompletionItem(movie_id, movie_entry['title']))
116+
for movie_id in self.MOVIE_DATABASE_IDS:
117+
if movie_id in self.MOVIE_DATABASE:
118+
movie_entry = self.MOVIE_DATABASE[movie_id]
119+
completions_with_desc.append(argparse_completer.CompletionItem(movie_id, movie_entry['title']))
118120

119121
return completions_with_desc
120122

0 commit comments

Comments
 (0)