choice already manage by argparse

This commit is contained in:
Emmanuel Garette 2019-08-03 10:33:45 +02:00
parent e6a9a37607
commit 46866f1e38
1 changed files with 8 additions and 4 deletions

View File

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