Allow runCommand arguments to be passed as a table internally

develop
lethosor 2014-06-16 11:16:35 -04:00
parent 7593d8404c
commit d538e13450
2 changed files with 37 additions and 6 deletions

@ -2230,11 +2230,34 @@ static int internal_getDir(lua_State *L)
static int internal_runCommand(lua_State *L)
{
luaL_checktype(L, 1, LUA_TSTRING);
CoreSuspender suspend;
std::string command = lua_tostring(L, 1);
buffered_color_ostream out;
command_result res = Core::getInstance().runCommand(out, command);
command_result res;
if (lua_gettop(L) == 0)
{
lua_pushstring(L, "");
}
if (lua_type(L, 1) == LUA_TTABLE)
{
std::string command = "";
std::vector<std::string> args;
lua_pushnil(L); // first key
while (lua_next(L, 1) != 0)
{
if (command == "")
command = lua_tostring(L, -1);
else
args.push_back(lua_tostring(L, -1));
lua_pop(L, 1); // remove value, leave key
}
CoreSuspender suspend;
res = Core::getInstance().runCommand(out, command, args);
}
else
{
std::string command = lua_tostring(L, 1);
CoreSuspender suspend;
res = Core::getInstance().runCommand(out, command);
}
auto fragments = out.fragments();
lua_newtable(L);
lua_pushinteger(L, (int)res);

@ -369,9 +369,17 @@ end
function dfhack.run_command(...)
args = {...}
if type(args[1]) == 'table' then
command = table.concat(args[1], ' ')
command = args[1]
elseif #args > 1 and type(args[2]) == 'table' then
-- {args[1]} + args[2]
command = args[2]
table.insert(command, 1, args[1])
elseif #args == 1 and type(args[1]) == 'string' then
command = args[1]
elseif #args > 1 and type(args[1]) == 'string' then
command = args
else
command = table.concat(args, ' ')
error('Invalid arguments')
end
result = internal.runCommand(command)
output = ""