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.

96 lines
3.4 KiB

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