command-lineluadeep-dream

How would I run a Lua script with user specified parameters from inside another Lua script?


How would I run a Lua script with user specified parameters from inside another Lua script?

Would the below code work? Where "content_image" is my specified input image (either saved to an image file, or still in the script) into the "deepdream.lua" script, and "output_image" is the output from the "deepdream.lua" script that I want to use in my Lua script.

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

The script I am seeking to run within another Lua script can be found here: https://github.com/bamos/dream-art/blob/master/deepdream.lua


Solution

  • If you want to load and execute a script by passing it a number of parameters, you have to do this by... loading the script and executing it by passing it a number of parameters:

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

    Note that this will not fill in args for the arguments the way lua.exe does. It will pass the parameters as variadic parameters, just like any other Lua function. So it can mess with your globals and so forth. Also, unlike executing lua.exe, this will be executed in the current process, so if it errors out, the error will have to be handled by you.

    If you want, it wouldn't be difficult at all to write a function that takes the string you provided, uses Lua patterns to parse parameters and so forth, and then loads the script with those parameters.

    If you want to execute a script exactly as if you had used lua.exe on it, then you would just use os.execute:

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