remove OD with no description in help if option remove_empty_description_od set to True

This commit is contained in:
Emmanuel Garette 2019-07-26 15:40:54 +02:00
parent 3d0ac1fb19
commit 62c0812863
2 changed files with 47 additions and 3 deletions

View File

@ -42,7 +42,7 @@ def get_config(json, has_tree=False, default_verbosity=False, add_long=False, ad
'Sub-Tree 2', 'Sub-Tree 2',
[str_]) [str_])
od2 = OptionDescription('od2', od2 = OptionDescription('od2',
'Second OptionDescription', '',
[before, subtree, after]) [before, subtree, after])
root = OptionDescription('root', root = OptionDescription('root',
'root', 'root',
@ -80,7 +80,7 @@ od1.od0:
-nv, --od1.od0.no-verbosity -nv, --od1.od0.no-verbosity
od2: od2:
Second OptionDescription od2
--od2.before BEFORE Before --od2.before BEFORE Before
--od2.after AFTER After --od2.after AFTER After
@ -115,7 +115,7 @@ od1.od0:
-nv, --od1.od0.no-verbosity -nv, --od1.od0.no-verbosity
od2: od2:
Second OptionDescription od2
--od2.before BEFORE Before --od2.before BEFORE Before
--od2.after AFTER After --od2.after AFTER After
@ -133,6 +133,38 @@ od2.subtree:
assert f.getvalue() == output 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): def test_optiondescription_help_subtree(json):
output = """usage: prog.py [-h] --od2.subtree.str STR --od2.before BEFORE --od2.after output = """usage: prog.py [-h] --od2.subtree.str STR --od2.before BEFORE --od2.after
AFTER AFTER

View File

@ -121,6 +121,12 @@ class TiramisuHelpFormatter(HelpFormatter):
len(self.items) == 1 and \ len(self.items) == 1 and \
self.items[0][0].__name__ == '_format_text': self.items[0][0].__name__ == '_format_text':
return '' 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() return super().format_help()
class _TiramisuHelpAction(_HelpAction): class _TiramisuHelpAction(_HelpAction):
@ -195,13 +201,16 @@ class TiramisuCmdlineParser(ArgumentParser):
root: str=None, root: str=None,
fullpath: bool=True, fullpath: bool=True,
remove_empty_od: bool=False, remove_empty_od: bool=False,
remove_empty_description_od: bool=False,
_forhelp: bool=False, _forhelp: bool=False,
**kwargs): **kwargs):
self.fullpath = fullpath self.fullpath = fullpath
self.config = config self.config = config
self.root = root self.root = root
self.remove_empty_od = remove_empty_od 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_od = self.remove_empty_od
TiramisuHelpFormatter.remove_empty_description_od = self.remove_empty_description_od
kwargs['formatter_class'] = TiramisuHelpFormatter kwargs['formatter_class'] = TiramisuHelpFormatter
if self.root is None: if self.root is None:
subconfig = self.config.option subconfig = self.config.option
@ -256,6 +265,7 @@ class TiramisuCmdlineParser(ArgumentParser):
self.prog, self.prog,
root=self.root, root=self.root,
remove_empty_od=self.remove_empty_od, remove_empty_od=self.remove_empty_od,
remove_empty_description_od=self.remove_empty_description_od,
fullpath=self.fullpath) fullpath=self.fullpath)
namespace_, args_ = new_parser._parse_known_args(args_, namespace) namespace_, args_ = new_parser._parse_known_args(args_, namespace)
else: else:
@ -492,6 +502,7 @@ class TiramisuCmdlineParser(ArgumentParser):
root=self.root, root=self.root,
fullpath=self.fullpath, fullpath=self.fullpath,
remove_empty_od=self.remove_empty_od, remove_empty_od=self.remove_empty_od,
remove_empty_description_od=self.remove_empty_description_od,
_forhelp=True) _forhelp=True)
return super(TiramisuCmdlineParser, help_formatter).format_usage(*args, **kwargs) return super(TiramisuCmdlineParser, help_formatter).format_usage(*args, **kwargs)
@ -501,6 +512,7 @@ class TiramisuCmdlineParser(ArgumentParser):
root=self.root, root=self.root,
fullpath=self.fullpath, fullpath=self.fullpath,
remove_empty_od=self.remove_empty_od, remove_empty_od=self.remove_empty_od,
remove_empty_description_od=self.remove_empty_description_od,
_forhelp=True) _forhelp=True)
return super(TiramisuCmdlineParser, help_formatter).format_help() return super(TiramisuCmdlineParser, help_formatter).format_help()