4

私のLuaプログラムでは、外部プログラムからの出力をキャッチする必要があります。この外部プログラムには、特定の環境変数が必要です。だから私はこれを行います:

e = ""
e = e .. "A=100;"
e = e .. "B=Hi;"
e = e .. "C=Test;"
file = io.popen(e .. "/bin/aprogr")

明らかに、環境が大きい場合、popen()の引数は制限を超える可能性があります(存在する場合)。

環境を外部プログラムに渡す他の方法はありますか?

4

1 に答える 1

4

ExtensionProposal APIos.spawnに関数があり ます。

次のように使用できます。

require"ex"
local proc, err = os.spawn{
    command = e.."/bin/aprogr",
    args = {
        "arg1",
        "arg2",
        -- etc
    },
    env = {
        A = 100, -- I assume it tostrings the value
        B = "Hi",
        C = "Test",
    },
    -- you can also specify stdin, stdout, and stderr
    -- see the proposal page for more info
}
if not proc then
    error("Failed to aprogrinate! "..tostring(err))
end

-- if you want to wait for the process to finish:
local exitcode = proc:wait()

lua-ex-paiは、POSIX および Windows の実装を提供します。

この実装のプリコンパイル済みバイナリは、LuaForWindowsディストリビューションにバンドルされています。

ユースケースのより簡潔なバージョンを次に示します。

require"ex"
local file = io.pipe()
local proc = assert(os.spawn(e.."/bin/aprogr", {
    env={ A = 100, B = "Hi", C = "Test" },
    stdout = file,
}))
-- write to file as you wish
于 2012-01-12T19:07:49.910 に答える