This commit is contained in:
Adam Hopkins 2022-01-17 20:52:25 +02:00
parent 70a3a6f9fc
commit 65459fdeb6
No known key found for this signature in database
GPG Key ID: 9F85EE6C807303FB
2 changed files with 26 additions and 5 deletions

View File

@ -59,10 +59,13 @@ Or, a path to a directory to run as a simple HTTP server:
os.environ.get("SANIC_RELOADER_PROCESS", "") != "true" os.environ.get("SANIC_RELOADER_PROCESS", "") != "true"
) )
self.args: List[Any] = [] self.args: List[Any] = []
self.groups: List[Group] = []
def attach(self): def attach(self):
for group in Group._registry: for group in Group._registry:
group.create(self.parser).attach() instance = group.create(self.parser)
instance.attach()
self.groups.append(instance)
def run(self): def run(self):
# This is to provide backwards compat -v to display version # This is to provide backwards compat -v to display version
@ -142,6 +145,8 @@ Or, a path to a directory to run as a simple HTTP server:
return app return app
def _build_run_kwargs(self): def _build_run_kwargs(self):
for group in self.groups:
group.prepare(self.args)
ssl: Union[None, dict, str, list] = [] ssl: Union[None, dict, str, list] = []
if self.args.tlshost: if self.args.tlshost:
ssl.append(None) ssl.append(None)

View File

@ -1,6 +1,7 @@
from __future__ import annotations from __future__ import annotations
from argparse import ArgumentParser, _ArgumentGroup from argparse import ArgumentParser, _ArgumentGroup
from os import getpid
from typing import List, Optional, Type, Union from typing import List, Optional, Type, Union
from sanic_routing import __version__ as __routing_version__ # type: ignore from sanic_routing import __version__ as __routing_version__ # type: ignore
@ -38,6 +39,9 @@ class Group:
"--no-" + args[0][2:], *args[1:], action="store_false", **kwargs "--no-" + args[0][2:], *args[1:], action="store_false", **kwargs
) )
def prepare(self, args) -> None:
...
class GeneralGroup(Group): class GeneralGroup(Group):
name = None name = None
@ -91,21 +95,33 @@ class HTTPVersionGroup(Group):
group.add_argument( group.add_argument(
"--http", "--http",
dest="http", dest="http",
action="append",
type=int, type=int,
default=1, default=0,
help=( help=(
"Which HTTP version to use: HTTP/1.1 or HTTP/3. Value should " "Which HTTP version to use: HTTP/1.1 or HTTP/3. Value should\n"
"be either 1 or 3 [default 1]" "be either 0, 1, or 3, where '0' means use whatever versions\n"
"are available [default 0]"
), ),
) )
group.add_argument(
"-1",
dest="http",
action="append_const",
const=1,
help=("Run Sanic server using HTTP/1.1"),
)
group.add_argument( group.add_argument(
"-3", "-3",
dest="http", dest="http",
action="store_const", action="append_const",
const=3, const=3,
help=("Run Sanic server using HTTP/3"), help=("Run Sanic server using HTTP/3"),
) )
def prepare(self, args):
print(args.http)
class SocketGroup(Group): class SocketGroup(Group):
name = "Socket binding" name = "Socket binding"