From 50df3503f2cba969f05876c81aa6ce36a1147666 Mon Sep 17 00:00:00 2001 From: jaca Date: Wed, 27 Sep 2023 19:23:41 +0200 Subject: [PATCH] resolve #763 -- choose multiselect for options with choices and nargs=+ or * --- gooey/python_bindings/argparse_to_json.py | 13 +++++++++++-- gooey/tests/test_argparse_to_json.py | 16 ++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/gooey/python_bindings/argparse_to_json.py b/gooey/python_bindings/argparse_to_json.py index a209aef..dfe8a2d 100644 --- a/gooey/python_bindings/argparse_to_json.py +++ b/gooey/python_bindings/argparse_to_json.py @@ -304,6 +304,8 @@ def categorize(actions, widget_dict, options): # pre-fill the 'counter' dropdown _json['data']['choices'] = list(map(str, range(0, 11))) yield _json + elif is_listbox(action): + yield action_to_json(action, _get_widget(action, 'Listbox'), options) else: raise UnknownWidgetType(action) @@ -352,7 +354,7 @@ def is_optional(action): def is_choice(action): ''' action with choices supplied ''' - return action.choices + return action.choices and not action.nargs def is_file(action): ''' action with FileType ''' @@ -404,6 +406,13 @@ def is_counter(action): return isinstance(action, _CountAction) +def is_listbox(action): + """ _actions whic can be translated into a Listbox """ + return (isinstance(action, _StoreAction) + and action.choices + and action.nargs in {'+', '*'}) + + def is_default_progname(name, subparser): return subparser.prog == '{} {}'.format(os.path.split(sys.argv[0])[-1], name) @@ -681,4 +690,4 @@ def this_is_a_comment(action, widget): 'TextField', 'Textarea', 'PasswordField', - ] \ No newline at end of file + ] diff --git a/gooey/tests/test_argparse_to_json.py b/gooey/tests/test_argparse_to_json.py index 3e978b4..162a9d5 100644 --- a/gooey/tests/test_argparse_to_json.py +++ b/gooey/tests/test_argparse_to_json.py @@ -290,3 +290,19 @@ class TestArgparse(unittest.TestCase): result = next(argparse_to_json.categorize(action, {}, {})) self.assertEqual(result['type'], expected_widget) + + def test_nargs_with_choices_chooses_good_widget(self): + """ + #763 argument with nargs in {+, *} and a list of choices should use + a Listbox widget + """ + cases = ['*', '+'] + + for nargs in cases: + with self.subTest(f'expect {nargs} to produce a Listbox'): + parser = ArgumentParser() + parser.add_argument('foo', nargs=nargs, choices=['choice', 'choice1']) + action = [parser._actions[-1]] + result = next(argparse_to_json.categorize(action, {}, {})) + self.assertEqual(result['type'], 'Listbox') +