summaryrefslogtreecommitdiff
path: root/cmd2
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2020-08-19 21:07:44 -0400
committerKevin Van Brunt <kmvanbrunt@gmail.com>2020-08-19 21:38:22 -0400
commit30d010f62196ff082bf243a4c460517cb70c70f2 (patch)
treef51eee6bde1a2f1a7f3a264a6e67a3bd1743d469 /cmd2
parent5dd2d03ef35a3d33ff53d82c8039d68e263246ee (diff)
downloadcmd2-git-30d010f62196ff082bf243a4c460517cb70c70f2.tar.gz
Fixed AttributeError when CommandSet that uses as_subcommand_to decorator is loaded during cmd2.Cmd.__init__().
Diffstat (limited to 'cmd2')
-rw-r--r--cmd2/cmd2.py38
1 files changed, 22 insertions, 16 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py
index 610ce4a3..adf797bf 100644
--- a/cmd2/cmd2.py
+++ b/cmd2/cmd2.py
@@ -259,22 +259,6 @@ class Cmd(cmd.Cmd):
multiline_commands=multiline_commands,
shortcuts=shortcuts)
- # Load modular commands
- self._installed_command_sets = [] # type: List[CommandSet]
- self._cmd_to_command_sets = {} # type: Dict[str, CommandSet]
- if command_sets:
- for command_set in command_sets:
- self.register_command_set(command_set)
-
- if auto_load_commands:
- self._autoload_commands()
-
- # Verify commands don't have invalid names (like starting with a shortcut)
- for cur_cmd in self.get_all_commands():
- valid, errmsg = self.statement_parser.is_valid_command(cur_cmd)
- if not valid:
- raise ValueError("Invalid command name {!r}: {}".format(cur_cmd, errmsg))
-
# Stores results from the last command run to enable usage of results in a Python script or interactive console
# Built-in commands don't make use of this. It is purely there for user-defined commands and convenience.
self.last_result = None
@@ -412,6 +396,28 @@ class Cmd(cmd.Cmd):
# If False, then complete() will sort the matches using self.default_sort_key before they are displayed.
self.matches_sorted = False
+ ############################################################################################################
+ # The following code block loads CommandSets, verifies command names, and registers subcommands.
+ # This block should appear after all attributes have been created since the registration code
+ # depends on them and it's possible a module's on_register() method may need to access some.
+ ############################################################################################################
+ # Load modular commands
+ self._installed_command_sets = [] # type: List[CommandSet]
+ self._cmd_to_command_sets = {} # type: Dict[str, CommandSet]
+ if command_sets:
+ for command_set in command_sets:
+ self.register_command_set(command_set)
+
+ if auto_load_commands:
+ self._autoload_commands()
+
+ # Verify commands don't have invalid names (like starting with a shortcut)
+ for cur_cmd in self.get_all_commands():
+ valid, errmsg = self.statement_parser.is_valid_command(cur_cmd)
+ if not valid:
+ raise ValueError("Invalid command name {!r}: {}".format(cur_cmd, errmsg))
+
+ # Add functions decorated to be subcommands
self._register_subcommands(self)
def find_commandsets(self, commandset_type: Type[CommandSet], *, subclass_match: bool = False) -> List[CommandSet]: