Skip to content

Commit 6b388ed

Browse files
committed
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
1 parent 8cf0b30 commit 6b388ed

File tree

3 files changed

+17
-9
lines changed

3 files changed

+17
-9
lines changed

cmd2/cmd2.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,6 @@ def __init__(self, completekey: str = 'tab', stdin=None, stdout=None, persistent
370370
self.exclude_from_history = '''history edit eof eos'''.split()
371371

372372
# Command aliases and macros
373-
self.aliases = dict()
374373
self.macros = dict()
375374

376375
self.initial_stdout = sys.stdout
@@ -385,7 +384,6 @@ def __init__(self, completekey: str = 'tab', stdin=None, stdout=None, persistent
385384
self.statement_parser = StatementParser(allow_redirection=allow_redirection,
386385
terminators=terminators,
387386
multiline_commands=multiline_commands,
388-
aliases=self.aliases,
389387
shortcuts=shortcuts)
390388
self._transcript_files = transcript_files
391389

@@ -541,15 +539,25 @@ def visible_prompt(self) -> str:
541539
return utils.strip_ansi(self.prompt)
542540

543541
@property
544-
def allow_redirection(self) -> bool:
545-
"""Read-only property to get whether or not redirection of stdout is allowed."""
546-
return self.statement_parser.allow_redirection
542+
def aliases(self) -> bool:
543+
"""Read-only property to access the aliases stored in the StatementParser."""
544+
return self.statement_parser.aliases
547545

548546
@property
549547
def shortcuts(self) -> Tuple[Tuple[str, str]]:
550548
"""Read-only property to access the shortcuts stored in the StatementParser."""
551549
return self.statement_parser.shortcuts
552550

551+
@property
552+
def allow_redirection(self) -> bool:
553+
"""Getter for the allow_redirection property that determines whether or not redirection of stdout is allowed."""
554+
return self.statement_parser.allow_redirection
555+
556+
@allow_redirection.setter
557+
def allow_redirection(self, value: bool) -> None:
558+
"""Setter for the allow_redirection property that determines whether or not redirection of stdout is allowed."""
559+
self.statement_parser.allow_redirection = value
560+
553561
def decolorized_write(self, fileobj: IO, msg: str) -> None:
554562
"""Write a string to a fileobject, stripping ANSI escape sequences if necessary
555563

cmd2/parsing.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -295,15 +295,15 @@ def __init__(self,
295295
else:
296296
self.terminators = tuple(terminators)
297297
if multiline_commands is None:
298-
self.multiline_commands = ()
298+
self.multiline_commands = tuple()
299299
else:
300300
self.multiline_commands = tuple(multiline_commands)
301301
if aliases is None:
302-
self.aliases = {}
302+
self.aliases = dict()
303303
else:
304304
self.aliases = aliases
305305
if shortcuts is None:
306-
self.shortcuts = ()
306+
self.shortcuts = tuple()
307307
else:
308308
self.shortcuts = tuple(shortcuts)
309309

tests/test_completion.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ def test_tokens_for_completion_redirect(cmd2_app):
635635
endidx = len(line)
636636
begidx = endidx - len(text)
637637

638-
cmd2_app.statement_parser.allow_redirection = True
638+
cmd2_app.allow_redirection = True
639639
expected_tokens = ['command', '|', '<', '>>', 'file']
640640
expected_raw_tokens = ['command', '|', '<', '>>', 'file']
641641

0 commit comments

Comments
 (0)