Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 1 commit into
base: 1.2.1-release
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions gooey/python_bindings/argparse_to_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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 '''
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -681,4 +690,4 @@ def this_is_a_comment(action, widget):
'TextField',
'Textarea',
'PasswordField',
]
]
16 changes: 16 additions & 0 deletions gooey/tests/test_argparse_to_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')