summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cmd2/cmd2.py12
-rw-r--r--cmd2/utils.py9
-rw-r--r--tests/test_completion.py2
3 files changed, 15 insertions, 8 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py
index e485973b..7273286b 100644
--- a/cmd2/cmd2.py
+++ b/cmd2/cmd2.py
@@ -1092,7 +1092,7 @@ class Cmd(cmd.Cmd):
self.allow_closing_quote = False
# Sort the matches before any trailing slashes are added
- matches.sort(key=str.lower)
+ matches = utils.alphabetical_sort(matches)
self.matches_sorted = True
# Build display_matches and add a slash to directories
@@ -1532,8 +1532,8 @@ class Cmd(cmd.Cmd):
# Sort matches alphabetically if they haven't already been sorted
if not self.matches_sorted:
- self.completion_matches.sort(key=str.lower)
- self.display_matches.sort(key=str.lower)
+ self.completion_matches = utils.alphabetical_sort(self.completion_matches)
+ self.display_matches = utils.alphabetical_sort(self.display_matches)
self.matches_sorted = True
try:
@@ -2325,12 +2325,10 @@ Usage: Usage: unalias [-a] name [name ...]
"""Show a list of commands which help can be displayed for.
"""
# Get a sorted list of help topics
- help_topics = self.get_help_topics()
- help_topics.sort(key=str.lower)
+ help_topics = utils.alphabetical_sort(self.get_help_topics())
# Get a sorted list of visible command names
- visible_commands = self.get_visible_commands()
- visible_commands.sort(key=str.lower)
+ visible_commands = utils.alphabetical_sort(self.get_visible_commands())
cmds_doc = []
cmds_undoc = []
diff --git a/cmd2/utils.py b/cmd2/utils.py
index d8afd922..d8bdf08d 100644
--- a/cmd2/utils.py
+++ b/cmd2/utils.py
@@ -157,3 +157,12 @@ def remove_duplicates(list_to_prune: List) -> List:
temp_dict[item] = None
return list(temp_dict.keys())
+
+
+def alphabetical_sort(list_to_sort: List[str]) -> List[str]:
+ """
+ Sorts a list of strings alphabetically
+ :param list_to_sort: the list being sorted
+ :return: the sorted list
+ """
+ return sorted(list_to_sort, key=str.casefold)
diff --git a/tests/test_completion.py b/tests/test_completion.py
index 138ab413..d50ad039 100644
--- a/tests/test_completion.py
+++ b/tests/test_completion.py
@@ -410,7 +410,7 @@ def test_delimiter_completion(cmd2_app):
# Remove duplicates from display_matches and sort it. This is typically done in complete().
display_list = utils.remove_duplicates(cmd2_app.display_matches)
- display_list.sort(key=str.lower)
+ display_list = utils.alphabetical_sort(display_list)
assert display_list == ['other user', 'user']