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

fix: exposing func arg in CLI under a different name (fixes #224) #230

Merged
merged 1 commit into from
Jul 13, 2024
Merged
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
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"
Loading