Skip to content

Commit

Permalink
improve help message
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengyao-lin committed Sep 22, 2019
1 parent 3da86b6 commit f57e2cf
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
29 changes: 13 additions & 16 deletions flagset/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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: <help>[. default value '<default>'][. env var '<env>']
kwargs["help"] = self.help if self.help is not None else ""
# help messages: <sentence>[. <sentence> ...]
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|...]
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())

0 comments on commit f57e2cf

Please sign in to comment.