summaryrefslogtreecommitdiff
path: root/cmd2/cmd2.py
diff options
context:
space:
mode:
Diffstat (limited to 'cmd2/cmd2.py')
-rw-r--r--cmd2/cmd2.py22
1 files changed, 16 insertions, 6 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py
index 1f981417..78da5d44 100644
--- a/cmd2/cmd2.py
+++ b/cmd2/cmd2.py
@@ -129,6 +129,10 @@ COMMAND_FUNC_PREFIX = 'do_'
# All help functions start with this
HELP_FUNC_PREFIX = 'help_'
+# Sorting keys for strings
+ALPHABETICAL_SORT_KEY = utils.norm_fold
+NATURAL_SORT_KEY = utils.natural_keys
+
def categorize(func: Union[Callable, Iterable], category: str) -> None:
"""Categorize a function.
@@ -492,6 +496,12 @@ class Cmd(cmd.Cmd):
if os.path.exists(startup_script) and os.path.getsize(startup_script) > 0:
self.cmdqueue.append("load '{}'".format(startup_script))
+ # The default key for sorting tab completion matches. This only applies when the matches are not
+ # already marked as sorted by setting self.matches_sorted to True. Its default value performs a
+ # case-insensitive alphabetical sort. If natural sorting preferred, then set this to NATURAL_SORT_KEY.
+ # Otherwise it can be set to any custom key to meet your needs.
+ self.matches_sort_key = ALPHABETICAL_SORT_KEY
+
############################################################################################################
# The following variables are used by tab-completion functions. They are reset each time complete() is run
# in reset_completion_defaults() and it is up to completer functions to set them before returning results.
@@ -520,7 +530,7 @@ class Cmd(cmd.Cmd):
self.matches_delimited = False
# Set to True before returning matches to complete() in cases where matches are sorted with custom ordering.
- # If False, then complete() will sort the matches alphabetically before they are displayed.
+ # If False, then complete() will sort the matches using self.matches_sort_key before they are displayed.
self.matches_sorted = False
# Set the pager(s) for use with the ppaged() method for displaying output using a pager
@@ -1119,8 +1129,8 @@ class Cmd(cmd.Cmd):
self.allow_appended_space = False
self.allow_closing_quote = False
- # Sort the matches alphabetically before any trailing slashes are added
- matches.sort(key=utils.norm_fold)
+ # Sort the matches before any trailing slashes are added
+ matches.sort(key=self.matches_sort_key)
self.matches_sorted = True
# Build display_matches and add a slash to directories
@@ -1560,10 +1570,10 @@ class Cmd(cmd.Cmd):
self.completion_matches[0] += str_to_append
- # Sort matches alphabetically if they haven't already been sorted
+ # Sort matches if they haven't already been sorted
if not self.matches_sorted:
- self.completion_matches.sort(key=utils.norm_fold)
- self.display_matches.sort(key=utils.norm_fold)
+ self.completion_matches.sort(key=self.matches_sort_key)
+ self.display_matches.sort(key=self.matches_sort_key)
self.matches_sorted = True
try: