From 46866f1e38a7f8001a95a836ef79bcf3e6ddf380 Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Sat, 3 Aug 2019 10:33:45 +0200 Subject: [PATCH] choice already manage by argparse --- tiramisu_cmdline_parser/api.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/tiramisu_cmdline_parser/api.py b/tiramisu_cmdline_parser/api.py index aac4de8..191da78 100644 --- a/tiramisu_cmdline_parser/api.py +++ b/tiramisu_cmdline_parser/api.py @@ -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: