Skip to content

Commit 2625541

Browse files
authored
Merge pull request #5 from python-project-templates/tkp/getarg
Add utility for getting extra args
2 parents 09a4e85 + 7a8751d commit 2625541

File tree

3 files changed

+52
-7
lines changed

3 files changed

+52
-7
lines changed

hatch_build/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
__version__ = "0.1.0"
2+
3+
from .cli import parse_extra_args

hatch_build/cli.py

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
1-
import argparse
1+
from argparse import ArgumentParser
2+
from typing import Callable, List, Optional, Tuple
23

34
from hatchling.cli.build import build_command
45

6+
__all__ = (
7+
"hatchling",
8+
"parse_extra_args",
9+
)
10+
_extras = None
511

6-
def hatchling() -> int:
7-
parser = argparse.ArgumentParser(prog="hatch-build", allow_abbrev=False)
12+
13+
def parse_extra_args(subparser: Optional[ArgumentParser]) -> List[str]:
14+
return subparser.parse_args(_extras) if _extras else {}, []
15+
16+
17+
def _hatchling_internal() -> Tuple[Optional[Callable], Optional[dict], List[str]]:
18+
parser = ArgumentParser(prog="hatch-build", allow_abbrev=False)
819
subparsers = parser.add_subparsers()
920

1021
defaults = {"metavar": ""}
@@ -21,7 +32,7 @@ def hatchling() -> int:
2132
# but they must be after a '--' separator
2233
if extras and extras[0] != "--":
2334
parser.print_help()
24-
return 1
35+
return None, None, None
2536

2637
# Wrap the parsed arguments in a dictionary
2738
kwargs = vars(kwargs)
@@ -30,7 +41,19 @@ def hatchling() -> int:
3041
command = kwargs.pop("func")
3142
except KeyError:
3243
parser.print_help()
33-
else:
34-
command(**kwargs)
44+
return None, None, None
45+
return command, kwargs, extras[1:] # Remove the '--' separator
46+
47+
48+
def hatchling() -> int:
49+
global _extras
50+
51+
command, kwargs, extras = _hatchling_internal()
52+
if command is None:
53+
return 1
54+
55+
# Set so plugins can reference
56+
_extras = extras
3557

58+
command(**kwargs)
3659
return 0

hatch_build/tests/test_cli.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import sys
2+
from argparse import ArgumentParser
23
from unittest.mock import patch
34

45
import pytest
56

6-
from hatch_build.cli import hatchling
7+
from hatch_build.cli import hatchling, parse_extra_args
78

89

910
@pytest.fixture
@@ -46,6 +47,18 @@ def ok_extra_argv():
4647
sys.argv = tmp_argv
4748

4849

50+
@pytest.fixture
51+
def get_arg():
52+
tmp_argv = sys.argv
53+
sys.argv = ["hatch-build", "--", "--extra-arg", "--extra-arg-with-value", "value", "--extra-arg-with-value-equals=value2"]
54+
parser = ArgumentParser()
55+
parser.add_argument("--extra-arg", action="store_true")
56+
parser.add_argument("--extra-arg-with-value")
57+
parser.add_argument("--extra-arg-with-value-equals")
58+
yield parser
59+
sys.argv = tmp_argv
60+
61+
4962
class TestHatchBuild:
5063
def test_hatchling(self, ok_argv):
5164
assert hatchling() == 0
@@ -86,3 +99,10 @@ def test_bad_extras(self, bad_extra_argv):
8699

87100
def test_ok_extras(self, ok_extra_argv):
88101
assert hatchling() == 0
102+
103+
def test_get_arg(self, get_arg):
104+
assert hatchling() == 0
105+
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"

0 commit comments

Comments
 (0)