From f57e2cf5913e734c983a2d205f2459d11bfd2ecc Mon Sep 17 00:00:00 2001 From: rod-lin Date: Sun, 22 Sep 2019 16:24:31 -0500 Subject: [PATCH] improve help message --- flagset/__init__.py | 29 +++++++++++++---------------- tests/test_basic.py | 2 ++ 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/flagset/__init__.py b/flagset/__init__.py index cfddba3..8287e8e 100644 --- a/flagset/__init__.py +++ b/flagset/__init__.py @@ -97,29 +97,25 @@ def _bind_argparser(self, parser, dest=None): if dest is not None and not self.positional: kwargs["dest"] = dest - # help message: [. default value ''][. env var ''] - kwargs["help"] = self.help if self.help is not None else "" + # help messages: [. ...] + help_msgs = [self.help] if self.help is not None else [] + alternatives = [] if self.default is not None: - default_msg = "{}default value '{}'".format( - ". " if kwargs["help"] != "" else "", self.default - ) - - kwargs["help"] += default_msg + help_msgs.append("default value '{}'".format(self.default)) if self.env_name is not None: - env_msg = "{}environment variable ${}".format( - ". " if kwargs["help"] != "" else "", self.env_name - ) - - kwargs["help"] += env_msg + alternatives.append("environment variable ${}".format(self.env_name)) if self.config_name is not None: - config_msg = "{}config variable '{}'".format( - ". " if kwargs["help"] != "" else "", self.config_name + alternatives.append("config variable '{}'".format(self.config_name)) + + if alternatives: + help_msgs.append( + "alternatively, you can use {}".format(", or ".join(alternatives)) ) - kwargs["help"] += config_msg + kwargs["help"] = ". ".join(help_msgs) # allowed usages of boolean flags: # 1. --flag [t|true|yes|...|f|false|no|...] @@ -164,11 +160,12 @@ class FlagSet: 5. optional help message """ - def __init__(self, init_set={}): + def __init__(self, init_set={}, version=None): """ :params config_parser: should take a string and return dict """ self.flags = init_set + self.version = version def add_flag(self, name, *args, **kwargs): assert name not in self.flags, "flag '{}' already exists".format(name) diff --git a/tests/test_basic.py b/tests/test_basic.py index 7e52caa..5e1fda5 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -80,3 +80,5 @@ def test_positional(self): # calling with -h flag with missing positional argument should not yield error output = io.StringIO() flags = fset.parse(args=["-h"], help_output_file=output, use_exc=True) + + print(output.getvalue())