Skip to content

Commit

Permalink
Better error handling in f and e commands
Browse files Browse the repository at this point in the history
  • Loading branch information
justas-d committed Feb 1, 2017
1 parent d9009ce commit dc30071
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
4 changes: 4 additions & 0 deletions base/Api/ApiBootstrap.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ _G.Api =
debug = _G.debug,
type = _G.type,
pcall = _G.pcall,
string =
{
dump = _G.string.dump
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion base/Api/ChatConsole.lua
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ function ChatConsole:HandleChatInput(input)

cmdKey = cmdKey .. token.Data

status, err = self:TryCommand(cmdKey, input:sub(token.Pos))
status, err = self:TryCommand(cmdKey, input:sub(token.Pos):match'^%s*(.*%S)' or '')
if status then return true, err end

cmdKey = cmdKey .. " "
Expand Down
12 changes: 10 additions & 2 deletions base/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,18 @@ assert(Api.ChatConsole:RegisterCommand(Api.ConsoleCommandClass("clear", "Clears

-- evaluate script
assert(Api.ChatConsole:RegisterCommand(Api.ConsoleCommandClass("e", "Evaluates lua input.", baseMod,
function(cmd, input) return Api.Std.loadstring(input)() end)))
function(cmd, input)
local chunk, err = Api.Std.loadstring(input)
if chunk == nil then return err end
return chunk()
end)))

-- execute script file
assert(Api.ChatConsole:RegisterCommand(Api.ConsoleCommandClass("f", "Executes lua file in mods/file/__INPUT__.lua", baseMod,
function(cmd, input) return Api.Std.loadfile("mods/file/" .. input .. ".lua")() end)))
function(cmd, input)
local chunk, err = Api.Std.loadfile("mods/file/" .. input .. ".lua")
if chunk == nil then return error end
return chunk()
end)))

Log.Write("Main.lua is done.")

0 comments on commit dc30071

Please sign in to comment.