aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorNathan Reiner <nathan@nathanreiner.xyz>2026-04-16 13:17:30 +0200
committerNathan Reiner <nathan@nathanreiner.xyz>2026-04-16 13:21:36 +0200
commit31157deb353acd0233e56cdf8ebf1fbe201631b3 (patch)
treeee1a7e589d3c47e6ee1a331a0f5c65196b21e7fd /src
inital commit
Diffstat (limited to 'src')
-rw-r--r--src/arg.lua126
-rwxr-xr-xsrc/blueprint16
2 files changed, 142 insertions, 0 deletions
diff --git a/src/arg.lua b/src/arg.lua
new file mode 100644
index 0000000..a0acd9c
--- /dev/null
+++ b/src/arg.lua
@@ -0,0 +1,126 @@
+
+local function help(opts)
+
+ io.write(arg[0])
+ io.write(" ")
+
+ for _, v in ipairs(opts) do
+ if v.kind == nil then
+ io.write("[" .. v.name .. "] ")
+ end
+ end
+ io.write("\n")
+
+ local max_width = 0
+
+ for _, v in ipairs(opts) do
+ if v.kind ~= nil then
+ max_width = math.max(v.name:len(), max_width)
+ end
+ end
+
+ for _, v in ipairs(opts) do
+ if v.kind == nil then
+ goto next
+ end
+
+ if v.kind == 'property' then
+ io.write("\t" .. "--" .. v.name .. " <value>")
+ elseif v.kind == 'flag' then
+ io.write("\t" .. "--" .. v.name .. " ")
+ end
+
+ if v.description ~= nil then
+ io.write(string.rep(" ", max_width - v.name:len()) .. " " .. "-- ")
+
+ if v.required then
+ io.write("(required) ")
+ end
+
+ io.write(v.description)
+ end
+
+ io.write("\n")
+
+ ::next::
+ end
+
+ os.exit()
+end
+
+function string:flag()
+ if self:sub(1, 2) ~= "--" then
+ return nil
+ end
+
+ return self:sub(3)
+end
+
+return function (opts)
+ local args = {}
+
+ local current_index = 1
+ local current_flag = nil
+
+ local params = {}
+
+ for _, v in ipairs(opts) do
+ if v.kind ~= nil then
+ params[v.name] = v
+ args[v.name] = v.default
+ else
+ params[#params + 1] = v
+ end
+ end
+
+ for _, value in ipairs(arg) do
+ local key = nil
+ if current_flag == nil then
+ local flag = value:flag()
+ if flag == nil then
+ local option = params[current_index]
+ current_index = current_index + 1
+
+ if option == nil then
+ print("error: too many arguments")
+ help(opts)
+ end
+
+ key = option.name
+ else
+ local option = params[flag]
+
+ if option == nil then
+ print("error: unknown flag '" .. flag .. "'")
+ help(opts)
+ end
+
+ if option.kind == 'flag' then
+ key = flag
+ value = true
+ else
+ current_flag = flag
+ goto next
+ end
+ end
+ else
+ local option = params[current_flag]
+
+ key = option.name
+ current_flag = nil
+ end
+
+ args[key] = value
+
+ ::next::
+ end
+
+ for _, option in ipairs(opts) do
+ if (option.kind == nil or option.required) and args[option.name] == nil then
+ print("error: missing argument '" .. option.name .. "'")
+ help(opts)
+ end
+ end
+
+ return args
+end
diff --git a/src/blueprint b/src/blueprint
new file mode 100755
index 0000000..ab0cba1
--- /dev/null
+++ b/src/blueprint
@@ -0,0 +1,16 @@
+#!/usr/bin/env lua
+
+function setup_package()
+ local str = debug.getinfo(2, "S").source:sub(2)
+ local path = str:match("(.*/)") or "./"
+ package.path = path .. '?.lua;'
+end
+
+setup_package()
+
+local args = require('arg') {
+ { name = "path" },
+
+ { name = "template", description = "some description", kind = "property", required = true },
+ { name = "flag", description = "other description", kind = "flag" },
+}