1- import argparse
1+ from argparse import ArgumentParser
2+ from typing import Callable , List , Optional , Tuple
23
34from 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
0 commit comments