From e840695d5de5a864c39065dc378da6ba6f12786b Mon Sep 17 00:00:00 2001 From: Jun Huang Date: Fri, 8 Dec 2023 13:23:09 +0800 Subject: [PATCH 1/2] feat: Show subcommand's help info when passing unrecognized arguments --- src/pdm/cli/utils.py | 8 ++++++++ tests/cli/test_utils.py | 4 ++++ 2 files changed, 12 insertions(+) create mode 100644 tests/cli/test_utils.py diff --git a/src/pdm/cli/utils.py b/src/pdm/cli/utils.py index c498104c61..81817e7778 100644 --- a/src/pdm/cli/utils.py +++ b/src/pdm/cli/utils.py @@ -9,6 +9,7 @@ from collections import ChainMap, OrderedDict from concurrent.futures import ThreadPoolExecutor from fnmatch import fnmatch +from gettext import gettext as _ from json import dumps from pathlib import Path from typing import ( @@ -152,6 +153,13 @@ def __init__(self, *args: Any, **kwargs: Any) -> None: ) self._optionals.title = "options" + def parse_known_args(self, args: Any = None, namespace: Any = None) -> Any: + args, argv = super().parse_known_args(args, namespace) + if argv: + msg = _("unrecognized arguments: %s") + self.error(msg % " ".join(argv)) + return args, argv + class ErrorArgumentParser(ArgumentParser): """A subclass of argparse.ArgumentParser that raises diff --git a/tests/cli/test_utils.py b/tests/cli/test_utils.py new file mode 100644 index 0000000000..d3993efc93 --- /dev/null +++ b/tests/cli/test_utils.py @@ -0,0 +1,4 @@ +def test_help_with_unknown_arguments(pdm): + result = pdm(["add", "--unknown-args"]) + assert "Usage: pdm add " in result.stderr + assert result.exit_code == 2 From 2f93472ddde41b4c2144f7b38e59ef03afd22378 Mon Sep 17 00:00:00 2001 From: Jun Huang Date: Fri, 8 Dec 2023 13:46:56 +0800 Subject: [PATCH 2/2] add news --- news/2480.feature.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 news/2480.feature.md diff --git a/news/2480.feature.md b/news/2480.feature.md new file mode 100644 index 0000000000..9c259573b4 --- /dev/null +++ b/news/2480.feature.md @@ -0,0 +1 @@ +Show subcommand's help info when passing unrecognized arguments.