From 2184332e625f548244e7286ec1def544d9a8917f Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Thu, 22 Aug 2019 15:58:34 +0200 Subject: [PATCH] python 3.5 support --- tests/test_readme.py | 37 +++++++++++++++++++++++++---- tiramisu_cmdline_parser/__init__.py | 2 +- tiramisu_cmdline_parser/api.py | 12 +++++----- 3 files changed, 40 insertions(+), 11 deletions(-) diff --git a/tests/test_readme.py b/tests/test_readme.py index b74de2d..65089ee 100644 --- a/tests/test_readme.py +++ b/tests/test_readme.py @@ -487,13 +487,13 @@ prog.py: error: unrecognized arguments: --int def test_readme_cross_tree(json): output = """usage: prog.py "none" [-h] [-v] [-nv] {str,list,int,none} -prog.py: error: unrecognized arguments: --int +prog.py: error: unrecognized arguments: --root.int """ parser = TiramisuCmdlineParser(get_config(json, True), 'prog.py') f = StringIO() with redirect_stderr(f): try: - parser.parse_args(['none', '--int']) + parser.parse_args(['none', '--root.int']) except SystemExit as err: assert str(err) == "2" else: @@ -503,13 +503,13 @@ prog.py: error: unrecognized arguments: --int def test_readme_cross_tree_remove(json): output = """usage: prog.py "none" [-h] [-v] [-nv] -prog.py: error: unrecognized arguments: --int +prog.py: error: unrecognized arguments: --root.int """ parser = TiramisuCmdlineParser(get_config(json, True), 'prog.py', display_modified_value=False) f = StringIO() with redirect_stderr(f): try: - parser.parse_args(['none', '--int']) + parser.parse_args(['none', '--root.int']) except SystemExit as err: assert str(err) == "2" else: @@ -859,3 +859,32 @@ def test_readme_longargument(json): parser = TiramisuCmdlineParser(config, 'prog.py') parser.parse_args(['list', '--list', 'a', '--v']) assert config.value.dict() == output + + +def test_readme_unknown_key(json): + output1 = """usage: prog.py [-h] [-v] [-nv] {str,list,int,none} +prog.py: error: unrecognized arguments: --unknown +""" + output2 = """usage: prog.py [-h] [-v] [-nv] {str,list,int,none} +prog.py: error: unrecognized arguments: --root.unknown +""" + parser = TiramisuCmdlineParser(get_config(json, True), 'prog.py', fullpath=False) + f = StringIO() + with redirect_stderr(f): + try: + parser.parse_args(['--unknown']) + except SystemExit as err: + assert str(err) == "2" + else: + raise Exception('must raises') + assert f.getvalue() == output1 + + f = StringIO() + with redirect_stderr(f): + try: + parser.parse_args(['--root.unknown']) + except SystemExit as err: + assert str(err) == "2" + else: + raise Exception('must raises') + assert f.getvalue() == output2 diff --git a/tiramisu_cmdline_parser/__init__.py b/tiramisu_cmdline_parser/__init__.py index 124abc5..2a5a6ac 100644 --- a/tiramisu_cmdline_parser/__init__.py +++ b/tiramisu_cmdline_parser/__init__.py @@ -1,4 +1,4 @@ from .api import TiramisuCmdlineParser -__version__ = "0.2" +__version__ = "0.3" __all__ = ('TiramisuCmdlineParser',) diff --git a/tiramisu_cmdline_parser/api.py b/tiramisu_cmdline_parser/api.py index 191da78..17cf642 100644 --- a/tiramisu_cmdline_parser/api.py +++ b/tiramisu_cmdline_parser/api.py @@ -20,7 +20,7 @@ from gettext import gettext as _ try: from tiramisu import Config from tiramisu.error import PropertiesOptionError, RequirementError, LeadershipError -except (ModuleNotFoundError, ImportError): +except ImportError: Config = None from tiramisu_api.error import PropertiesOptionError RequirementError = PropertiesOptionError @@ -29,7 +29,7 @@ try: from tiramisu__api import Config as ConfigJson if Config is None: Config = ConfigJson -except ModuleNotFoundError: +except ImportError: ConfigJson = Config @@ -44,7 +44,7 @@ def get_choice_list(obj, properties, display): if display: choices = '{{{}}}'.format(','.join(choices)) if 'mandatory' not in properties: - choices = f'[{choices}]' + choices = '[{}]'.format(choices) return choices @@ -376,9 +376,9 @@ class TiramisuCmdlineParser(ArgumentParser): if type != 'boolean': if isinstance(value, list): for val in value: - self.prog += f' "{val}"' + self.prog += ' "{}"'.format(val) else: - self.prog += f' "{value}"' + self.prog += ' "{}"'.format(value) def _config_list(self, config: Config, @@ -512,7 +512,7 @@ class TiramisuCmdlineParser(ArgumentParser): else: kwargs['nargs'] = 2 if _forhelp and 'mandatory' not in properties: - metavar = f'[{metavar}]' + metavar = '[{}]'.format(metavar) 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))