Skip to content

Commit

Permalink
feat: Show subcommand's help info when passing unrecognized arguments (
Browse files Browse the repository at this point in the history
  • Loading branch information
leemars authored Dec 11, 2023
1 parent 1ab79a6 commit 42ff1e7
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
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

0 comments on commit 42ff1e7

Please sign in to comment.