remove OD if name == description is now managed with internal argparser mechanism

This commit is contained in:
Emmanuel Garette 2019-07-28 22:44:17 +02:00 committed by Emmanuel Garette
parent 528d16ec88
commit 88949d8682
2 changed files with 14 additions and 19 deletions

View file

@ -9,7 +9,7 @@ from tiramisu import IntOption, StrOption, BoolOption, ChoiceOption, \
from tiramisu_api import Config as JsonConfig from tiramisu_api import Config as JsonConfig
def get_config(json, has_tree=False, default_verbosity=False, add_long=False, add_store_false=False): def get_config(json, has_tree=False, default_verbosity=False, add_long=False, add_store_false=False, empty_optiondescription=False):
choiceoption = ChoiceOption('cmd', choiceoption = ChoiceOption('cmd',
'choice the sub argument', 'choice the sub argument',
('str', 'list', 'int', 'none'), ('str', 'list', 'int', 'none'),
@ -26,8 +26,12 @@ def get_config(json, has_tree=False, default_verbosity=False, add_long=False, ad
booloption, booloption,
short_booloption, short_booloption,
]) ])
if empty_optiondescription:
descr = None
else:
descr = 'First OptionDescription'
od1 = OptionDescription('od1', od1 = OptionDescription('od1',
'First OptionDescription', descr,
[od0]) [od0])
before = StrOption('before', before = StrOption('before',
'Before', 'Before',
@ -137,9 +141,6 @@ def test_optiondescription_help_remove_empty_description_od(json):
optional arguments: optional arguments:
-h, --help show this help message and exit -h, --help show this help message and exit
od1:
First OptionDescription
od1.od0: od1.od0:
Sub-Tree 1 Sub-Tree 1
@ -148,13 +149,17 @@ od1.od0:
increase output verbosity increase output verbosity
-nv, --od1.od0.no-verbosity -nv, --od1.od0.no-verbosity
od2:
--od2.before BEFORE Before
--od2.after AFTER After
od2.subtree: od2.subtree:
Sub-Tree 2 Sub-Tree 2
--od2.subtree.str STR --od2.subtree.str STR
string option 2 string option 2
""" """
parser = TiramisuCmdlineParser(get_config(json), 'prog.py', remove_empty_description_od=True) parser = TiramisuCmdlineParser(get_config(json, empty_optiondescription=True), 'prog.py')
f = StringIO() f = StringIO()
with redirect_stdout(f): with redirect_stdout(f):
parser.print_help() parser.print_help()

View file

@ -128,13 +128,6 @@ class TiramisuHelpFormatter:
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.formatter.remove_empty_description_od and \
self.items is not None and \
self.heading is not None and \
len(self.items) > 1 and \
self.items[0][0].__name__ != '_format_text':
return ''
return super().format_help() return super().format_help()
@ -227,7 +220,6 @@ 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,
formatter_class=HelpFormatter, formatter_class=HelpFormatter,
_forhelp: bool=False, _forhelp: bool=False,
**kwargs): **kwargs):
@ -235,11 +227,9 @@ class TiramisuCmdlineParser(ArgumentParser):
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
if TiramisuHelpFormatter not in formatter_class.__mro__: if TiramisuHelpFormatter not in formatter_class.__mro__:
formatter_class = type('TiramisuHelpFormatter', (TiramisuHelpFormatter, formatter_class), {}) formatter_class = type('TiramisuHelpFormatter', (TiramisuHelpFormatter, formatter_class), {})
formatter_class.remove_empty_od = self.remove_empty_od formatter_class.remove_empty_od = self.remove_empty_od
formatter_class.remove_empty_description_od = self.remove_empty_description_od
kwargs['formatter_class'] = formatter_class kwargs['formatter_class'] = formatter_class
if self.root is None: if self.root is None:
subconfig = self.config.option subconfig = self.config.option
@ -294,9 +284,9 @@ 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,
formatter_class=self.formatter_class, formatter_class=self.formatter_class,
epilog=self.epilog, epilog=self.epilog,
description=self.description,
fullpath=self.fullpath) fullpath=self.fullpath)
namespace_, args_ = new_parser._parse_known_args(args_, new_parser.namespace) namespace_, args_ = new_parser._parse_known_args(args_, new_parser.namespace)
else: else:
@ -555,9 +545,9 @@ 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,
formatter_class=self.formatter_class, formatter_class=self.formatter_class,
epilog=self.epilog, epilog=self.epilog,
description=self.description,
_forhelp=True) _forhelp=True)
return super(TiramisuCmdlineParser, help_formatter).format_usage(*args, **kwargs) return super(TiramisuCmdlineParser, help_formatter).format_usage(*args, **kwargs)
@ -567,9 +557,9 @@ 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,
formatter_class=self.formatter_class, formatter_class=self.formatter_class,
epilog=self.epilog, epilog=self.epilog,
description=self.description,
_forhelp=True) _forhelp=True)
return super(TiramisuCmdlineParser, help_formatter).format_help() return super(TiramisuCmdlineParser, help_formatter).format_help()