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

Resolve conflict between inferred and declared CLI options #234

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
10 changes: 10 additions & 0 deletions src/argh/assembling.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,16 @@ def _merge_inferred_and_declared_args(
#
specs_by_func_arg_name = OrderedDict()

# eliminate inferred options that conflict with declared ones
for inferred in inferred_args:
for declared in declared_args:
if inferred.func_arg_name == declared.func_arg_name:
continue
inferred.cli_arg_names = [
name for name in inferred.cli_arg_names
if name not in declared.cli_arg_names
]

# arguments inferred from function signature
for parser_add_argument_spec in inferred_args:
specs_by_func_arg_name[parser_add_argument_spec.func_arg_name] = (
Expand Down
19 changes: 19 additions & 0 deletions tests/test_regressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,22 @@ def func(*, list_files=False):

assert run(parser, "").out == "list_files=False\n"
assert run(parser, "-l").out == "list_files=True\n"


def test_regression_issue233():
"""
Issue #233: conflict when short option does not match first letter
of long option
Use case: assigning short names to options
"""

@argh.arg("-n", "--user-name")
@argh.arg("-N", "--note")
def func(*, user_name="world", note=""):
return f"hello, {user_name}! {note}"

parser = DebugArghParser()
parser.set_default_command(func)

assert run(parser, "-n Sam -N Cheers").out == "hello, Sam! Cheers\n"
Loading