move cli functionality to a separate file
This commit is contained in:
parent
d3428354d2
commit
9e44070fe7
2 changed files with 44 additions and 22 deletions
44
cli.py
Normal file
44
cli.py
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
import argparse
|
||||||
|
import http.server
|
||||||
|
import socketserver
|
||||||
|
import sys
|
||||||
|
import os
|
||||||
|
|
||||||
|
import recipes
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
parser = argparse.ArgumentParser()
|
||||||
|
subparsers = parser.add_subparsers(required=True, dest="subcommand")
|
||||||
|
|
||||||
|
parser_build = subparsers.add_parser("build")
|
||||||
|
parser_build.add_argument("directory", type=str)
|
||||||
|
|
||||||
|
parser_serve = subparsers.add_parser("serve")
|
||||||
|
parser_serve.add_argument("directory", type=str)
|
||||||
|
parser_serve.add_argument("--port", type=int, default=8000)
|
||||||
|
parser_serve.add_argument("--address", type=str, default="127.0.0.1")
|
||||||
|
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
ret = 0
|
||||||
|
if args.subcommand == "build":
|
||||||
|
ret = recipes.Builder().build(args.directory)
|
||||||
|
elif args.subcommand == "serve":
|
||||||
|
os.chdir(f"{args.directory}/out/html")
|
||||||
|
httpd = socketserver.TCPServer(
|
||||||
|
(args.address, args.port), http.server.SimpleHTTPRequestHandler
|
||||||
|
)
|
||||||
|
print(f"serving at http://{args.address}:{args.port}")
|
||||||
|
try:
|
||||||
|
httpd.serve_forever()
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
# unhandled, but valid subcommand
|
||||||
|
assert False
|
||||||
|
sys.exit(ret)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
22
recipes.py
22
recipes.py
|
@ -1,6 +1,5 @@
|
||||||
from abc import abstractmethod
|
from abc import abstractmethod
|
||||||
import collections
|
import collections
|
||||||
import sys
|
|
||||||
from typing import Dict, List, Any, Optional, Self, Set
|
from typing import Dict, List, Any, Optional, Self, Set
|
||||||
import os
|
import os
|
||||||
import json
|
import json
|
||||||
|
@ -689,24 +688,3 @@ class Builder:
|
||||||
print("ERROR:", e)
|
print("ERROR:", e)
|
||||||
return 1
|
return 1
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
def help() -> None:
|
|
||||||
print(f"usage: {sys.argv[0]} build DIR - build pages in DIR/out")
|
|
||||||
print(f" {sys.argv[0]} -h - show help")
|
|
||||||
|
|
||||||
|
|
||||||
def main() -> None:
|
|
||||||
if len(sys.argv) == 2 and sys.argv[1] == "-h":
|
|
||||||
help()
|
|
||||||
sys.exit(0)
|
|
||||||
elif len(sys.argv) == 3 and sys.argv[1] == "build":
|
|
||||||
ret = Builder().build(sys.argv[2])
|
|
||||||
sys.exit(ret)
|
|
||||||
else:
|
|
||||||
help()
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
main()
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue