Skip to content

Commit

Permalink
Merge pull request #1003 from python-cmd2/move_attach
Browse files Browse the repository at this point in the history
Setting subcommand handler on attached_parser and not its parent parser
  • Loading branch information
kmvanbrunt authored Sep 30, 2020
2 parents 77444b7 + 980a7e4 commit ed7b9e5
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions cmd2/cmd2.py
Original file line number Diff line number Diff line change
Expand Up @@ -676,10 +676,6 @@ def _register_subcommands(self, cmdset: Union[CommandSet, 'Cmd']) -> None:
raise CommandSetRegistrationError('Could not find argparser for command "{}" needed by subcommand: {}'
.format(command_name, str(method)))

# Set the subcommand handler function
defaults = {constants.NS_ATTR_SUBCMD_HANDLER: method}
subcmd_parser.set_defaults(**defaults)

def find_subcommand(action: argparse.ArgumentParser, subcmd_names: List[str]) -> argparse.ArgumentParser:
if not subcmd_names:
return action
Expand Down Expand Up @@ -720,6 +716,12 @@ def find_subcommand(action: argparse.ArgumentParser, subcmd_names: List[str]) ->
add_parser_kwargs['add_help'] = False

attached_parser = action.add_parser(subcommand_name, **add_parser_kwargs)

# Set the subcommand handler
defaults = {constants.NS_ATTR_SUBCMD_HANDLER: method}
attached_parser.set_defaults(**defaults)

# Set what instance the handler is bound to
setattr(attached_parser, constants.PARSER_ATTR_COMMANDSET, cmdset)
break

Expand Down

0 comments on commit ed7b9e5

Please sign in to comment.