Browse Source

Issue #122: human friendly option names

pull/121/merge
= 9 years ago
committed by chriskiehl
parent
commit
fa2f556c74
2 changed files with 26 additions and 5 deletions
  1. 7
      gooey/python_bindings/argparse_to_json.py
  2. 24
      gooey/python_bindings/gooey_parser.py

7
gooey/python_bindings/argparse_to_json.py

@ -9,7 +9,8 @@ from argparse import (
_StoreConstAction,
_StoreFalseAction,
_StoreTrueAction,
ArgumentParser, _SubParsersAction)
ArgumentParser,
_SubParsersAction)
from collections import OrderedDict
from functools import partial
@ -150,7 +151,7 @@ def build_radio_group(mutex_group):
options = [
{
'display_name': mutex_arg.dest,
'display_name': mutex_arg.metavar or mutex_arg.dest,
'help': mutex_arg.help,
'nargs': mutex_arg.nargs or '',
'commands': mutex_arg.option_strings,
@ -174,7 +175,7 @@ def as_json(action, widget, required):
'type': widget,
'required': required,
'data': {
'display_name': action.dest,
'display_name': action.metavar or action.dest,
'help': action.help,
'nargs': action.nargs or '',
'commands': action.option_strings,

24
gooey/python_bindings/gooey_parser.py

@ -1,10 +1,23 @@
from argparse import ArgumentParser, _SubParsersAction
from argparse import ArgumentParser, _SubParsersAction, _MutuallyExclusiveGroup
class GooeySubParser(_SubParsersAction):
def __init__(self, *args, **kwargs):
super(GooeySubParser, self).__init__(*args, **kwargs)
class GooeyMutuallyExclusiveGroup(_MutuallyExclusiveGroup):
def __init__(self, parser, widgets, *args, **kwargs):
self.parser = parser
self.widgets = widgets
super(GooeyMutuallyExclusiveGroup, self).__init__(self.parser, *args, **kwargs)
def add_argument(self, *args, **kwargs):
widget = kwargs.pop('widget', None)
metavar = kwargs.pop('metavar', None)
super(GooeyMutuallyExclusiveGroup, self).add_argument(*args, **kwargs)
self.parser._actions[-1].metavar = metavar
self.widgets[self.parser._actions[-1].dest] = widget
class GooeyParser(object):
def __init__(self, **kwargs):
@ -25,11 +38,18 @@ class GooeyParser(object):
def add_argument(self, *args, **kwargs):
widget = kwargs.pop('widget', None)
metavar = kwargs.pop('metavar', None)
self.parser.add_argument(*args, **kwargs)
self.parser._actions[-1].metavar = metavar
self.widgets[self.parser._actions[-1].dest] = widget
# def add_mutually_exclusive_group(self, **kwargs):
# return self.parser.add_mutually_exclusive_group(**kwargs)
def add_mutually_exclusive_group(self, **kwargs):
return self.parser.add_mutually_exclusive_group(**kwargs)
group = GooeyMutuallyExclusiveGroup(self.parser, self.widgets, **kwargs)
self.parser._mutually_exclusive_groups.append(group)
return group
def add_argument_group(self, *args, **kwargs):
return self.parser.add_argument_group(*args, **kwargs)

Loading…
Cancel
Save