Skip to content

Commit e86a8d9

Browse files
authored
Merge pull request #6 from python-project-templates/tkp/tst
Tweak parse_extra_args logic to not require argparser
2 parents 34beaea + 4e6cf69 commit e86a8d9

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

hatch_build/cli.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@
1010
_extras = None
1111

1212

13-
def parse_extra_args(subparser: Optional[ArgumentParser]) -> List[str]:
14-
return subparser.parse_args(_extras) if _extras else {}, []
13+
def parse_extra_args(subparser: Optional[ArgumentParser] = None) -> List[str]:
14+
if subparser is None:
15+
subparser = ArgumentParser(prog="hatch-build-extras", allow_abbrev=False)
16+
kwargs, extras = subparser.parse_known_args(_extras or [])
17+
return vars(kwargs), extras
1518

1619

1720
def _hatchling_internal() -> Tuple[Optional[Callable], Optional[dict], List[str]]:

hatch_build/tests/test_cli.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,15 @@ def ok_extra_argv():
5050
@pytest.fixture
5151
def get_arg():
5252
tmp_argv = sys.argv
53-
sys.argv = ["hatch-build", "--", "--extra-arg", "--extra-arg-with-value", "value", "--extra-arg-with-value-equals=value2"]
53+
sys.argv = [
54+
"hatch-build",
55+
"--",
56+
"--extra-arg",
57+
"--extra-arg-with-value",
58+
"value",
59+
"--extra-arg-with-value-equals=value2",
60+
"--extra-arg-not-in-parser",
61+
]
5462
parser = ArgumentParser()
5563
parser.add_argument("--extra-arg", action="store_true")
5664
parser.add_argument("--extra-arg-with-value")
@@ -103,6 +111,13 @@ def test_ok_extras(self, ok_extra_argv):
103111
def test_get_arg(self, get_arg):
104112
assert hatchling() == 0
105113
args, _ = parse_extra_args(get_arg)
106-
assert args.extra_arg is True
107-
assert args.extra_arg_with_value == "value"
108-
assert args.extra_arg_with_value_equals == "value2"
114+
assert args["extra_arg"] is True
115+
assert args["extra_arg_with_value"] == "value"
116+
assert args["extra_arg_with_value_equals"] == "value2"
117+
118+
def test_get_arg_no_passthrough(self, get_arg):
119+
assert hatchling() == 0
120+
_, kwargs = parse_extra_args()
121+
assert "--extra-arg" in kwargs
122+
assert "--extra-arg-with-value" in kwargs
123+
assert kwargs[kwargs.index("--extra-arg-with-value") + 1] == "value"

0 commit comments

Comments
 (0)