2017-03-09 65 views
1

我如何使用用戶指定的參數從另一個Lua腳本中運行Lua腳本?我將如何使用用戶指定的參數從另一個Lua腳本中運行Lua腳本?

下面的代碼可以工作嗎?其中「content_image」是我指定的輸入圖像(保存到圖像文件或仍然在腳本中)到「deepdream.lua」腳本中,「output_image」是我想要的「deepdream.lua」腳本的輸出在我的Lua腳本中使用。

dofile("deepdream.lua -content_image content_image -output_image output_image") 

我要求另一Lua的腳本中運行該腳本可以在這裏找到:https://github.com/bamos/dream-art/blob/master/deepdream.lua

+0

'os.execute(「th deepdream.lua -content_image conte nt_image -output_image output_image「)' –

回答

2

如果要加載和傳遞一些參數執行腳本,你必須這樣做由...加載腳本,並通過它傳遞一些參數執行它:

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

注意,這將不填寫args爲參數的方式lua.exe呢。它將像任何其他Lua函數一樣將參數作爲可變參數傳遞。所以它可能會混淆你的全局變量等等。此外,與執行lua.exe不同,此操​​作將在當前進程中執行,因此如果出錯,則必須由您處理該錯誤。

如果需要,編寫一個接受您提供的字符串的函數並不難,使用Lua模式來解析參數等等,然後用這些參數加載腳本。

如果要執行完全一樣,如果你使用過它lua.exe一個腳本,那麼你就只使用os.execute

os.execute("lua.exe deepdream.lua -content_image content_image -output_image output_image") 
+0

我的理由是在第一個腳本運行第二個腳本後,兩個腳本之間的變量具有相似的名稱,導致衝突。實際上我試圖在Neural-Style的修改版本中運行這個DeepDream腳本:https://gist.github.com/ProGamerGov/ea432324a09822a19af916fe1bfcfc01,這樣我就可以以簡單的方式將樣式轉換和DeepDream結合起來測試概念。所以我的測試環境是Ubuntu。 – ProGamerGov

+0

我還會添加一個[rings](https://github.com/keplerproject/rings)。 –

2

您可以在使用arg的loadFile與參數:

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

in deepdream.lua:

local arg={...} 

local content_image = arg[1].content_image 
local output_image = arg[1].output_image