Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hide private args; convert "_"->"-" in *args name. #106

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions argh/assembling.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion test/test_assembling.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens in the case without the metavar arg?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's always called with a metavar now, see line 120 of the patch to assembling.py.

help=argh.constants.DEFAULT_ARGUMENT_TEMPLATE),
]

Expand Down