Skip to content

Commit

Permalink
Resolve conflict between inferred and declared CLI options
Browse files Browse the repository at this point in the history
- Filter out inferred CLI options that conflict with declared ones
- Regression test for this issue
- Fixes issue neithere#233
  • Loading branch information
sswam committed Sep 19, 2024
1 parent 8d25286 commit 060ea47
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
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"

0 comments on commit 060ea47

Please sign in to comment.