diff --git a/gooey/python_bindings/argparse_to_json.py b/gooey/python_bindings/argparse_to_json.py index a209aef5..dfe8a2db 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 3e978b40..162a9d50 100644 --- a/gooey/tests/test_argparse_to_json.py +++ b/gooey/tests/test_argparse_to_json.py @@ -290,3 +290,19 @@ def test_filetype_chooses_good_widget(self): 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') +