diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 77c9fcd..63daf4b 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -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) --------------------------- diff --git a/src/argh/decorators.py b/src/argh/decorators.py index ff0d2be..0de1c98 100644 --- a/src/argh/decorators.py +++ b/src/argh/decorators.py @@ -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( diff --git a/tests/test_regressions.py b/tests/test_regressions.py index 343da81..2d5b7fd 100644 --- a/tests/test_regressions.py +++ b/tests/test_regressions.py @@ -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"