[issue24166] ArgumentParser behavior does not match generated help
Benjamin Schubert
report at bugs.python.org
Tue May 12 10:19:21 CEST 2015
Benjamin Schubert added the comment:
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
----------
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue24166>
_______________________________________
More information about the Python-bugs-list
mailing list