Skip to content
This repository has been archived by the owner on Jul 7, 2024. It is now read-only.

Commit

Permalink
Improve arg loader, general changes
Browse files Browse the repository at this point in the history
  • Loading branch information
SquidDev committed Dec 6, 2014
1 parent 487aa9d commit 948eedd
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 24 deletions.
36 changes: 25 additions & 11 deletions ArgParse.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ function Parameter:Matches(arg, options, tArgs)
end
arg = arg:sub(2)

if not (arg:find("^"..self.name.."$") or arg:find("^"..self.shortcut.."$")) then
local name, shortcut = Utils.EscapePattern(self.name), Utils.EscapePattern(self.shortcut)
if not (arg:find("^"..name.."$") or arg:find("^"..shortcut.."$")) then
return false
end

Expand Down Expand Up @@ -46,9 +47,11 @@ function Switch:Matches(arg, options, tArgs)
arg = arg:sub(2)

local value = nil
if arg:find("^"..self.name.."$") or arg:find("^"..self.shortcut.."$") then
local name, shortcut = Utils.EscapePattern(self.name), Utils.EscapePattern(self.shortcut)

if arg:find("^"..name.."$") or arg:find("^"..shortcut.."$") then
value = true
elseif arg:find("^".."not%-" .. self.name.."$") or arg:find("^".."n" .. self.shortcut.."$") then
elseif arg:find("^".."not%-" .. name.."$") or arg:find("^".."n" .. shortcut.."$") then
value = false
else
return false
Expand Down Expand Up @@ -97,6 +100,7 @@ end
function Argument:Count(count)
assert(type(count) == "number" or count == "*", "Bad argument to Argument:count. Expected number, got " .. count)
self.count = count
self.oldCount = count
return self
end

Expand All @@ -106,7 +110,7 @@ function Argument:Default(value)
end

local Parser = {}
--- Add a parameter (--key=value)
--- Add a parameter (-key value)
-- @tparam string The name of the parameter
-- @treturn Parameter The created parameter object
function Parser:Parameter(name)
Expand All @@ -130,7 +134,7 @@ end
-- @tparam string The name of the argument
-- @treturn Argument The created argument object
function Parser:Argument(name)
local arg = setmetatable({name=name,count=1}, {__index=Argument})
local arg = setmetatable({name=name,count=1,oldCount = 1}, {__index=Argument})
table.insert(self.arguments, arg)
self.changed = true
return arg
Expand Down Expand Up @@ -164,14 +168,23 @@ function Parser:ParseArg(arg, args)
end

function Parser:Parse()
local spare = {}
local args = self.args
local args = {}

-- Reset variables
for _, v in ipairs(self.arguments) do
v.matched = false
v.count = v.oldCount
end
-- Copy table
for _, v in ipairs(self.args) do
table.insert(args, v)
end

-- Process args
for arg in function() return table.remove(args, 1) end do
if not self:ParseArg(arg, args) then
table.insert(spare, arg)
end
self:ParseArg(arg, args)
end
self.args = spare

self.changed = false
return self
end
Expand All @@ -192,6 +205,7 @@ end
-- @treturn table A dictionary of calculated results
function Parser:Options()
if self.changed then
self.options = {}
self:Parse()
end

Expand Down
19 changes: 18 additions & 1 deletion Howl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ parser
parser
:Switch "time"
:Shortcut "t"
parser
:Switch "trace"
parser
:Argument "task"

Expand All @@ -17,7 +19,8 @@ end)
local tasks = Task.Tasks()
-- Setup listener for time
parser:OnChanged(function(self, options)
tasks:ShowTime(options.time)
tasks.ShowTime = options.time
tasks.Traceback = options.trace
end)

parser:Options() -- Setup options
Expand All @@ -31,6 +34,19 @@ tasks:Task "list" (function()
tasks:ListTasks()
end):Description "Lists all the tasks"

