Browse Source

resolve #763 -- choose multiselect for options with choices and nargs=+ or *

pull/892/head
jaca 1 year ago
parent
commit
50df3503f2
No known key found for this signature in database GPG Key ID: B67CEF4EE3D609B0
2 changed files with 27 additions and 2 deletions
  1. 13
      gooey/python_bindings/argparse_to_json.py
  2. 16
      gooey/tests/test_argparse_to_json.py

13
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',
]
]

16
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')
Loading…
Cancel
Save