From 067831bddde47a5fc46b8f03659ca94acbca1afb Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Fri, 26 Jul 2019 15:17:36 +0200 Subject: [PATCH] remove empty OD in help if option remove_empty_od set to True --- test/test_optiondescription.py | 35 ++++++++++++++++++++++++++++++++++ test/test_shortarg.py | 4 ++++ tiramisu_cmdline_parser/api.py | 15 ++++++++++++++- 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/test/test_optiondescription.py b/test/test_optiondescription.py index dbc481d..7732a9a 100644 --- a/test/test_optiondescription.py +++ b/test/test_optiondescription.py @@ -98,6 +98,41 @@ od2.subtree: assert f.getvalue() == output +def test_optiondescription_help_remove_empty_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.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: + Second OptionDescription + + --od2.before BEFORE Before + --od2.after AFTER After + +od2.subtree: + Sub-Tree 2 + + --od2.subtree.str STR + string option 2 +""" + parser = TiramisuCmdlineParser(get_config(json), 'prog.py', remove_empty_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/test/test_shortarg.py b/test/test_shortarg.py index 98ebdb2..e68278c 100644 --- a/test/test_shortarg.py +++ b/test/test_shortarg.py @@ -47,6 +47,10 @@ def test_short(json): parser = TiramisuCmdlineParser(config, 'prog.py') parser.parse_args(['-l', 'a']) assert config.value.dict() == output + # + assert config.option('list').value.get() == config.option('l').value.get() + assert config.option('list').owner.get() == config.option('l').owner.get() + assert config.option('list').owner.isdefault() == config.option('l').owner.isdefault() def test_short_mandatory(json): diff --git a/tiramisu_cmdline_parser/api.py b/tiramisu_cmdline_parser/api.py index ed8e5ed..84a1dc5 100644 --- a/tiramisu_cmdline_parser/api.py +++ b/tiramisu_cmdline_parser/api.py @@ -17,7 +17,6 @@ from argparse import ArgumentParser, Namespace, SUPPRESS, _HelpAction, HelpForma from copy import copy from gettext import gettext as _ - try: from tiramisu import Config from tiramisu.error import PropertiesOptionError, RequirementError @@ -115,6 +114,14 @@ class TiramisuHelpFormatter(HelpFormatter): ret = ret.rsplit('.', 1)[1] return ret + class _Section(HelpFormatter._Section): + def format_help(self): + # Remove empty OD + if self.formatter.remove_empty_od and \ + len(self.items) == 1 and \ + self.items[0][0].__name__ == '_format_text': + return '' + return super().format_help() class _TiramisuHelpAction(_HelpAction): needs = False @@ -187,11 +194,14 @@ class TiramisuCmdlineParser(ArgumentParser): *args, root: str=None, fullpath: bool=True, + remove_empty_od: bool=False, _forhelp: bool=False, **kwargs): self.fullpath = fullpath self.config = config self.root = root + self.remove_empty_od = remove_empty_od + TiramisuHelpFormatter.remove_empty_od = self.remove_empty_od kwargs['formatter_class'] = TiramisuHelpFormatter if self.root is None: subconfig = self.config.option @@ -245,6 +255,7 @@ class TiramisuCmdlineParser(ArgumentParser): new_parser = TiramisuCmdlineParser(self.config, self.prog, root=self.root, + remove_empty_od=self.remove_empty_od, fullpath=self.fullpath) namespace_, args_ = new_parser._parse_known_args(args_, namespace) else: @@ -480,6 +491,7 @@ class TiramisuCmdlineParser(ArgumentParser): self.prog, root=self.root, fullpath=self.fullpath, + remove_empty_od=self.remove_empty_od, _forhelp=True) return super(TiramisuCmdlineParser, help_formatter).format_usage(*args, **kwargs) @@ -488,6 +500,7 @@ class TiramisuCmdlineParser(ArgumentParser): self.prog, root=self.root, fullpath=self.fullpath, + remove_empty_od=self.remove_empty_od, _forhelp=True) return super(TiramisuCmdlineParser, help_formatter).format_help()