Added sys.argv support

This commit is contained in:
Feodor Fitsner 2024-10-31 13:13:46 -07:00
parent f377b41147
commit dddf2f4b9b
1 changed files with 12 additions and 7 deletions

View File

@ -22,7 +22,7 @@ const pythonModuleName = "{{ cookiecutter.python_module_name }}";
final hideLoadingPage = final hideLoadingPage =
bool.tryParse("{{ cookiecutter.hide_loading_animation }}".toLowerCase()) ?? bool.tryParse("{{ cookiecutter.hide_loading_animation }}".toLowerCase()) ??
true; true;
const outLogFilename = "out.log"; const outLogFilename = "console.log";
const errorExitCode = 100; const errorExitCode = 100;
List<CreateControlFactory> createControlFactories = [ List<CreateControlFactory> createControlFactories = [
@ -55,9 +55,10 @@ if os.getenv("FLET_PLATFORM") == "android":
ssl._create_default_https_context = create_default_context ssl._create_default_https_context = create_default_context
out_file = open("$outLogFilename", "w+", buffering=1) app_temp_dir = os.getenv("FLET_APP_TEMP")
out_file = open(os.path.join(app_temp_dir, "$outLogFilename"), "w+", buffering=1)
callback_socket_addr = os.environ.get("FLET_PYTHON_CALLBACK_SOCKET_ADDR") callback_socket_addr = os.getenv("FLET_PYTHON_CALLBACK_SOCKET_ADDR")
if ":" in callback_socket_addr: if ":" in callback_socket_addr:
addr, port = callback_socket_addr.split(":") addr, port = callback_socket_addr.split(":")
callback_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) callback_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
@ -77,6 +78,7 @@ sys.exit = flet_exit
ex = None ex = None
try: try:
sys.argv = [{argv}]
runpy.run_module("{module_name}", run_name="__main__") runpy.run_module("{module_name}", run_name="__main__")
except Exception as e: except Exception as e:
ex = e ex = e
@ -91,7 +93,7 @@ String assetsDir = "";
String appDir = ""; String appDir = "";
Map<String, String> environmentVariables = {}; Map<String, String> environmentVariables = {};
void main() async { void main([List<String>? args]) async {
if (isProduction) { if (isProduction) {
// ignore: avoid_returning_null_for_void // ignore: avoid_returning_null_for_void
debugPrint = (String? message, {int? wrapWidth}) => null; debugPrint = (String? message, {int? wrapWidth}) => null;
@ -114,7 +116,7 @@ void main() async {
createControlFactories: createControlFactories createControlFactories: createControlFactories
) )
: FutureBuilder( : FutureBuilder(
future: runPythonApp(), future: runPythonApp(args),
builder: builder:
(BuildContext context, AsyncSnapshot<String?> snapshot) { (BuildContext context, AsyncSnapshot<String?> snapshot) {
if (snapshot.hasData || snapshot.hasError) { if (snapshot.hasData || snapshot.hasError) {
@ -204,8 +206,11 @@ Future prepareApp() async {
return ""; return "";
} }
Future<String?> runPythonApp() async { Future<String?> runPythonApp(List<String>? args) async {
var script = pythonScript.replaceAll('{module_name}', pythonModuleName);
var argvItems = args?.map((a) => "\"${a.replaceAll('"', '\\"')}\"");
var argv = "[${argvItems != null ? argvItems.join(',') : ''}]";
var script = pythonScript.replaceAll('{module_name}', pythonModuleName).replaceAll('{argv}', argv);
var completer = Completer<String>(); var completer = Completer<String>();