From d8ee3486e9f5047b30891d1d14484e4e5a9e237d Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Fri, 26 Jul 2019 16:39:01 +0200 Subject: [PATCH] use rawformatter and add epilog --- test/test_help.py | 74 ++++++++++++++++++++++++++++++++++ tiramisu_cmdline_parser/api.py | 9 +++-- 2 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 test/test_help.py diff --git a/test/test_help.py b/test/test_help.py new file mode 100644 index 0000000..3e2226d --- /dev/null +++ b/test/test_help.py @@ -0,0 +1,74 @@ +from io import StringIO +from contextlib import redirect_stdout, redirect_stderr +import pytest + + +from tiramisu_cmdline_parser import TiramisuCmdlineParser +from tiramisu import IntOption, StrOption, BoolOption, ChoiceOption, \ + SymLinkOption, OptionDescription, Config +from tiramisu_json_api import Config as JsonConfig + + + +def get_config(json): + choiceoption = ChoiceOption('cmd', + 'choice the sub argument', + ('str', 'list', 'int', 'none'), + properties=('mandatory', + 'positional')) + od = OptionDescription('od', + 'od', + [choiceoption]) + root = OptionDescription('root', + 'root', + [od]) + config = Config(root) + config.property.read_write() + if json == 'tiramisu': + return config + jconfig = JsonConfig(config.option.dict()) + return jconfig + + +@pytest.fixture(params=['tiramisu', 'tiramisu-json']) +def json(request): + return request.param + + +def test_help(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 +""" + parser = TiramisuCmdlineParser(get_config(json), 'prog.py') + f = StringIO() + with redirect_stdout(f): + parser.print_help() + assert f.getvalue() == output + + +def test_help_epilog(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") + f = StringIO() + with redirect_stdout(f): + parser.print_help() + assert f.getvalue() == output diff --git a/tiramisu_cmdline_parser/api.py b/tiramisu_cmdline_parser/api.py index 1f3571d..04736cb 100644 --- a/tiramisu_cmdline_parser/api.py +++ b/tiramisu_cmdline_parser/api.py @@ -13,7 +13,7 @@ # You should have received a copy of the GNU Lesser General Public License # along with this program. If not, see . from typing import Union, List, Dict, Tuple, Optional, Any -from argparse import ArgumentParser, Namespace, SUPPRESS, _HelpAction, HelpFormatter +from argparse import ArgumentParser, Namespace, SUPPRESS, _HelpAction, RawDescriptionHelpFormatter from copy import copy from gettext import gettext as _ @@ -106,7 +106,7 @@ class TiramisuNamespace(Namespace): self._config.option(true_key, index).value.set(value) -class TiramisuHelpFormatter(HelpFormatter): +class TiramisuHelpFormatter(RawDescriptionHelpFormatter): def _get_default_metavar_for_optional(self, action): ret = super()._get_default_metavar_for_optional(action) @@ -114,7 +114,7 @@ class TiramisuHelpFormatter(HelpFormatter): ret = ret.rsplit('.', 1)[1] return ret - class _Section(HelpFormatter._Section): + class _Section(RawDescriptionHelpFormatter._Section): def format_help(self): # Remove empty OD if self.formatter.remove_empty_od and \ @@ -272,6 +272,7 @@ class TiramisuCmdlineParser(ArgumentParser): root=self.root, remove_empty_od=self.remove_empty_od, remove_empty_description_od=self.remove_empty_description_od, + epilog=self.epilog, fullpath=self.fullpath) namespace_, args_ = new_parser._parse_known_args(args_, namespace) else: @@ -509,6 +510,7 @@ class TiramisuCmdlineParser(ArgumentParser): fullpath=self.fullpath, remove_empty_od=self.remove_empty_od, remove_empty_description_od=self.remove_empty_description_od, + epilog=self.epilog, _forhelp=True) return super(TiramisuCmdlineParser, help_formatter).format_usage(*args, **kwargs) @@ -519,6 +521,7 @@ class TiramisuCmdlineParser(ArgumentParser): fullpath=self.fullpath, remove_empty_od=self.remove_empty_od, remove_empty_description_od=self.remove_empty_description_od, + epilog=self.epilog, _forhelp=True) return super(TiramisuCmdlineParser, help_formatter).format_help()