Skip to content

Commit b75cdab

Browse files
authored
Merge pull request #5 from apple1417/master
type hint argument parser and add argument wrappers
2 parents 2dc7284 + f9c14d8 commit b75cdab

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

Readme.md

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ game specific things:
3131
- Changed the display version to be sourced from `mod_manager.display_version` in the unrealsdk
3232
config file, rather than an environment variable.
3333

34+
- Gave `@command` and `ArgParseCommand.add_argument` default type hinting for the args they forward.
35+
3436
### v1.5
3537
- Added a default `rlm` command, which is a helper to reload Python modules during development.
3638
- Deprecated the `auto_enable` arg in the `@hook` decorator, since it was misleading and in 99% of

command.py

+47-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
import shlex
77
import sys
88
from abc import ABC, abstractmethod
9-
from collections.abc import Callable
9+
from collections.abc import Callable, Iterable, Sequence
1010
from dataclasses import dataclass
11-
from typing import Any, overload
11+
from typing import Any, Protocol, overload
1212

1313
from unrealsdk import logging
1414
from unrealsdk.commands import NEXT_LINE, add_command, has_command, remove_command
@@ -75,6 +75,26 @@ def _handle_cmd(self, line: str, cmd_len: int) -> None:
7575
except SystemExit:
7676
pass
7777

78+
@overload
79+
def add_argument[T](
80+
self,
81+
*name_or_flags: str,
82+
action: str | type[argparse.Action] = ...,
83+
nargs: int | str | None = None,
84+
const: Any = ...,
85+
default: Any = ...,
86+
type: Callable[[str], Any] | argparse.FileType | str = ...,
87+
choices: Iterable[T] | None = ...,
88+
required: bool = ...,
89+
help: str | None = ...,
90+
metavar: str | tuple[str, ...] | None = ...,
91+
dest: str | None = ...,
92+
version: str = ...,
93+
**kwargs: Any,
94+
) -> argparse.Action: ...
95+
@overload
96+
def add_argument(self, *args: Any, **kwargs: Any) -> argparse.Action: ...
97+
7898
def add_argument(self, *args: Any, **kwargs: Any) -> argparse.Action:
7999
"""Wrapper which forwards to the parser's add_argument method."""
80100
return self.parser.add_argument(*args, **kwargs)
@@ -84,6 +104,31 @@ def __call__(self, args: argparse.Namespace) -> None:
84104
self.callback(args)
85105

86106

107+
class _FormatterClass(Protocol):
108+
def __call__(self, *, prog: str) -> argparse.HelpFormatter: ...
109+
110+
111+
@overload
112+
def command(
113+
cmd: str | None = None,
114+
splitter: ARGPARSE_SPLITTER = shlex.split,
115+
*,
116+
prog: str | None = None,
117+
usage: str | None = None,
118+
description: str | None = None,
119+
epilog: str | None = None,
120+
parents: Sequence[argparse.ArgumentParser] = [],
121+
formatter_class: _FormatterClass = ...,
122+
prefix_chars: str = "-",
123+
fromfile_prefix_chars: str | None = None,
124+
argument_default: Any = None,
125+
conflict_handler: str = "error",
126+
add_help: bool = True,
127+
allow_abbrev: bool = True,
128+
exit_on_error: bool = True,
129+
) -> Callable[[ARGPARSE_CALLBACK], ArgParseCommand]: ...
130+
131+
87132
@overload
88133
def command(
89134
cmd: str | None = None,

0 commit comments

Comments
 (0)