diff --git a/tests/test_leadership.py b/tests/test_leadership.py index 25d535d..3f21331 100644 --- a/tests/test_leadership.py +++ b/tests/test_leadership.py @@ -331,6 +331,53 @@ prog.py: error: the following arguments are required: --leader.follower_submulti assert config.value.dict() == output +def test_leadership_modif_mandatory_remove(json): + output = {'leader.leader': ['192.168.1.1'], + 'leader.follower': [None], + 'leader.follower_mandatory': ['255.255.255.128'], + 'leader.follower_boolean': [None], + 'leader.follower_choice': [None], + 'leader.follower_integer': [None], + 'leader.follower_submulti': [['255.255.255.128']]} + output2 = """usage: prog.py --leader.leader "192.168.1.1" [-h] [--leader.pop-leader INDEX] + [--leader.follower INDEX [FOLLOWER]] + --leader.follower_submulti INDEX + [FOLLOWER_SUBMULTI ...] + [--leader.follower_integer INDEX [FOLLOWER_INTEGER]] + [--leader.follower_boolean INDEX] + [--leader.no-follower_boolean INDEX] + [--leader.follower_choice INDEX [{opt1,opt2}]] + --leader.follower_mandatory INDEX + FOLLOWER_MANDATORY +prog.py: error: the following arguments are required: --leader.follower_submulti""" + + config = get_config(json, with_mandatory=True) + parser = TiramisuCmdlineParser(config, 'prog.py', display_modified_value=False) + f = StringIO() + with redirect_stderr(f): + try: + parser.parse_args(['--leader.leader', '192.168.1.1']) + except SystemExit as err: + assert str(err) == "2" + else: + raise Exception('must raises') + assert f.getvalue() == output2 + ', --leader.follower_mandatory\n' + f = StringIO() + with redirect_stderr(f): + try: + parser.parse_args(['--leader.leader', '192.168.1.1', + '--leader.follower_mandatory', '0', '255.255.255.128']) + except SystemExit as err: + assert str(err) == "2" + else: + raise Exception('must raises') + assert f.getvalue() == output2 + '\n' + parser.parse_args(['--leader.leader', '192.168.1.1', + '--leader.follower_submulti', '0', '255.255.255.128', + '--leader.follower_mandatory', '0', '255.255.255.128']) + assert config.value.dict() == output + + def test_leadership_modif_mandatory_unvalidate(json): output = {'leader.leader': ['192.168.1.1'], 'leader.follower': [None], diff --git a/tests/test_readme.py b/tests/test_readme.py index e3bb1e4..58848ce 100644 --- a/tests/test_readme.py +++ b/tests/test_readme.py @@ -154,6 +154,27 @@ optional arguments: assert f.getvalue() == output +def test_readme_help_modif_positional_remove(json): + output = """usage: prog.py "str" [-h] [-v] [-nv] --str STR + +optional arguments: + -h, --help show this help message and exit + -v, --verbosity increase output verbosity + -nv, --no-verbosity + --str STR string option +""" + parser = TiramisuCmdlineParser(get_config(json), 'prog.py', display_modified_value=False) + f = StringIO() + with redirect_stdout(f): + try: + parser.parse_args(['str', '--help']) + except SystemExit as err: + assert str(err) == "0" + else: + raise Exception('must raises') + assert f.getvalue() == output + + def test_readme_help_modif(json): output = """usage: prog.py "str" --str "toto" [-h] [-v] [-nv] --str STR {str,list,int,none} @@ -179,7 +200,27 @@ optional arguments: assert f.getvalue() == output -def test_readme_help_modif_short1(json): +def test_readme_help_modif_remove(json): + output = """usage: prog.py "str" --str "toto" [-h] [-v] [-nv] + +optional arguments: + -h, --help show this help message and exit + -v, --verbosity increase output verbosity + -nv, --no-verbosity +""" + parser = TiramisuCmdlineParser(get_config(json), 'prog.py', display_modified_value=False) + f = StringIO() + with redirect_stdout(f): + try: + parser.parse_args(['str', '--str', 'toto', '--help']) + except SystemExit as err: + assert str(err) == "0" + else: + raise Exception('must raises') + assert f.getvalue() == output + + +def test_readme_help_modif_short(json): output = """usage: prog.py "str" -v [-h] [-v] [-nv] --str STR {str,list,int,none} positional arguments: @@ -203,6 +244,27 @@ optional arguments: assert f.getvalue() == output +def test_readme_help_modif_short_remove(json): + # FIXME -v -nv ?? pas de description + output = """usage: prog.py "str" -v [-h] [-nv] --str STR + +optional arguments: + -h, --help show this help message and exit + -nv, --no-verbosity + --str STR string option +""" + parser = TiramisuCmdlineParser(get_config(json), 'prog.py', display_modified_value=False) + f = StringIO() + with redirect_stdout(f): + try: + parser.parse_args(['str', '-v', '--help']) + except SystemExit as err: + assert str(err) == "0" + else: + raise Exception('must raises') + assert f.getvalue() == output + + def test_readme_help_modif_short_no(json): output = """usage: prog.py "str" -v [-h] [-v] [-nv] --str STR {str,list,int,none} @@ -227,6 +289,27 @@ optional arguments: assert f.getvalue() == output +def test_readme_help_modif_short_no_remove(json): + # FIXME -v -nv ?? c'est -nv qui est set + output = """usage: prog.py "str" -v [-h] [-nv] --str STR + +optional arguments: + -h, --help show this help message and exit + -nv, --no-verbosity + --str STR string option +""" + parser = TiramisuCmdlineParser(get_config(json), 'prog.py', display_modified_value=False) + f = StringIO() + with redirect_stdout(f): + try: + parser.parse_args(['str', '-nv', '--help']) + except SystemExit as err: + assert str(err) == "0" + else: + raise Exception('must raises') + assert f.getvalue() == output + + def test_readme_positional_mandatory(json): output = """usage: prog.py [-h] [-v] [-nv] {str,list,int,none} prog.py: error: the following arguments are required: cmd @@ -291,6 +374,22 @@ prog.py: error: the following arguments are required: --str assert f.getvalue() == output +def test_readme_mandatory_remove(json): + output = """usage: prog.py "str" [-h] [-v] [-nv] --str STR +prog.py: error: the following arguments are required: --str +""" + parser = TiramisuCmdlineParser(get_config(json), 'prog.py', display_modified_value=False) + f = StringIO() + with redirect_stderr(f): + try: + parser.parse_args(['str']) + except SystemExit as err: + assert str(err) == "2" + else: + raise Exception('must raises') + assert f.getvalue() == output + + def test_readme_mandatory_tree(json): output = """usage: prog.py "str" [-h] [-v] [-nv] --root.str STR {str,list,int,none} prog.py: error: the following arguments are required: --root.str @@ -307,6 +406,22 @@ prog.py: error: the following arguments are required: --root.str assert f.getvalue() == output +def test_readme_mandatory_tree_remove(json): + output = """usage: prog.py "str" [-h] [-v] [-nv] --root.str STR +prog.py: error: the following arguments are required: --root.str +""" + parser = TiramisuCmdlineParser(get_config(json, True), 'prog.py', display_modified_value=False) + f = StringIO() + with redirect_stderr(f): + try: + parser.parse_args(['str']) + except SystemExit as err: + assert str(err) == "2" + else: + raise Exception('must raises') + assert f.getvalue() == output + + def test_readme_mandatory_tree_flatten(json): output = """usage: prog.py "str" [-h] [-v] [-nv] --str STR {str,list,int,none} prog.py: error: the following arguments are required: --str @@ -323,6 +438,22 @@ prog.py: error: the following arguments are required: --str assert f.getvalue() == output +def test_readme_mandatory_tree_flatten_remove(json): + output = """usage: prog.py "str" [-h] [-v] [-nv] --str STR +prog.py: error: the following arguments are required: --str +""" + parser = TiramisuCmdlineParser(get_config(json, True), 'prog.py', fullpath=False, display_modified_value=False) + f = StringIO() + with redirect_stderr(f): + try: + parser.parse_args(['str']) + except SystemExit as err: + assert str(err) == "2" + else: + raise Exception('must raises') + assert f.getvalue() == output + + def test_readme_cross(json): output = """usage: prog.py "none" [-h] [-v] [-nv] {str,list,int,none} prog.py: error: unrecognized arguments: --int @@ -339,6 +470,22 @@ prog.py: error: unrecognized arguments: --int assert f.getvalue() == output +def test_readme_cross_remove(json): + output = """usage: prog.py "none" [-h] [-v] [-nv] +prog.py: error: unrecognized arguments: --int +""" + parser = TiramisuCmdlineParser(get_config(json), 'prog.py', display_modified_value=False) + f = StringIO() + with redirect_stderr(f): + try: + parser.parse_args(['none', '--int']) + except SystemExit as err: + assert str(err) == "2" + else: + raise Exception('must raises') + assert f.getvalue() == output + + def test_readme_cross_tree(json): output = """usage: prog.py "none" [-h] [-v] [-nv] {str,list,int,none} prog.py: error: unrecognized arguments: --int @@ -355,6 +502,22 @@ prog.py: error: unrecognized arguments: --int assert f.getvalue() == output +def test_readme_cross_tree_remove(json): + output = """usage: prog.py "none" [-h] [-v] [-nv] +prog.py: error: unrecognized arguments: --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']) + except SystemExit as err: + assert str(err) == "2" + else: + raise Exception('must raises') + assert f.getvalue() == output + + def test_readme_cross_tree_flatten(json): output = """usage: prog.py "none" [-h] [-v] [-nv] {str,list,int,none} prog.py: error: unrecognized arguments: --int @@ -371,6 +534,22 @@ prog.py: error: unrecognized arguments: --int assert f.getvalue() == output +def test_readme_cross_tree_flatten_remove(json): + output = """usage: prog.py "none" [-h] [-v] [-nv] +prog.py: error: unrecognized arguments: --int +""" + parser = TiramisuCmdlineParser(get_config(json, True), 'prog.py', fullpath=False, display_modified_value=False) + f = StringIO() + with redirect_stderr(f): + try: + parser.parse_args(['none', '--int']) + except SystemExit as err: + assert str(err) == "2" + else: + raise Exception('must raises') + assert f.getvalue() == output + + def test_readme_unknown(json): output = """usage: prog.py [-h] [-v] [-nv] {str,list,int,none} prog.py: error: argument root.cmd: invalid choice: 'unknown' (choose from 'str', 'list', 'int', 'none') diff --git a/tiramisu_cmdline_parser/api.py b/tiramisu_cmdline_parser/api.py index 1ee08c1..4bbe00c 100644 --- a/tiramisu_cmdline_parser/api.py +++ b/tiramisu_cmdline_parser/api.py @@ -223,6 +223,7 @@ class TiramisuCmdlineParser(ArgumentParser): root: str=None, fullpath: bool=True, remove_empty_od: bool=False, + display_modified_value: bool=True, formatter_class=HelpFormatter, _forhelp: bool=False, **kwargs): @@ -230,6 +231,7 @@ class TiramisuCmdlineParser(ArgumentParser): self.config = config self.root = root self.remove_empty_od = remove_empty_od + self.display_modified_value = display_modified_value if TiramisuHelpFormatter not in formatter_class.__mro__: formatter_class = type('TiramisuHelpFormatter', (TiramisuHelpFormatter, formatter_class), {}) formatter_class.remove_empty_od = self.remove_empty_od @@ -287,6 +289,7 @@ class TiramisuCmdlineParser(ArgumentParser): self.prog, root=self.root, remove_empty_od=self.remove_empty_od, + display_modified_value=self.display_modified_value, formatter_class=self.formatter_class, epilog=self.epilog, description=self.description, @@ -407,6 +410,8 @@ class TiramisuCmdlineParser(ArgumentParser): 'type': option.type(), 'name': name, 'value': value} + if not self.display_modified_value: + continue if 'positional' in properties: if option.type() == 'boolean': raise ValueError(_('boolean option must not be positional')) @@ -548,6 +553,7 @@ class TiramisuCmdlineParser(ArgumentParser): root=self.root, fullpath=self.fullpath, remove_empty_od=self.remove_empty_od, + display_modified_value=self.display_modified_value, formatter_class=self.formatter_class, epilog=self.epilog, description=self.description, @@ -560,6 +566,7 @@ class TiramisuCmdlineParser(ArgumentParser): root=self.root, fullpath=self.fullpath, remove_empty_od=self.remove_empty_od, + display_modified_value=self.display_modified_value, formatter_class=self.formatter_class, epilog=self.epilog, description=self.description,