From 96b2e83eab4c10e73a9d06a1f82d1b97803a13df Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Sat, 25 Jun 2016 23:39:06 -0700 Subject: [PATCH] Hide private args; convert "_"->"-" in *args name. --- argh/assembling.py | 11 ++++++++--- test/test_assembling.py | 2 +- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/argh/assembling.py b/argh/assembling.py index afe7ba7..e722ff0 100644 --- a/argh/assembling.py +++ b/argh/assembling.py @@ -77,14 +77,16 @@ def _get_args_from_signature(function): annotations = dict((k,v) for k,v in function.__annotations__.items() if isinstance(v, str)) + cmdline_args = [a for a in spec.args + kwonly if not a.startswith("_")] + # define the list of conflicting option strings # (short forms, i.e. single-character ones) - chars = [a[0] for a in spec.args + kwonly] + chars = [a[0] for a in cmdline_args if a in list(defaults) + kwonly] char_counts = dict((char, chars.count(char)) for char in set(chars)) conflicting_opts = tuple(char for char in char_counts if 1 < char_counts[char]) - for name in spec.args + kwonly: + for name in cmdline_args: flags = [] # name_or_flags akwargs = {} # keyword arguments for add_argument() @@ -113,7 +115,10 @@ def _get_args_from_signature(function): if spec.varargs: # *args - yield dict(option_strings=[spec.varargs], nargs='*') + to_yield = dict(option_strings=[spec.varargs], nargs='*') + if '_' in spec.varargs: + to_yield["metavar"] = spec.varargs.replace('_', '-') + yield to_yield def _guess(kwargs): diff --git a/test/test_assembling.py b/test/test_assembling.py index bfd6d14..2e6c663 100644 --- a/test/test_assembling.py +++ b/test/test_assembling.py @@ -181,7 +181,7 @@ def func(*file_paths): argh.set_default_command(parser, func) assert parser.add_argument.mock_calls == [ - mock.call('file_paths', nargs='*', + mock.call('file_paths', nargs='*', metavar='file-paths', help=argh.constants.DEFAULT_ARGUMENT_TEMPLATE), ]