diff options
author | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2019-02-27 22:41:53 -0500 |
---|---|---|
committer | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2019-02-27 22:41:53 -0500 |
commit | 53c41ea5ff6eda6bba94938c16505e4db10c46b6 (patch) | |
tree | 549bcd9171ae3c2ab91baa3a142e79957e27e576 /examples | |
parent | 60d6e4c7e41ffd21d67c21b4e88efc6775e5a1cd (diff) | |
download | cmd2-git-53c41ea5ff6eda6bba94938c16505e4db10c46b6.tar.gz |
Updated the examples to illustrate sorting CompletionItems
Diffstat (limited to 'examples')
-rwxr-xr-x | examples/tab_autocomp_dynamic.py | 7 | ||||
-rwxr-xr-x | examples/tab_autocompletion.py | 7 |
2 files changed, 10 insertions, 4 deletions
diff --git a/examples/tab_autocomp_dynamic.py b/examples/tab_autocomp_dynamic.py index ebabf1f9..bedc9d4b 100755 --- a/examples/tab_autocomp_dynamic.py +++ b/examples/tab_autocomp_dynamic.py @@ -9,7 +9,7 @@ Released under MIT license, see LICENSE file from typing import List import cmd2 -from cmd2 import argparse_completer +from cmd2 import argparse_completer, utils actors = ['Mark Hamill', 'Harrison Ford', 'Carrie Fisher', 'Alec Guinness', 'Peter Mayhew', 'Anthony Daniels', 'Adam Driver', 'Daisy Ridley', 'John Boyega', 'Oscar Isaac', @@ -175,11 +175,14 @@ class TabCompleteExample(cmd2.Cmd): """Demonstrates showing tabular hinting of tab completion information""" completions_with_desc = [] - for movie_id in self.MOVIE_DATABASE_IDS: + # Sort the movie id strings with a natural sort since they contain numbers + for movie_id in utils.natural_sort(self.MOVIE_DATABASE_IDS): if movie_id in self.MOVIE_DATABASE: movie_entry = self.MOVIE_DATABASE[movie_id] completions_with_desc.append(argparse_completer.CompletionItem(movie_id, movie_entry['title'])) + # Mark that we already sorted the matches + self.matches_sorted = True return completions_with_desc ################################################################################### diff --git a/examples/tab_autocompletion.py b/examples/tab_autocompletion.py index 156c2d45..aa28fc10 100755 --- a/examples/tab_autocompletion.py +++ b/examples/tab_autocompletion.py @@ -11,7 +11,7 @@ import itertools from typing import List import cmd2 -from cmd2 import argparse_completer +from cmd2 import argparse_completer, utils actors = ['Mark Hamill', 'Harrison Ford', 'Carrie Fisher', 'Alec Guinness', 'Peter Mayhew', 'Anthony Daniels', 'Adam Driver', 'Daisy Ridley', 'John Boyega', 'Oscar Isaac', @@ -113,11 +113,14 @@ class TabCompleteExample(cmd2.Cmd): """Demonstrates showing tabular hinting of tab completion information""" completions_with_desc = [] - for movie_id in self.MOVIE_DATABASE_IDS: + # Sort the movie id strings with a natural sort since they contain numbers + for movie_id in utils.natural_sort(self.MOVIE_DATABASE_IDS): if movie_id in self.MOVIE_DATABASE: movie_entry = self.MOVIE_DATABASE[movie_id] completions_with_desc.append(argparse_completer.CompletionItem(movie_id, movie_entry['title'])) + # Mark that we already sorted the matches + self.matches_sorted = True return completions_with_desc # This demonstrates a number of customizations of the AutoCompleter version of ArgumentParser |