remove empty OD in help if option remove_empty_od set to True

This commit is contained in:
Emmanuel Garette 2019-07-26 15:17:36 +02:00
parent e14b997a54
commit 3d0ac1fb19
3 changed files with 53 additions and 1 deletions

View File

@ -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

View File

@ -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):

View File

@ -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()