summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTodd Leonhardt <todd.leonhardt@gmail.com>2019-03-16 17:38:05 -0400
committerTodd Leonhardt <todd.leonhardt@gmail.com>2019-03-16 17:38:05 -0400
commit6b388ede0d1cc77a587164010cd0c47aa6ec7052 (patch)
treec0ca71c520f6451d1f12666be8790ba3387a6ed9
parent8cf0b30af678717160a6c53cc7c0c84aa007c42f (diff)
downloadcmd2-git-6b388ede0d1cc77a587164010cd0c47aa6ec7052.tar.gz
Aliases are now stored within the StatementParser instance
Also: - Added read-only aliases property to cmd2.Cmd to get aliases from the StatementParser - Added a setter for the allow_redirection property in cmd2.Cmd - Made some initialization code more self-documenting
-rw-r--r--cmd2/cmd2.py18
-rw-r--r--cmd2/parsing.py6
-rw-r--r--tests/test_completion.py2
3 files changed, 17 insertions, 9 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py
index 0c789e5f..e7ada8cd 100644
--- a/cmd2/cmd2.py
+++ b/cmd2/cmd2.py
@@ -370,7 +370,6 @@ class Cmd(cmd.Cmd):
self.exclude_from_history = '''history edit eof eos'''.split()
# Command aliases and macros
- self.aliases = dict()
self.macros = dict()
self.initial_stdout = sys.stdout
@@ -385,7 +384,6 @@ class Cmd(cmd.Cmd):
self.statement_parser = StatementParser(allow_redirection=allow_redirection,
terminators=terminators,
multiline_commands=multiline_commands,
- aliases=self.aliases,
shortcuts=shortcuts)
self._transcript_files = transcript_files
@@ -541,15 +539,25 @@ class Cmd(cmd.Cmd):
return utils.strip_ansi(self.prompt)
@property
- def allow_redirection(self) -> bool:
- """Read-only property to get whether or not redirection of stdout is allowed."""
- return self.statement_parser.allow_redirection
+ def aliases(self) -> bool:
+ """Read-only property to access the aliases stored in the StatementParser."""
+ return self.statement_parser.aliases
@property
def shortcuts(self) -> Tuple[Tuple[str, str]]:
"""Read-only property to access the shortcuts stored in the StatementParser."""
return self.statement_parser.shortcuts
+ @property
+ def allow_redirection(self) -> bool:
+ """Getter for the allow_redirection property that determines whether or not redirection of stdout is allowed."""
+ return self.statement_parser.allow_redirection
+
+ @allow_redirection.setter
+ def allow_redirection(self, value: bool) -> None:
+ """Setter for the allow_redirection property that determines whether or not redirection of stdout is allowed."""
+ self.statement_parser.allow_redirection = value
+
def decolorized_write(self, fileobj: IO, msg: str) -> None:
"""Write a string to a fileobject, stripping ANSI escape sequences if necessary
diff --git a/cmd2/parsing.py b/cmd2/parsing.py
index 380c9261..cd81f250 100644
--- a/cmd2/parsing.py
+++ b/cmd2/parsing.py
@@ -295,15 +295,15 @@ class StatementParser:
else:
self.terminators = tuple(terminators)
if multiline_commands is None:
- self.multiline_commands = ()
+ self.multiline_commands = tuple()
else:
self.multiline_commands = tuple(multiline_commands)
if aliases is None:
- self.aliases = {}
+ self.aliases = dict()
else:
self.aliases = aliases
if shortcuts is None:
- self.shortcuts = ()
+ self.shortcuts = tuple()
else:
self.shortcuts = tuple(shortcuts)
diff --git a/tests/test_completion.py b/tests/test_completion.py
index a7c26928..47a7a9d6 100644
--- a/tests/test_completion.py
+++ b/tests/test_completion.py
@@ -635,7 +635,7 @@ def test_tokens_for_completion_redirect(cmd2_app):
endidx = len(line)
begidx = endidx - len(text)
- cmd2_app.statement_parser.allow_redirection = True
+ cmd2_app.allow_redirection = True
expected_tokens = ['command', '|', '<', '>>', 'file']
expected_raw_tokens = ['command', '|', '<', '>>', 'file']