do not use RawDescriptionHelpFormatter by default

This commit is contained in:
Emmanuel Garette 2019-07-26 21:35:22 +02:00 committed by Emmanuel Garette
parent 549151b3b8
commit 452e1fb0e0
2 changed files with 36 additions and 8 deletions

View file

@ -1,6 +1,7 @@
from io import StringIO from io import StringIO
from contextlib import redirect_stdout, redirect_stderr from contextlib import redirect_stdout, redirect_stderr
import pytest import pytest
from argparse import RawDescriptionHelpFormatter
from tiramisu_cmdline_parser import TiramisuCmdlineParser from tiramisu_cmdline_parser import TiramisuCmdlineParser
@ -64,11 +65,31 @@ od:
{str,list,int,none} choice the sub argument {str,list,int,none} choice the sub argument
two two line
line
""" """
parser = TiramisuCmdlineParser(get_config(json), 'prog.py', epilog="\ntwo\nline") parser = TiramisuCmdlineParser(get_config(json), 'prog.py', epilog="\ntwo\nline")
f = StringIO() f = StringIO()
with redirect_stdout(f): with redirect_stdout(f):
parser.print_help() parser.print_help()
assert f.getvalue() == output assert f.getvalue() == output
def test_help_epilog_raw(json):
output = """usage: prog.py [-h] {str,list,int,none}
optional arguments:
-h, --help show this help message and exit
od:
od
{str,list,int,none} choice the sub argument
two
line
"""
parser = TiramisuCmdlineParser(get_config(json), 'prog.py', epilog="\ntwo\nline", formatter_class=RawDescriptionHelpFormatter)
f = StringIO()
with redirect_stdout(f):
parser.print_help()
assert f.getvalue() == output

View file

@ -13,7 +13,7 @@
# You should have received a copy of the GNU Lesser General Public License # You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
from typing import Union, List, Dict, Tuple, Optional, Any from typing import Union, List, Dict, Tuple, Optional, Any
from argparse import ArgumentParser, Namespace, SUPPRESS, _HelpAction, RawDescriptionHelpFormatter from argparse import ArgumentParser, Namespace, SUPPRESS, _HelpAction, HelpFormatter
from copy import copy from copy import copy
from gettext import gettext as _ from gettext import gettext as _
@ -106,7 +106,7 @@ class TiramisuNamespace(Namespace):
self._config.option(true_key, index).value.set(value) self._config.option(true_key, index).value.set(value)
class TiramisuHelpFormatter(RawDescriptionHelpFormatter): class TiramisuHelpFormatter:
def _get_default_metavar_for_optional(self, def _get_default_metavar_for_optional(self,
action): action):
ret = super()._get_default_metavar_for_optional(action) ret = super()._get_default_metavar_for_optional(action)
@ -114,7 +114,7 @@ class TiramisuHelpFormatter(RawDescriptionHelpFormatter):
ret = ret.rsplit('.', 1)[1] ret = ret.rsplit('.', 1)[1]
return ret return ret
class _Section(RawDescriptionHelpFormatter._Section): class _Section(HelpFormatter._Section):
def format_help(self): def format_help(self):
# Remove empty OD # Remove empty OD
if self.formatter.remove_empty_od and \ if self.formatter.remove_empty_od and \
@ -135,6 +135,7 @@ class TiramisuHelpFormatter(RawDescriptionHelpFormatter):
return '' return ''
return super().format_help() return super().format_help()
class _TiramisuHelpAction(_HelpAction): class _TiramisuHelpAction(_HelpAction):
needs = False needs = False
def __call__(self, *args, **kwargs): def __call__(self, *args, **kwargs):
@ -208,6 +209,7 @@ class TiramisuCmdlineParser(ArgumentParser):
fullpath: bool=True, fullpath: bool=True,
remove_empty_od: bool=False, remove_empty_od: bool=False,
remove_empty_description_od: bool=False, remove_empty_description_od: bool=False,
formatter_class=HelpFormatter,
_forhelp: bool=False, _forhelp: bool=False,
**kwargs): **kwargs):
self.fullpath = fullpath self.fullpath = fullpath
@ -215,9 +217,11 @@ class TiramisuCmdlineParser(ArgumentParser):
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 self.remove_empty_description_od = remove_empty_description_od
TiramisuHelpFormatter.remove_empty_od = self.remove_empty_od if TiramisuHelpFormatter not in formatter_class.__mro__:
TiramisuHelpFormatter.remove_empty_description_od = self.remove_empty_description_od formatter_class = type('TiramisuHelpFormatter', (TiramisuHelpFormatter, formatter_class), {})
kwargs['formatter_class'] = TiramisuHelpFormatter 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
if self.root is None: if self.root is None:
subconfig = self.config.option subconfig = self.config.option
else: else:
@ -272,6 +276,7 @@ class TiramisuCmdlineParser(ArgumentParser):
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, remove_empty_description_od=self.remove_empty_description_od,
formatter_class=self.formatter_class,
epilog=self.epilog, epilog=self.epilog,
fullpath=self.fullpath) fullpath=self.fullpath)
namespace_, args_ = new_parser._parse_known_args(args_, namespace) namespace_, args_ = new_parser._parse_known_args(args_, namespace)
@ -510,6 +515,7 @@ class TiramisuCmdlineParser(ArgumentParser):
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, remove_empty_description_od=self.remove_empty_description_od,
formatter_class=self.formatter_class,
epilog=self.epilog, epilog=self.epilog,
_forhelp=True) _forhelp=True)
return super(TiramisuCmdlineParser, help_formatter).format_usage(*args, **kwargs) return super(TiramisuCmdlineParser, help_formatter).format_usage(*args, **kwargs)
@ -521,6 +527,7 @@ class TiramisuCmdlineParser(ArgumentParser):
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, remove_empty_description_od=self.remove_empty_description_od,
formatter_class=self.formatter_class,
epilog=self.epilog, epilog=self.epilog,
_forhelp=True) _forhelp=True)
return super(TiramisuCmdlineParser, help_formatter).format_help() return super(TiramisuCmdlineParser, help_formatter).format_help()