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. 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