diff options
author | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2020-08-20 19:35:37 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-08-20 19:35:37 -0400 |
commit | 5f76f955ba3c5cd7f4e3aaef3e9e2163d4160c7a (patch) | |
tree | 075c3bc028e1e9f711c9fa6f63301a418d7ee4c9 /cmd2/cmd2.py | |
parent | 5dd2d03ef35a3d33ff53d82c8039d68e263246ee (diff) | |
parent | 27b10936b123053accc41f20246a0c027d0cbb66 (diff) | |
download | cmd2-git-5f76f955ba3c5cd7f4e3aaef3e9e2163d4160c7a.tar.gz |
Merge pull request #980 from python-cmd2/move_module_loading
Fixed AttributeError when loading CommandSet
Diffstat (limited to 'cmd2/cmd2.py')
-rw-r--r-- | cmd2/cmd2.py | 44 |
1 files changed, 27 insertions, 17 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index 610ce4a3..f6a5251c 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]: @@ -631,10 +637,14 @@ class Cmd(cmd.Cmd): # iterate through all matching methods for method_name, method in methods: - subcommand_name = getattr(method, constants.SUBCMD_ATTR_NAME) + subcommand_name = getattr(method, constants.SUBCMD_ATTR_NAME) # type: str full_command_name = getattr(method, constants.SUBCMD_ATTR_COMMAND) # type: str subcmd_parser = getattr(method, constants.CMD_ATTR_ARGPARSER) + subcommand_valid, errmsg = self.statement_parser.is_valid_command(subcommand_name, is_subcommand=True) + if not subcommand_valid: + raise CommandSetRegistrationError('Subcommand {} is not valid: {}'.format(str(subcommand_name), errmsg)) + command_tokens = full_command_name.split() command_name = command_tokens[0] subcommand_names = command_tokens[1:] |