如何在另一个 Lua 脚本内运行用户指定参数的 Lua 脚本?

如何在另一个 Lua 脚本内运行用户指定参数的 Lua 脚本?

以下代码是否可行?其中“content_image”是指用户指定的输入图像(可以保存到图像文件中,也可以仍然在脚本中),输入到“deepdream.lua”脚本中,“output_image”是我想要在我的 Lua 脚本中使用的“deepdream.lua”脚本的输出。

dofile(“deepdream.lua -content_image content_image -output_image output_image”)

我想在另一个 Lua 脚本内运行的脚本可以在此处找到:https://github.com/bamos/dream-art/blob/master/deepdream.lua

点赞
用户734069
用户734069

如果你想通过传递一个参数来加载和执行脚本,你需要这样做...加载脚本并通过传递一个一系列的参数来执行它:

local chunk = loadfile("deepdream.lua")
chunk("-content_image", "content_image", "-output_image", "output_image")

请注意,这将 不会lua.exe 一样填充 args 的参数。它将像任何其他 Lua 函数一样通过可变的参数传递参数。因此,它可能会影响你的全局变量等。此外,与执行 lua.exe 不同,这将在当前进程中执行,因此如果出错,错误将由你处理。

如果你愿意,非常容易编写一个函数,使用 Lua 模式来解析参数等字符串,然后使用这些参数加载脚本。

如果你想要完全像使用 lua.exe 一样执行脚本,那么你只需要使用 os.execute:

os.execute("lua.exe deepdream.lua -content_image content_image -output_image output_image")
2017-03-09 17:41:37
用户7504558
用户7504558

你可以在 arg 中使用参数来使用 loadfile:

loadfile("deepdream.lua")({content_image="content_image",output_image="output_image"})

在 deepdream.lua 中:

local arg={...}

local content_image = arg[1].content_image
local output_image  = arg[1].output_image
2017-03-09 18:11:31