choice already manage by argparse

This commit is contained in:
Emmanuel Garette 2019-08-03 10:33:45 +02:00 committed by Emmanuel Garette
parent dc94b37896
commit 11b2bcc591

View file

@ -34,7 +34,11 @@ except ModuleNotFoundError:
def get_choice_list(obj, properties, display):
choices = obj.value.list()
def convert(choice):
if isinstance(choice, int):
return str(choice)
return choice
choices = [convert(choice) for choice in obj.value.list()]
if choices[0] == '':
del choices[0]
if display:
@ -109,7 +113,6 @@ class TiramisuNamespace(Namespace):
_setattr(option, true_key, key, value)
except ValueError as err:
if option.option.type() == 'choice':
choices = get_choice_list(option, option.property.get(), False)
values = option.value.list()
if isinstance(true_value, list):
for val in value:
@ -118,6 +121,7 @@ class TiramisuNamespace(Namespace):
break
else:
display_value = true_value
choices = get_choice_list(option, option.property.get(), False)
raise ValueError("argument {}: invalid choice: '{}' (choose from {})".format(self.arguments[key], display_value, ', '.join([f"'{val}'" for val in choices])))
else:
raise err
@ -509,7 +513,7 @@ class TiramisuCmdlineParser(ArgumentParser):
kwargs['nargs'] = 2
if _forhelp and 'mandatory' not in properties:
metavar = f'[{metavar}]'
if option.type() == 'choice' and _forhelp:
if option.type() == 'choice':
# do not manage choice with argparse there is problem with integer problem
kwargs['metavar'] = ('INDEX', get_choice_list(obj, properties, True))
else:
@ -525,7 +529,7 @@ class TiramisuCmdlineParser(ArgumentParser):
if _forhelp and option.type() == 'boolean':
kwargs['metavar'] = 'INDEX'
kwargs['nargs'] = 1
elif option.type() == 'choice' and not option.isfollower() and _forhelp:
elif option.type() == 'choice' and not option.isfollower():
# do not manage choice with argparse there is problem with integer problem
kwargs['choices'] = get_choice_list(obj, properties, False)
else: