This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author tellendil
Recipients paul.j3, tellendil
Date 2015-05-12.08:19:21
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1431418761.21.0.782828969175.issue24166@psf.upfronthosting.co.za>
In-reply-to
Content
I solved my problem by subclassing the ArgumentParser and redefining parse_args as follow :

class MultipleArgumentParser(ArgumentParser):
    def parse_args(self, args=None, namespace=None):
        args, argv = self.parse_known_args(args, namespace)

        if not argv:
            return args

        # save old actions, before rerunning the parser without the _SubParsersActions
        self._old_actions = self._actions.copy()
        self._actions = [action for action in self._old_actions if not isinstance(action, _SubParsersAction)]

        # parse the remaining command line
        args2, argv2 = self.parse_known_args(argv, None)

        self._actions = self._old_actions.copy()

        if argv2:
            msg = _('unrecognized arguments: %s')
            self.error(msg % ' '.join(argv2))

        for key, value in vars(args2).items():
            if isinstance(value, collections.Iterable):
                setattr(args, key, [value for value in itertools.chain(getattr(args, key), value)])

        return args


I know this is not generic enough and not cleanly done. However, would this be an interesting addition to the argumentparser ? If so, I can try to make a generic implementation, which would allow having multiple arguments after a subparser which did not match them
History
Date User Action Args
2015-05-12 08:19:21tellendilsetrecipients: + tellendil, paul.j3
2015-05-12 08:19:21tellendilsetmessageid: <1431418761.21.0.782828969175.issue24166@psf.upfronthosting.co.za>
2015-05-12 08:19:21tellendillinkissue24166 messages
2015-05-12 08:19:21tellendilcreate