Skip to content

Setting subcommand handler on attached_parser and not its parent parser #1003

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 30, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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