From 39d3d33e5e1a049aac74a18610b877a52bf86179 Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Wed, 7 Aug 2024 08:23:05 +0200 Subject: [PATCH] add add_extra_options parameter --- tests/test_choice.py | 2 +- tests/test_leadership.py | 92 ++++++++++++++++++++++++++++++++-- tiramisu_cmdline_parser/api.py | 31 +++++++----- 3 files changed, 109 insertions(+), 16 deletions(-) diff --git a/tests/test_choice.py b/tests/test_choice.py index b16ec3b..750252d 100644 --- a/tests/test_choice.py +++ b/tests/test_choice.py @@ -26,7 +26,7 @@ def get_config(json): properties=('positional', 'mandatory')) str_ = ChoiceOption('str', 'choice the sub argument', - ('str1', 'str2', 'str3')) + ('str1', 'str2', 'str3', None)) int_ = ChoiceOption('int', 'choice the sub argument', (1, 2, 3)) diff --git a/tests/test_leadership.py b/tests/test_leadership.py index 813eac8..6787203 100644 --- a/tests/test_leadership.py +++ b/tests/test_leadership.py @@ -5,7 +5,7 @@ import pytest from tiramisu_cmdline_parser import TiramisuCmdlineParser from tiramisu import IntOption, StrOption, BoolOption, ChoiceOption, \ - OptionDescription, Leadership, Config, submulti + OptionDescription, Leadership, SymLinkOption, Config, submulti try: from tiramisu_api import Config as JsonConfig # params = ['tiramisu', 'tiramisu-json'] @@ -16,8 +16,14 @@ except: from .utils import TestHelpFormatter, to_dict -def get_config(json, with_mandatory=False): - leader = StrOption('leader', "Leader var", ['192.168.0.1'], multi=True) +def get_config(json, with_mandatory=False, with_symlink=False, with_default_value=True): + if with_default_value: + default = ['192.168.0.1'] + else: + default = [] + leader = StrOption('leader', "Leader var", default, multi=True) + if with_symlink: + link_leader = SymLinkOption('l', leader) follower = StrOption('follower', "Follower", multi=True) if with_mandatory: properties = ('mandatory',) @@ -25,9 +31,14 @@ def get_config(json, with_mandatory=False): properties = None follower_submulti = StrOption('follower_submulti', "Follower submulti", multi=submulti, properties=properties) follower_integer = IntOption('follower_integer', "Follower integer", multi=True) + if with_symlink: + link_follower = SymLinkOption('i', follower_integer) follower_boolean = BoolOption('follower_boolean', "Follower boolean", multi=True) follower_choice = ChoiceOption('follower_choice', "Follower choice", ('opt1', 'opt2'), multi=True) opt_list = [leader, follower, follower_submulti, follower_integer, follower_boolean, follower_choice] + if with_symlink: + opt_list.append(link_leader) + opt_list.append(link_follower) if with_mandatory: opt_list.append(StrOption('follower_mandatory', "Follower mandatory", multi=True, properties=('mandatory',))) leadership = Leadership('leader', 'leader', opt_list) @@ -74,6 +85,81 @@ leader: assert f.getvalue() == output +def test_leadership_help_no_pop(json): + output = """usage: prog.py [-h] [--leader.leader [LEADER ...]] [--leader.follower INDEX [FOLLOWER]] --leader.follower_submulti INDEX [FOLLOWER_SUBMULTI ...] [--leader.follower_integer INDEX [FOLLOWER_INTEGER]] [--leader.follower_boolean INDEX] [--leader.follower_choice INDEX [{opt1,opt2}]] --leader.follower_mandatory INDEX FOLLOWER_MANDATORY + +options: + -h, --help show this help message and exit + +leader: + --leader.leader [LEADER ...] + Leader var + --leader.follower INDEX [FOLLOWER] + Follower + --leader.follower_submulti INDEX [FOLLOWER_SUBMULTI ...] + Follower submulti + --leader.follower_integer INDEX [FOLLOWER_INTEGER] + Follower integer + --leader.follower_boolean INDEX + Follower boolean + --leader.follower_choice INDEX [{opt1,opt2}] + Follower choice + --leader.follower_mandatory INDEX FOLLOWER_MANDATORY + Follower mandatory +""" + parser = TiramisuCmdlineParser(get_config(json, with_mandatory=True), 'prog.py', add_extra_options=False, formatter_class=TestHelpFormatter) + f = StringIO() + with redirect_stdout(f): + parser.print_help() + assert f.getvalue() == output + + +def test_leadership_help_short_no_default(json): + output = """usage: prog.py [-h] [-l [LEADER ...]] + +options: + -h, --help show this help message and exit + +leader: + -l [LEADER ...], --leader.leader [LEADER ...] + Leader var +""" + parser = TiramisuCmdlineParser(get_config(json, with_mandatory=True, with_symlink=True, with_default_value=False), 'prog.py', add_extra_options=False, formatter_class=TestHelpFormatter) + f = StringIO() + with redirect_stdout(f): + parser.print_help() + assert f.getvalue() == output + + +def test_leadership_help_short(json): + output = """usage: prog.py [-h] [-l [LEADER ...]] [--leader.follower INDEX [FOLLOWER]] --leader.follower_submulti INDEX [FOLLOWER_SUBMULTI ...] [-i INDEX [FOLLOWER_INTEGER]] [--leader.follower_boolean INDEX] [--leader.follower_choice INDEX [{opt1,opt2}]] --leader.follower_mandatory INDEX FOLLOWER_MANDATORY + +options: + -h, --help show this help message and exit + +leader: + -l [LEADER ...], --leader.leader [LEADER ...] + Leader var + --leader.follower INDEX [FOLLOWER] + Follower + --leader.follower_submulti INDEX [FOLLOWER_SUBMULTI ...] + Follower submulti + -i INDEX [FOLLOWER_INTEGER], --leader.follower_integer INDEX [FOLLOWER_INTEGER] + Follower integer + --leader.follower_boolean INDEX + Follower boolean + --leader.follower_choice INDEX [{opt1,opt2}] + Follower choice + --leader.follower_mandatory INDEX FOLLOWER_MANDATORY + Follower mandatory +""" + parser = TiramisuCmdlineParser(get_config(json, with_mandatory=True, with_symlink=True), 'prog.py', add_extra_options=False, formatter_class=TestHelpFormatter) + f = StringIO() + with redirect_stdout(f): + parser.print_help() + assert f.getvalue() == output + + def test_leadership_modif_leader(json): output = {'leader.leader': ['192.168.1.1'], 'leader.follower': [None], diff --git a/tiramisu_cmdline_parser/api.py b/tiramisu_cmdline_parser/api.py index 05d5287..87e051a 100644 --- a/tiramisu_cmdline_parser/api.py +++ b/tiramisu_cmdline_parser/api.py @@ -19,7 +19,7 @@ from gettext import gettext as _ #try: from tiramisu import Config -from tiramisu.error import PropertiesOptionError, LeadershipError +from tiramisu.error import PropertiesOptionError, LeadershipError, ConfigError #except ImportError: # Config = None # from tiramisu_api.error import PropertiesOptionError @@ -37,9 +37,7 @@ def get_choice_list(config, properties, display): if isinstance(choice, int): return str(choice) return choice - choices = [convert(choice) for choice in config.value.list()] - if choices and choices[0] == '': - del choices[0] + choices = [convert(choice) for choice in config.value.list() if choice != '' and choice is not None] if display: choices = '{{{}}}'.format(','.join(choices)) if 'mandatory' not in properties: @@ -284,7 +282,7 @@ class TiramisuCmdlineParser(ArgumentParser): display_modified_value: bool=True, formatter_class=ArgumentDefaultsHelpFormatter, unrestraint: bool=False, - add_no_option_to_boolean: bool=True, + add_extra_options: bool=True, short_name_max_len: int=1, _forhelp: bool=False, **kwargs): @@ -295,7 +293,7 @@ class TiramisuCmdlineParser(ArgumentParser): self.root = root self.remove_empty_od = remove_empty_od self.unrestraint = unrestraint - self.add_no_option_to_boolean = add_no_option_to_boolean + self.add_extra_options = add_extra_options self.display_modified_value = display_modified_value self.short_name_max_len = short_name_max_len if TiramisuHelpFormatter not in formatter_class.__mro__: @@ -365,7 +363,7 @@ class TiramisuCmdlineParser(ArgumentParser): epilog=self.epilog, description=self.description, unrestraint=self.unrestraint, - add_no_option_to_boolean=self.add_no_option_to_boolean, + add_extra_options=self.add_extra_options, short_name_max_len=self.short_name_max_len, fullpath=self.fullpath) namespace_, args_ = new_parser._parse_known_args(args_, new_parser.namespace) @@ -409,6 +407,7 @@ class TiramisuCmdlineParser(ArgumentParser): prefix: Optional[str], _forhelp: bool, group, level): + obj = None for obj in config: # do not display frozen option if 'frozen' in obj.property.get(): @@ -426,7 +425,7 @@ class TiramisuCmdlineParser(ArgumentParser): else: prefix_ = obj.path() self._config_to_argparser(_forhelp, obj, prefix_, newgroup, level + 1) - elif self.add_no_option_to_boolean and obj.type() == 'boolean' and not obj.issymlinkoption(): + elif self.add_extra_options and obj.type() == 'boolean' and not obj.issymlinkoption(): if not obj.isleader(): yield obj, False, None yield obj, True, None @@ -434,13 +433,21 @@ class TiramisuCmdlineParser(ArgumentParser): yield obj, False, False yield obj, False, True yield obj, True, None - elif obj.isleader(): + elif self.add_extra_options and obj.isleader(): yield obj, None, False yield obj, None, True else: if obj.type() == 'boolean' and obj.value.default() is True: - raise ValueError(_(f'the boolean "{obj.path()}" cannot have a default value to True with option add_no_option_to_boolean')) + raise ValueError(_(f'the boolean "{obj.path()}" cannot have a default value to True with option add_extra_options')) yield obj, None, None + if obj is not None and not obj.isoptiondescription() and obj.isleader(): + # no follower found, search if there is a symlink + for sobj in config.list(uncalculated=True): + try: + if sobj.issymlinkoption() and sobj.option().isleader(): + yield sobj, None, None + except ConfigError: + pass def _config_to_argparser(self, _forhelp: bool, @@ -639,7 +646,7 @@ class TiramisuCmdlineParser(ArgumentParser): remove_empty_od=self.remove_empty_od, display_modified_value=self.display_modified_value, formatter_class=self.formatter_class, - add_no_option_to_boolean=self.add_no_option_to_boolean, + add_extra_options=self.add_extra_options, short_name_max_len=self.short_name_max_len, epilog=self.epilog, description=self.description, @@ -654,7 +661,7 @@ class TiramisuCmdlineParser(ArgumentParser): remove_empty_od=self.remove_empty_od, display_modified_value=self.display_modified_value, formatter_class=self.formatter_class, - add_no_option_to_boolean=self.add_no_option_to_boolean, + add_extra_options=self.add_extra_options, short_name_max_len=self.short_name_max_len, epilog=self.epilog, description=self.description,