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

feat: Show subcommand's help info when passing unrecognized arguments #2480

Merged
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
1 change: 1 addition & 0 deletions news/2480.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Show subcommand's help info when passing unrecognized arguments.
8 changes: 8 additions & 0 deletions src/pdm/cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions tests/cli/test_utils.py
Original file line number Diff line number Diff line change
@@ -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