add --access-logs flag to sanic cli

This commit is contained in:
Adam Hopkins 2020-10-25 20:36:22 +02:00
parent 7b1bce8d90
commit a026cd7195

View File

@ -9,15 +9,25 @@ from sanic.app import Sanic
from sanic.log import logger from sanic.log import logger
class SanicArgumentParser(ArgumentParser):
def add_bool_arguments(self, *args, **kwargs):
group = self.add_mutually_exclusive_group()
group.add_argument(*args, action="store_true", **kwargs)
kwargs["help"] = "no " + kwargs["help"]
group.add_argument(
"--no-" + args[0][2:], *args[1:], action="store_false", **kwargs
)
def main(): def main():
parser = ArgumentParser(prog="sanic") parser = SanicArgumentParser(prog="sanic")
parser.add_argument( parser.add_argument(
"-H", "-H",
"--host", "--host",
dest="host", dest="host",
type=str, type=str,
default="127.0.0.1", default="127.0.0.1",
help="Host address [default 127.0.0.1]", help="host address [default 127.0.0.1]",
) )
parser.add_argument( parser.add_argument(
"-p", "-p",
@ -25,7 +35,7 @@ def main():
dest="port", dest="port",
type=int, type=int,
default=8000, default=8000,
help="Port to serve on [default 8000]", help="port to serve on [default 8000]",
) )
parser.add_argument( parser.add_argument(
"-u", "-u",
@ -33,13 +43,13 @@ def main():
dest="unix", dest="unix",
type=str, type=str,
default="", default="",
help="Location of unix socket", help="location of unix socket",
) )
parser.add_argument( parser.add_argument(
"--cert", dest="cert", type=str, help="Location of certificate for SSL" "--cert", dest="cert", type=str, help="location of certificate for SSL"
) )
parser.add_argument( parser.add_argument(
"--key", dest="key", type=str, help="Location of keyfile for SSL." "--key", dest="key", type=str, help="location of keyfile for SSL."
) )
parser.add_argument( parser.add_argument(
"-w", "-w",
@ -47,14 +57,17 @@ def main():
dest="workers", dest="workers",
type=int, type=int,
default=1, default=1,
help="Number of worker processes [default 1]", help="number of worker processes [default 1]",
) )
parser.add_argument("--debug", dest="debug", action="store_true") parser.add_argument("--debug", dest="debug", action="store_true")
parser.add_bool_arguments(
"--access-logs", dest="access_log", help="display access logs"
)
parser.add_argument( parser.add_argument(
"-v", "--version", action="version", version=f"Sanic {__version__}", "-v", "--version", action="version", version=f"Sanic {__version__}",
) )
parser.add_argument( parser.add_argument(
"module", help="Path to your Sanic app. Example: path.to.server:app" "module", help="path to your Sanic app. Example: path.to.server:app"
) )
args = parser.parse_args() args = parser.parse_args()
@ -93,6 +106,7 @@ def main():
unix=args.unix, unix=args.unix,
workers=args.workers, workers=args.workers,
debug=args.debug, debug=args.debug,
access_log=args.access_log,
ssl=ssl, ssl=ssl,
) )
except ImportError as e: except ImportError as e: