You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
3.4 KiB

  1. from argparse import ArgumentParser, _SubParsersAction, _MutuallyExclusiveGroup
  2. class GooeySubParser(_SubParsersAction):
  3. def __init__(self, *args, **kwargs):
  4. super(GooeySubParser, self).__init__(*args, **kwargs)
  5. class GooeyMutuallyExclusiveGroup(_MutuallyExclusiveGroup):
  6. def __init__(self, parser, widgets, *args, **kwargs):
  7. self.parser = parser
  8. self.widgets = widgets
  9. super(GooeyMutuallyExclusiveGroup, self).__init__(self.parser, *args, **kwargs)
  10. def add_argument(self, *args, **kwargs):
  11. widget = kwargs.pop('widget', None)
  12. metavar = kwargs.pop('metavar', None)
  13. super(GooeyMutuallyExclusiveGroup, self).add_argument(*args, **kwargs)
  14. self.parser._actions[-1].metavar = metavar
  15. self.widgets[self.parser._actions[-1].dest] = widget
  16. class GooeyParser(object):
  17. def __init__(self, **kwargs):
  18. self.__dict__['parser'] = ArgumentParser(**kwargs)
  19. self.widgets = {}
  20. @property
  21. def _mutually_exclusive_groups(self):
  22. return self.parser._mutually_exclusive_groups
  23. @property
  24. def _actions(self):
  25. return self.parser._actions
  26. @property
  27. def description(self):
  28. return self.parser.description
  29. def add_argument(self, *args, **kwargs):
  30. widget = kwargs.pop('widget', None)
  31. metavar = kwargs.pop('metavar', None)
  32. self.parser.add_argument(*args, **kwargs)
  33. self.parser._actions[-1].metavar = metavar
  34. self.widgets[self.parser._actions[-1].dest] = widget
  35. # def add_mutually_exclusive_group(self, **kwargs):
  36. # return self.parser.add_mutually_exclusive_group(**kwargs)
  37. def add_mutually_exclusive_group(self, **kwargs):
  38. group = GooeyMutuallyExclusiveGroup(self.parser, self.widgets, **kwargs)
  39. self.parser._mutually_exclusive_groups.append(group)
  40. return group
  41. def add_argument_group(self, *args, **kwargs):
  42. return self.parser.add_argument_group(*args, **kwargs)
  43. def parse_args(self, args=None, namespace=None):
  44. return self.parser.parse_args(args, namespace)
  45. def add_subparsers(self, **kwargs):
  46. if self._subparsers is not None:
  47. self.error(_('cannot have multiple subparser arguments'))
  48. # add the parser class to the arguments if it's not present
  49. kwargs.setdefault('parser_class', type(self))
  50. if 'title' in kwargs or 'description' in kwargs:
  51. title = _(kwargs.pop('title', 'subcommands'))
  52. description = _(kwargs.pop('description', None))
  53. self._subparsers = self.add_argument_group(title, description)
  54. else:
  55. self._subparsers = self._positionals
  56. # prog defaults to the usage message of this parser, skipping
  57. # optional arguments and with no "usage:" prefix
  58. if kwargs.get('prog') is None:
  59. formatter = self._get_formatter()
  60. positionals = self._get_positional_actions()
  61. groups = self._mutually_exclusive_groups
  62. formatter.add_usage(self.usage, positionals, groups, '')
  63. kwargs['prog'] = formatter.format_help().strip()
  64. # create the parsers action and add it to the positionals list
  65. parsers_class = self._pop_action_class(kwargs, 'parsers')
  66. action = parsers_class(option_strings=[], **kwargs)
  67. self._subparsers._add_action(action)
  68. # return the created parsers action
  69. return action
  70. def __getattr__(self, item):
  71. return getattr(self.parser, item)
  72. def __setattr__(self, key, value):
  73. return setattr(self.parser, key, value)