Skip to content

Commit dd59993

Browse files
committed
Create a temporary file if --show and no --output
1 parent a74694c commit dd59993

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

src/pyscript/cli.py

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""The main CLI entrypoint and commands."""
2+
import time
23
import webbrowser
34
from pathlib import Path
45
from typing import Any, Optional
@@ -69,18 +70,38 @@ def wrap(
6970
show: Optional[bool] = _show_option,
7071
) -> None:
7172
"""Wrap a Python script inside an HTML file."""
73+
74+
if not input_file and not command:
75+
raise Abort(
76+
"Must provide either an input '.py' file or a command with the '-c' option."
77+
)
78+
if input_file and command:
79+
raise Abort("Cannot provide both an input '.py' file and '-c' option.")
80+
81+
# Derive the output path if it is not provided
82+
remove_output = False
83+
if output is None:
84+
if command and show:
85+
output = Path("pyscript_tmp.html")
86+
remove_output = True
87+
elif not command:
88+
assert input_file is not None
89+
output = input_file.with_suffix(".html")
90+
else:
91+
raise Abort("Must provide an output file or use `--show` option")
92+
7293
if input_file is not None:
73-
if command is not None:
74-
raise Abort("Cannot provide both an input file and `-c` option.")
7594
file_to_html(input_file, output)
7695

7796
if command:
78-
if output is None:
79-
raise Abort("Must provide an output file or use `--show` option")
8097
string_to_html(command, output)
8198

99+
assert output is not None
100+
82101
if show:
83-
if output is None:
84-
raise Abort("Must provide an output file to use `--show` option")
85102
console.print("Opening in web browser!")
86103
webbrowser.open(f"file://{output.resolve()}")
104+
105+
if remove_output:
106+
time.sleep(1)
107+
output.unlink()

0 commit comments

Comments
 (0)