From fa2f556c7465fac5dd35fc28bf00fe45a2d18494 Mon Sep 17 00:00:00 2001 From: = <=> Date: Sat, 31 Oct 2015 12:13:24 -0400 Subject: [PATCH] Issue #122: human friendly option names --- gooey/python_bindings/argparse_to_json.py | 7 ++++--- gooey/python_bindings/gooey_parser.py | 24 +++++++++++++++++++++-- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/gooey/python_bindings/argparse_to_json.py b/gooey/python_bindings/argparse_to_json.py index 3508f7d..f4e3393 100644 --- a/gooey/python_bindings/argparse_to_json.py +++ b/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, diff --git a/gooey/python_bindings/gooey_parser.py b/gooey/python_bindings/gooey_parser.py index 9fe18e8..334e606 100644 --- a/gooey/python_bindings/gooey_parser.py +++ b/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)