summaryrefslogtreecommitdiff
path: root/cmd2/parsing.py
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2019-07-16 10:48:17 -0400
committerKevin Van Brunt <kmvanbrunt@gmail.com>2019-07-16 10:48:17 -0400
commit90cfcdfc795d0c4eb6768eae9623a049e9e79e8d (patch)
tree0112e281f5c6c62afe041b53b13b4dfa0824e8c0 /cmd2/parsing.py
parent1820ebe60a4d059b6f016041fe9b401758ae321e (diff)
downloadcmd2-git-90cfcdfc795d0c4eb6768eae9623a049e9e79e8d.tar.gz
Moved the sorting of shortcuts into StatementParser which allows users to pass in a dictionary instead of a tuple for shortcuts.
Diffstat (limited to 'cmd2/parsing.py')
-rw-r--r--cmd2/parsing.py13
1 files changed, 8 insertions, 5 deletions
diff --git a/cmd2/parsing.py b/cmd2/parsing.py
index 2e94516a..dbfabc80 100644
--- a/cmd2/parsing.py
+++ b/cmd2/parsing.py
@@ -249,7 +249,7 @@ class StatementParser:
terminators: Optional[Iterable[str]] = None,
multiline_commands: Optional[Iterable[str]] = None,
aliases: Optional[Dict[str, str]] = None,
- shortcuts: Optional[Iterable[Tuple[str, str]]] = None) -> None:
+ shortcuts: Optional[Dict[str, str]] = None) -> None:
"""Initialize an instance of StatementParser.
The following will get converted to an immutable tuple before storing internally:
@@ -261,7 +261,7 @@ class StatementParser:
:param terminators: iterable containing strings which should terminate multiline commands
:param multiline_commands: iterable containing the names of commands that accept multiline input
:param aliases: dictionary containing aliases
- :param shortcuts: an iterable of tuples with each tuple containing the shortcut and the expansion
+ :param shortcuts: dictionary containing shortcuts
"""
self.allow_redirection = allow_redirection
if terminators is None:
@@ -276,10 +276,13 @@ class StatementParser:
self.aliases = dict()
else:
self.aliases = aliases
+
if shortcuts is None:
- self.shortcuts = tuple()
- else:
- self.shortcuts = tuple(shortcuts)
+ shortcuts = constants.DEFAULT_SHORTCUTS
+
+ # Sort the shortcuts in descending order by name length because the longest match
+ # should take precedence. (e.g., @@file should match '@@' and not '@'.
+ self.shortcuts = tuple(sorted(shortcuts.items(), key=lambda x: len(x[0]), reverse=True))
# commands have to be a word, so make a regular expression
# that matches the first word in the line. This regex has three