Skip to content

Commit 7fee463

Browse files
committed
Add cli
1 parent bba0e83 commit 7fee463

File tree

3 files changed

+153
-2
lines changed

3 files changed

+153
-2
lines changed

hatch_build/cli.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import argparse
2+
3+
from hatchling.cli.build import build_command
4+
5+
6+
def hatchling() -> int:
7+
parser = argparse.ArgumentParser(prog="hatch-build", allow_abbrev=False)
8+
subparsers = parser.add_subparsers()
9+
10+
defaults = {"metavar": ""}
11+
build_command(subparsers, defaults)
12+
13+
# Replace parser with just the build one
14+
parser = subparsers.choices["build"]
15+
parser.prog = "hatch-build"
16+
17+
# Parse known arguments
18+
kwargs, extras = parser.parse_known_args()
19+
20+
# Extras can exist to be detected in custom hooks and plugins,
21+
# but they must be after a '--' separator
22+
if extras and extras[0] != "--":
23+
parser.print_help()
24+
return 1
25+
26+
# Wrap the parsed arguments in a dictionary
27+
kwargs = vars(kwargs)
28+
29+
try:
30+
command = kwargs.pop("func")
31+
except KeyError:
32+
parser.print_help()
33+
else:
34+
command(**kwargs)
35+
36+
return 0
37+
38+
# parser = subparsers.add_parser('build')
39+
# parser.add_argument(
40+
# '-d', '--directory', dest='directory', help='The directory in which to build artifacts', **defaults
41+
# )
42+
# parser.add_argument(
43+
# '-t',
44+
# '--target',
45+
# dest='targets',
46+
# action='append',
47+
# help='Comma-separated list of targets to build, overriding project defaults',
48+
# **defaults,
49+
# )
50+
# parser.add_argument('--hooks-only', dest='hooks_only', action='store_true', default=None)
51+
# parser.add_argument('--no-hooks', dest='no_hooks', action='store_true', default=None)
52+
# parser.add_argument('-c', '--clean', dest='clean', action='store_true', default=None)
53+
# parser.add_argument('--clean-hooks-after', dest='clean_hooks_after', action='store_true', default=None)
54+
# parser.add_argument('--clean-only', dest='clean_only', action='store_true')
55+
# parser.add_argument('--app', dest='called_by_app', action='store_true', help=argparse.SUPPRESS)
56+
# parser.set_defaults(func=build_impl)

hatch_build/tests/test_cli.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import sys
2+
from unittest.mock import patch
3+
4+
import pytest
5+
6+
from hatch_build.cli import hatchling
7+
8+
9+
@pytest.fixture
10+
def ok_argv():
11+
tmp_argv = sys.argv
12+
sys.argv = ["hatch-build"]
13+
yield
14+
sys.argv = tmp_argv
15+
16+
17+
@pytest.fixture
18+
def help_argv():
19+
tmp_argv = sys.argv
20+
sys.argv = ["hatch-build", "--help"]
21+
yield
22+
sys.argv = tmp_argv
23+
24+
25+
@pytest.fixture
26+
def bad_argv():
27+
tmp_argv = sys.argv
28+
sys.argv = ["hatch-build", "unexpected_arg"]
29+
yield
30+
sys.argv = tmp_argv
31+
32+
33+
@pytest.fixture
34+
def bad_extra_argv():
35+
tmp_argv = sys.argv
36+
sys.argv = ["hatch-build", "--unexpected_arg"]
37+
yield
38+
sys.argv = tmp_argv
39+
40+
41+
@pytest.fixture
42+
def ok_extra_argv():
43+
tmp_argv = sys.argv
44+
sys.argv = ["hatch-build", "--", "--extra-arg"]
45+
yield
46+
sys.argv = tmp_argv
47+
48+
49+
class TestHatchBuild:
50+
def test_hatchling(self, ok_argv):
51+
assert hatchling() == 0
52+
53+
def test_help(self, help_argv):
54+
from contextlib import redirect_stderr, redirect_stdout
55+
from io import StringIO
56+
57+
f = StringIO()
58+
with patch("sys.exit"):
59+
with redirect_stdout(f), redirect_stderr(f):
60+
result = hatchling()
61+
output = f.getvalue()
62+
assert result == 0
63+
assert "usage: hatch-build [-h] [-d] [-t] [--hooks-only]" in output
64+
65+
def test_bad(self, bad_argv):
66+
from contextlib import redirect_stderr, redirect_stdout
67+
from io import StringIO
68+
69+
f = StringIO()
70+
with redirect_stdout(f), redirect_stderr(f):
71+
result = hatchling()
72+
output = f.getvalue()
73+
assert result == 1
74+
assert "usage: hatch-build [-h] [-d] [-t]" in output
75+
76+
def test_bad_extras(self, bad_extra_argv):
77+
from contextlib import redirect_stderr, redirect_stdout
78+
from io import StringIO
79+
80+
f = StringIO()
81+
with redirect_stdout(f), redirect_stderr(f):
82+
result = hatchling()
83+
output = f.getvalue()
84+
assert result == 1
85+
assert "usage: hatch-build [-h] [-d] [-t]" in output
86+
87+
def test_ok_extras(self, ok_extra_argv):
88+
assert hatchling() == 0

pyproject.toml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ readme = "README.md"
1010
license = { text = "Apache-2.0" }
1111
version = "0.1.0"
1212
requires-python = ">=3.9"
13-
keywords = []
13+
keywords = [
14+
"build",
15+
"hatch",
16+
"packaging",
17+
]
1418

1519
classifiers = [
1620
"Development Status :: 3 - Alpha",
@@ -26,7 +30,9 @@ classifiers = [
2630
"Programming Language :: Python :: 3.14",
2731
]
2832

29-
dependencies = []
33+
dependencies = [
34+
"hatchling>=1.14.1,<1.15",
35+
]
3036

3137
[project.optional-dependencies]
3238
develop = [
@@ -46,6 +52,7 @@ develop = [
4652
]
4753

4854
[project.scripts]
55+
hatch-build = "hatch_build.cli:hatchling"
4956

5057
[project.urls]
5158
Repository = "https://github.com/python-project-templates/hatch-build"

0 commit comments

Comments
 (0)