From 1ade6c883f97013253d41b72433f2242436dc978 Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Fri, 26 Jul 2019 15:40:54 +0200 Subject: [PATCH] remove OD with no description in help if option remove_empty_description_od set to True --- test/test_optiondescription.py | 38 +++++++++++++++++++++++++++++++--- tiramisu_cmdline_parser/api.py | 12 +++++++++++ 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/test/test_optiondescription.py b/test/test_optiondescription.py index 7732a9a..908efa3 100644 --- a/test/test_optiondescription.py +++ b/test/test_optiondescription.py @@ -42,7 +42,7 @@ def get_config(json, has_tree=False, default_verbosity=False, add_long=False, ad 'Sub-Tree 2', [str_]) od2 = OptionDescription('od2', - 'Second OptionDescription', + '', [before, subtree, after]) root = OptionDescription('root', 'root', @@ -80,7 +80,7 @@ od1.od0: -nv, --od1.od0.no-verbosity od2: - Second OptionDescription + od2 --od2.before BEFORE Before --od2.after AFTER After @@ -115,7 +115,7 @@ od1.od0: -nv, --od1.od0.no-verbosity od2: - Second OptionDescription + od2 --od2.before BEFORE Before --od2.after AFTER After @@ -133,6 +133,38 @@ od2.subtree: assert f.getvalue() == output +def test_optiondescription_help_remove_empty_description_od(json): + output = """usage: prog.py [-h] [-v] [-nv] --od2.subtree.str STR --od2.before BEFORE + --od2.after AFTER + {str,list,int,none} + +optional arguments: + -h, --help show this help message and exit + +od1: + First OptionDescription + +od1.od0: + Sub-Tree 1 + + {str,list,int,none} choice the sub argument + -v, --od1.od0.verbosity + increase output verbosity + -nv, --od1.od0.no-verbosity + +od2.subtree: + Sub-Tree 2 + + --od2.subtree.str STR + string option 2 +""" + parser = TiramisuCmdlineParser(get_config(json), 'prog.py', remove_empty_description_od=True) + f = StringIO() + with redirect_stdout(f): + parser.print_help() + assert f.getvalue() == output + + def test_optiondescription_help_subtree(json): output = """usage: prog.py [-h] --od2.subtree.str STR --od2.before BEFORE --od2.after AFTER diff --git a/tiramisu_cmdline_parser/api.py b/tiramisu_cmdline_parser/api.py index 84a1dc5..179e1fc 100644 --- a/tiramisu_cmdline_parser/api.py +++ b/tiramisu_cmdline_parser/api.py @@ -121,6 +121,12 @@ class TiramisuHelpFormatter(HelpFormatter): len(self.items) == 1 and \ self.items[0][0].__name__ == '_format_text': return '' + # Remove OD if name == description + if self.items and \ + self.formatter.remove_empty_description_od and \ + self.items[0][0].__name__ == '_format_text' and \ + self.items[0][1][0] == self.heading: + return '' return super().format_help() class _TiramisuHelpAction(_HelpAction): @@ -195,13 +201,16 @@ class TiramisuCmdlineParser(ArgumentParser): root: str=None, fullpath: bool=True, remove_empty_od: bool=False, + remove_empty_description_od: bool=False, _forhelp: bool=False, **kwargs): self.fullpath = fullpath self.config = config self.root = root self.remove_empty_od = remove_empty_od + self.remove_empty_description_od = remove_empty_description_od TiramisuHelpFormatter.remove_empty_od = self.remove_empty_od + TiramisuHelpFormatter.remove_empty_description_od = self.remove_empty_description_od kwargs['formatter_class'] = TiramisuHelpFormatter if self.root is None: subconfig = self.config.option @@ -256,6 +265,7 @@ class TiramisuCmdlineParser(ArgumentParser): self.prog, root=self.root, remove_empty_od=self.remove_empty_od, + remove_empty_description_od=self.remove_empty_description_od, fullpath=self.fullpath) namespace_, args_ = new_parser._parse_known_args(args_, namespace) else: @@ -492,6 +502,7 @@ class TiramisuCmdlineParser(ArgumentParser): root=self.root, fullpath=self.fullpath, remove_empty_od=self.remove_empty_od, + remove_empty_description_od=self.remove_empty_description_od, _forhelp=True) return super(TiramisuCmdlineParser, help_formatter).format_usage(*args, **kwargs) @@ -501,6 +512,7 @@ class TiramisuCmdlineParser(ArgumentParser): root=self.root, fullpath=self.fullpath, remove_empty_od=self.remove_empty_od, + remove_empty_description_od=self.remove_empty_description_od, _forhelp=True) return super(TiramisuCmdlineParser, help_formatter).format_help()