Skip to content

Commit ed7b9e5

Browse files
authored
Merge pull request #1003 from python-cmd2/move_attach
Setting subcommand handler on attached_parser and not its parent parser
2 parents 77444b7 + 980a7e4 commit ed7b9e5

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

cmd2/cmd2.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -676,10 +676,6 @@ def _register_subcommands(self, cmdset: Union[CommandSet, 'Cmd']) -> None:
676676
raise CommandSetRegistrationError('Could not find argparser for command "{}" needed by subcommand: {}'
677677
.format(command_name, str(method)))
678678

679-
# Set the subcommand handler function
680-
defaults = {constants.NS_ATTR_SUBCMD_HANDLER: method}
681-
subcmd_parser.set_defaults(**defaults)
682-
683679
def find_subcommand(action: argparse.ArgumentParser, subcmd_names: List[str]) -> argparse.ArgumentParser:
684680
if not subcmd_names:
685681
return action
@@ -720,6 +716,12 @@ def find_subcommand(action: argparse.ArgumentParser, subcmd_names: List[str]) ->
720716
add_parser_kwargs['add_help'] = False
721717

722718
attached_parser = action.add_parser(subcommand_name, **add_parser_kwargs)
719+
720+
# Set the subcommand handler
721+
defaults = {constants.NS_ATTR_SUBCMD_HANDLER: method}
722+
attached_parser.set_defaults(**defaults)
723+
724+
# Set what instance the handler is bound to
723725
setattr(attached_parser, constants.PARSER_ATTR_COMMANDSET, cmdset)
724726
break
725727

0 commit comments

Comments
 (0)