Skip to content

Commit

Permalink
command runner
Browse files Browse the repository at this point in the history
  • Loading branch information
willmcgugan committed Aug 30, 2024
1 parent dadb35e commit e654d8c
Showing 1 changed file with 34 additions and 4 deletions.
38 changes: 34 additions & 4 deletions src/textual_dev/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from importlib.metadata import version
from textual.constants import DEVTOOLS_HOST, DEVTOOLS_PORT

from .tools.run import exec_command, run_app
from .tools.run import _is_python_path, exec_command, run_app

WINDOWS = platform.system() == "Windows"
"""True if we're running on Windows."""
Expand Down Expand Up @@ -228,7 +228,7 @@ def _run_app(


@run.command("serve")
@click.argument("command")
@click.argument("import_name", metavar="FILE or FILE:APP")
@click.option("-h", "--host", type=str, default="localhost", help="Host to serve on")
@click.option("-p", "--port", type=int, default=8000, help="Port of server")
@click.option(
Expand All @@ -240,8 +240,25 @@ def _run_app(
)
@click.option("-u", "--url", type=str, default=None, help="Public URL")
@click.option("--dev", type=bool, default=False, is_flag=True, help="Enable debug mode")
@click.option(
"-c",
"--command",
"command",
type=bool,
default=False,
help="Run as command rather that a file / module.",
is_flag=True,
)
@click.argument("extra_args", nargs=-1, type=click.UNPROCESSED)
def serve(
command: str, host: str, port: int, title: str, url: str | None, dev: bool
import_name: str,
host: str,
port: int,
title: str,
url: str | None,
dev: bool,
extra_args: tuple[str],
command: bool = False,
) -> None:
"""Run a local web server to serve the application.
Expand All @@ -257,7 +274,20 @@ def serve(
"""
from textual_serve.server import Server

server = Server(command, host, port, title=title, public_url=url)
import_name, *args = [*shlex.split(import_name, posix=not WINDOWS), *extra_args]

if command:
run_args = shlex.join(args)
run_command = f"{import_name} {run_args}"
server = Server(run_command, host, port, title=title, public_url=url)
else:
if _is_python_path(import_name):
run_command = f"python {import_name}"
else:
run_args = shlex.join(args)
run_command = f"{import_name} {run_args}"
server = Server(run_command, host, port, title=title, public_url=url)

server.serve(debug=dev)


Expand Down

0 comments on commit e654d8c

Please sign in to comment.