Skip to content

Commit

Permalink
fix: exposing func arg in CLI under a different name (fixes #224)
Browse files Browse the repository at this point in the history
  • Loading branch information
neithere committed Jul 13, 2024
1 parent 7f7d6f4 commit 9fd19ce
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ Version 0.31.3

Bugs fixed:

- fix type annotation of `errors` in `wrap_errors` (PR #229 by @laazy)
- test failures under Python 3.13 (issue #228 by @mgorny).
- wrong type annotation of `errors` in `wrap_errors` (PR #229 by @laazy)
- tests were failing under Python 3.13 (issue #228 by @mgorny)
- regression: can't set argument name with `dest` via decorator
(issue #224 by @mathieulongtin)

Version 0.31.2 (2024-01-24)
---------------------------
Expand Down
6 changes: 5 additions & 1 deletion src/argh/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,11 @@ def wrapper(func: Callable) -> Callable:
if not args:
raise CliArgToFuncArgGuessingError("at least one CLI arg must be defined")

func_arg_name = naive_guess_func_arg_name(args)
if "dest" in kwargs:
func_arg_name = kwargs.pop("dest")
else:
func_arg_name = naive_guess_func_arg_name(args)

cli_arg_names = [name.replace("_", "-") for name in args]
completer = kwargs.pop("completer", None)
spec = ParserAddArgumentSpec.make_from_kwargs(
Expand Down
19 changes: 19 additions & 0 deletions tests/test_regressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,22 @@ def func(*, paths: Optional[List[str]] = ["one", "two"]):
assert run(parser, "").out == "['one', 'two']\n"
assert run(parser, "--paths alpha").out == "['alpha']\n"
assert run(parser, "--paths alpha beta gamma").out == "['alpha', 'beta', 'gamma']\n"


def test_regression_issue224():
"""
Issue #224: @arg param `dest` was ignored and Argh was unable to map the
declaration onto the function signature.
Use case: expose a function argument with a different name in the CLI.
"""

@argh.arg("-l", dest="list_files")
def func(*, list_files=False):
return f"list_files={list_files}"

parser = DebugArghParser()
parser.set_default_command(func)

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

0 comments on commit 9fd19ce

Please sign in to comment.