summaryrefslogtreecommitdiff
path: root/cmd2/cmd2.py
diff options
context:
space:
mode:
authorTodd Leonhardt <todd.leonhardt@gmail.com>2018-09-26 21:02:29 -0400
committerTodd Leonhardt <todd.leonhardt@gmail.com>2018-09-26 21:02:29 -0400
commit540a02439523d220f215b0a9f03ff55fb9b260d5 (patch)
treeffb4df8275d78884186ededdeb128768ca3bee97 /cmd2/cmd2.py
parent06808d31371c7b8fbe750ba68c8da49c1ffef2f5 (diff)
parentbb8f6dd930820ef580d3399726256ac0a19a78dc (diff)
downloadcmd2-git-540a02439523d220f215b0a9f03ff55fb9b260d5.tar.gz
Merged master into macro.
This fixes unit test failures due to aliases and macros being class instead of instance attributes.
Diffstat (limited to 'cmd2/cmd2.py')
-rw-r--r--cmd2/cmd2.py26
1 files changed, 19 insertions, 7 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py
index a5610f50..30d3a75c 100644
--- a/cmd2/cmd2.py
+++ b/cmd2/cmd2.py
@@ -312,8 +312,6 @@ class Cmd(cmd.Cmd):
# Attributes used to configure the StatementParser, best not to change these at runtime
multiline_commands = []
shortcuts = {'?': 'help', '!': 'shell', '@': 'load', '@@': '_relative_load'}
- aliases = dict()
- macros = dict()
terminators = [';']
# Attributes which are NOT dynamically settable at runtime
@@ -392,6 +390,10 @@ class Cmd(cmd.Cmd):
# Commands to exclude from the history command
self.exclude_from_history = '''history edit eof eos'''.split()
+ # Command aliases and macros
+ self.aliases = dict()
+ self.macros = dict()
+
self._finalize_app_parameters()
self.initial_stdout = sys.stdout
@@ -1609,6 +1611,16 @@ class Cmd(cmd.Cmd):
return commands
+ def get_alias_names(self) -> List[str]:
+ """Return a list of alias names."""
+ alias_names = set(self.aliases)
+ return list(alias_names)
+
+ def get_macro_names(self) -> List[str]:
+ """Return a list of macro names."""
+ macro_names = set(self.macros)
+ return list(macro_names)
+
def get_commands_aliases_and_macros_for_completion(self) -> List[str]:
"""Return a list of visible commands, aliases, and macros for tab completion"""
visible_commands = set(self.get_visible_commands())
@@ -2341,7 +2353,7 @@ class Cmd(cmd.Cmd):
alias_delete_parser = alias_subparsers.add_parser('delete', help=alias_delete_help,
description=alias_delete_description)
setattr(alias_delete_parser.add_argument('name', type=str, nargs='*', help='alias to delete'),
- ACTION_ARG_CHOICES, aliases)
+ ACTION_ARG_CHOICES, get_alias_names)
alias_delete_parser.add_argument('-a', '--all', action='store_true', help="all aliases will be deleted")
alias_delete_parser.set_defaults(func=alias_delete)
@@ -2355,7 +2367,7 @@ class Cmd(cmd.Cmd):
alias_list_parser = alias_subparsers.add_parser('list', help=alias_list_help,
description=alias_list_description)
setattr(alias_list_parser.add_argument('name', type=str, nargs="*", help='alias to list'),
- ACTION_ARG_CHOICES, aliases)
+ ACTION_ARG_CHOICES, get_alias_names)
alias_list_parser.set_defaults(func=alias_list)
@with_argparser(alias_parser, preserve_quotes=True)
@@ -2540,7 +2552,7 @@ class Cmd(cmd.Cmd):
description=macro_create_description,
epilog=macro_create_epilog)
setattr(macro_create_parser.add_argument('name', type=str, help='name of this macro'),
- ACTION_ARG_CHOICES, macros)
+ ACTION_ARG_CHOICES, get_macro_names)
setattr(macro_create_parser.add_argument('command', type=str, help='what the macro resolves to'),
ACTION_ARG_CHOICES, get_commands_aliases_and_macros_for_completion)
setattr(macro_create_parser.add_argument('command_args', type=str, nargs=argparse.REMAINDER,
@@ -2554,7 +2566,7 @@ class Cmd(cmd.Cmd):
macro_delete_parser = macro_subparsers.add_parser('delete', help=macro_delete_help,
description=macro_delete_description)
setattr(macro_delete_parser.add_argument('name', type=str, nargs='*', help='macro to delete'),
- ACTION_ARG_CHOICES, macros)
+ ACTION_ARG_CHOICES, get_macro_names)
macro_delete_parser.add_argument('-a', '--all', action='store_true', help="all macros will be deleted")
macro_delete_parser.set_defaults(func=macro_delete)
@@ -2568,7 +2580,7 @@ class Cmd(cmd.Cmd):
macro_list_parser = macro_subparsers.add_parser('list', help=macro_list_help,
description=macro_list_description)
setattr(macro_list_parser.add_argument('name', type=str, nargs="*", help='macro to list'),
- ACTION_ARG_CHOICES, macros)
+ ACTION_ARG_CHOICES, get_macro_names)
macro_list_parser.set_defaults(func=macro_list)
@with_argparser(macro_parser, preserve_quotes=True)