Skip to content

Commit 4480b63

Browse files
authored
Add mesop init CLI command (google#633)
1 parent b47ea60 commit 4480b63

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

mesop/bin/bin.py

+34-2
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,45 @@
3030
def main(argv: Sequence[str]):
3131
if len(argv) < 2:
3232
print(
33-
"""\u001b[31mERROR: missing command-line argument to Mesop application.\u001b[0m
33+
"""\u001b[31mERROR: missing command-line argument to Mesop.\u001b[0m
3434
35-
Example command:
35+
Example run command:
3636
$\u001b[35m mesop file.py\u001b[0m"""
3737
)
3838
sys.exit(1)
3939

40+
if argv[1] == "init":
41+
if len(argv) > 3:
42+
print(
43+
f"""\u001b[31mERROR: Too many command-line arguments for mesop init.\u001b[0m
44+
45+
Actual:
46+
$\u001b[35m mesop {" ".join(argv[1:])}\u001b[0m
47+
48+
Re-run with:
49+
$\u001b[35m mesop init file.py\u001b[0m"""
50+
)
51+
sys.exit(1)
52+
53+
filename = argv[2] if len(argv) == 3 else "main.py"
54+
absolute_path = make_path_absolute(filename)
55+
if os.path.isfile(absolute_path):
56+
print(f"\u001b[31mERROR: {filename} already exists.\u001b[0m")
57+
sys.exit(1)
58+
with open(
59+
os.path.join(
60+
os.path.dirname(os.path.abspath(__file__)),
61+
"../examples/starter_kit/starter_kit.py",
62+
)
63+
) as src_file:
64+
with open(absolute_path, "w") as dest_file:
65+
dest_file.write(src_file.read().replace("/starter_kit", "/"))
66+
print(f"""Created starter kit Mesop app at {filename} 🎉
67+
68+
Run Mesop app:
69+
$\u001b[35m mesop {filename}\u001b[0m
70+
""")
71+
sys.exit(0)
4072
if len(argv) > 2:
4173
print(
4274
f"""\u001b[31mERROR: Too many command-line arguments.\u001b[0m

0 commit comments

Comments
 (0)