tasks:Task "help" (function()
Utils.Print("Howl [-v] [-t] <task>\nAvaliable tasks: ")
tasks:ListTasks(" ")

Utils.Print([[Options:
-v/-verbose Verbose output
-t/-time Show time taken for tasks
Use -not-{name} or -n{short} to switch an option off
]])

end)

-- If no other task exists run this
tasks:Default(function()
Utils.PrintError("No default task exists.")
Expand All @@ -46,6 +62,7 @@ local environment = HowlFile.SetupEnvironment({
Options = parser,
Dependencies = Depends.Depends,
Verbose = Utils.Verbose,
File = function(...) return fs.combine(currentDirectory, ...) end
}, currentDirectory)

-- Load the file
Expand Down
10 changes: 9 additions & 1 deletion HowlFileLoader.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
--- @module HowlFileLoader

--- Finds the howl file
-- @treturn string The name of the howl file
-- @treturn string The path of the howl file
local function FindHowl()
local currentDirectory = shell.dir()
local howlFiles = {"Howlfile", "Howlfile.lua"}
Expand All @@ -20,8 +25,11 @@ local function FindHowl()
error("Cannot find HowlFile. Looking for '" .. table.concat(howlFiles, "', '") .. "'")
end

--- Create an environment for running howl files
-- @tparam table variables A list of variables to include in the environment
-- @tparam table The created environment
local function SetupEnvironment(variables)
local env = setmetatable(variables, { __index = getfenv()})
local env = setmetatable(variables or {}, { __index = getfenv()})

env._G = _G
function env.loadfile(path)
Expand Down
28 changes: 18 additions & 10 deletions Task.lua
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,6 @@ function TaskRunner:Default(task)
return self
end

--- Sets whether tasks should be timedthe time each task took?
-- @tparam bool showTime Time tasks?
-- @treturn TaskRunner The current task object
function TaskRunner:ShowTime(showTime)
self.showTime = showTime
return self
end

--- Run a task, and all its dependencies
-- @tparam string name Name of the task to run
-- @tparam table arguments A list of arguments to pass to the task
Expand All @@ -109,7 +101,8 @@ function TaskRunner:Run(name, context)
name = "<default>"
end

local showTime = self.showTime
local showTime = self.ShowTime
local traceback = self.Traceback

if not currentTask then
Utils.PrintError("Cannot find a task called " .. name)
Expand All @@ -127,7 +120,22 @@ function TaskRunner:Run(name, context)
local oldTime = os.time()
Utils.PrintColor(colors.cyan, "Running " .. name)
assert(currentTask.action, "Action cannot be nil")
local s, err = pcall(function() currentTask.action() end)

local s, err = true, nil
if traceback then
xpcall(currentTask.action, function(msg)
for i = 4, 15 do
local _, err = pcall(function() error("", i) end)
if msg:match("Howlfile") then break end
msg = msg .. "\n " .. err
end

err = msg
s = false
end)
else
s, err = pcall(currentTask.action)
end
if s then
Utils.PrintSuccess(name .. ": Success")
else
Expand Down
27 changes: 26 additions & 1 deletion Utils.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
--@module Utils
--- @module Utils

local isVerbose = false

Expand Down Expand Up @@ -42,6 +42,29 @@ local function Verbose(...)
end
end

local matches = {
["^"] = "%^";
["$"] = "%$";
["("] = "%(";
[")"] = "%)";
["%"] = "%%";
["."] = "%.";
["["] = "%[";
["]"] = "%]";
["*"] = "%*";
["+"] = "%+";
["-"] = "%-";
["?"] = "%?";
["\0"] = "%z";
}

--- Escape a string for using in a pattern
-- @tparam string pattern The string to escape
-- @treturn string The escaped pattern
local function EscapePattern(pattern)
return (pattern:gsub(".", matches))
end

return {
Print = print,
PrintError = printError,
Expand All @@ -51,4 +74,6 @@ return {

SetVerbose = SetVerbose,
Verbose = Verbose,

EscapePattern = EscapePattern,
}

0 comments on commit 948eedd

Please sign in to comment.