From 551c6529000e4daf7eac4a3da804905ef74f7842 Mon Sep 17 00:00:00 2001 From: Micha Sengotta Date: Thu, 11 May 2023 22:51:03 +0200 Subject: [PATCH] Add CLI option -m to run as module --- .../flet/src/flet/cli/commands/run.py | 25 ++++++++++++++++--- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/sdk/python/packages/flet/src/flet/cli/commands/run.py b/sdk/python/packages/flet/src/flet/cli/commands/run.py index 7b45f1edb..c09a09f69 100644 --- a/sdk/python/packages/flet/src/flet/cli/commands/run.py +++ b/sdk/python/packages/flet/src/flet/cli/commands/run.py @@ -31,6 +31,14 @@ def add_arguments(self, parser: argparse.ArgumentParser) -> None: default=None, help="custom TCP port to run Flet app on", ) + parser.add_argument( + "-m", + "--module", + dest="module", + action="store_true", + default=False, + help="treat the script as a python module path as opposed to a file path", + ) parser.add_argument( "-d", "--directory", @@ -74,9 +82,17 @@ def add_arguments(self, parser: argparse.ArgumentParser) -> None: def handle(self, options: argparse.Namespace) -> None: # print("RUN COMMAND", options) - script_path = options.script - if not os.path.isabs(options.script): - script_path = str(Path(os.getcwd()).joinpath(options.script).resolve()) + if options.module: + script_path = str(options.script).replace(".", "/") + if os.path.isdir(script_path): + script_path += "/__main__.py" + else: + script_path += ".py" + else: + script_path = options.script + + if not os.path.isabs(script_path): + script_path = str(Path(os.getcwd()).joinpath(script_path).resolve()) if not Path(script_path).exists(): print(f"File not found: {script_path}") @@ -99,7 +115,8 @@ def handle(self, options: argparse.Namespace) -> None: ) my_event_handler = Handler( - [sys.executable, "-u", script_path], + [sys.executable, "-u"] + ["-m"] * options.module + + [options.script if options.module else script_path], None if options.directory or options.recursive else script_path, port, uds_path,