summaryrefslogtreecommitdiff
path: root/cmd2/cmd2.py
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2019-07-15 23:37:10 -0400
committerKevin Van Brunt <kmvanbrunt@gmail.com>2019-07-15 23:37:10 -0400
commit1820ebe60a4d059b6f016041fe9b401758ae321e (patch)
tree51e7933c21af8fa5393024f99cdec702feb705a7 /cmd2/cmd2.py
parent94b424e9c41f99c6eb268c6c97f09e99a8342de8 (diff)
downloadcmd2-git-1820ebe60a4d059b6f016041fe9b401758ae321e.tar.gz
Renamed matches_sort_key to default_sort_key and using it to sort additional d
Diffstat (limited to 'cmd2/cmd2.py')
-rw-r--r--cmd2/cmd2.py36
1 files changed, 18 insertions, 18 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py
index 4ba2e83e..b8e5d9d2 100644
--- a/cmd2/cmd2.py
+++ b/cmd2/cmd2.py
@@ -468,11 +468,13 @@ class Cmd(cmd.Cmd):
elif transcript_files:
self._transcript_files = transcript_files
- # 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 default key for sorting string results. Its default value performs a case-insensitive alphabetical sort.
+ # If natural sorting is preferred, then set this to NATURAL_SORT_KEY.
+ # cmd2 uses this key for sorting:
+ # command and category names
+ # alias, macro, and settable names
+ # tab completion results when self.matches_sorted is False
+ self.default_sort_key = ALPHABETICAL_SORT_KEY
############################################################################################################
# The following variables are used by tab-completion functions. They are reset each time complete() is run
@@ -501,8 +503,8 @@ class Cmd(cmd.Cmd):
# quote matches that are completed in a delimited fashion
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 using self.matches_sort_key before they are displayed.
+ # Set to True before returning matches to complete() in cases where matches have already been sorted.
+ # If False, then complete() will sort the matches using self.default_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
@@ -1107,7 +1109,7 @@ class Cmd(cmd.Cmd):
self.allow_closing_quote = False
# Sort the matches before any trailing slashes are added
- matches.sort(key=self.matches_sort_key)
+ matches.sort(key=self.default_sort_key)
self.matches_sorted = True
# Build display_matches and add a slash to directories
@@ -1553,8 +1555,8 @@ class Cmd(cmd.Cmd):
# Sort matches if they haven't already been sorted
if not self.matches_sorted:
- self.completion_matches.sort(key=self.matches_sort_key)
- self.display_matches.sort(key=self.matches_sort_key)
+ self.completion_matches.sort(key=self.default_sort_key)
+ self.display_matches.sort(key=self.default_sort_key)
self.matches_sorted = True
try:
@@ -2326,8 +2328,7 @@ class Cmd(cmd.Cmd):
else:
self.perror("Alias '{}' not found".format(cur_name))
else:
- sorted_aliases = utils.alphabetical_sort(self.aliases)
- for cur_alias in sorted_aliases:
+ for cur_alias in sorted(self.aliases, key=self.default_sort_key):
self.poutput("alias create {} {}".format(cur_alias, self.aliases[cur_alias]))
# Top-level parser for alias
@@ -2507,8 +2508,7 @@ class Cmd(cmd.Cmd):
else:
self.perror("Macro '{}' not found".format(cur_name))
else:
- sorted_macros = utils.alphabetical_sort(self.macros)
- for cur_macro in sorted_macros:
+ for cur_macro in sorted(self.macros, key=self.default_sort_key):
self.poutput("macro create {} {}".format(cur_macro, self.macros[cur_macro].value))
# Top-level parser for macro
@@ -2692,10 +2692,10 @@ class Cmd(cmd.Cmd):
"""Show a list of commands which help can be displayed for.
"""
# Get a sorted list of help topics
- help_topics = utils.alphabetical_sort(self.get_help_topics())
+ help_topics = sorted(self.get_help_topics(), key=self.default_sort_key)
# Get a sorted list of visible command names
- visible_commands = utils.alphabetical_sort(self.get_visible_commands())
+ visible_commands = sorted(self.get_visible_commands(), key=self.default_sort_key)
cmds_doc = []
cmds_undoc = []
@@ -2730,7 +2730,7 @@ class Cmd(cmd.Cmd):
# Categories found, Organize all commands by category
self.poutput('{}'.format(str(self.doc_leader)))
self.poutput('{}'.format(str(self.doc_header)), end="\n\n")
- for category in sorted(cmds_cats.keys()):
+ for category in sorted(cmds_cats.keys(), key=self.default_sort_key):
self._print_topics(category, cmds_cats[category], verbose)
self._print_topics('Other', cmds_doc, verbose)
@@ -2903,7 +2903,7 @@ class Cmd(cmd.Cmd):
maxlen = max(maxlen, len(result[p]))
if result:
- for p in sorted(result):
+ for p in sorted(result, key=self.default_sort_key):
if args.long:
self.poutput('{} # {}'.format(result[p].ljust(maxlen), self.settable[p]))
else: