aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.envrc1
-rw-r--r--.gitignore1
-rw-r--r--flake.lock27
-rw-r--r--flake.nix37
-rw-r--r--src/arg.lua126
-rwxr-xr-xsrc/blueprint16
6 files changed, 208 insertions, 0 deletions
diff --git a/.envrc b/.envrc
new file mode 100644
index 0000000..3550a30
--- /dev/null
+++ b/.envrc
@@ -0,0 +1 @@
+use flake
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..9b42106
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+.direnv/
diff --git a/flake.lock b/flake.lock
new file mode 100644
index 0000000..2130399
--- /dev/null
+++ b/flake.lock
@@ -0,0 +1,27 @@
+{
+ "nodes": {
+ "nixpkgs": {
+ "locked": {
+ "lastModified": 1776169885,
+ "narHash": "sha256-l/iNYDZ4bGOAFQY2q8y5OAfBBtrDAaPuRQqWaFHVRXM=",
+ "owner": "nixos",
+ "repo": "nixpkgs",
+ "rev": "4bd9165a9165d7b5e33ae57f3eecbcb28fb231c9",
+ "type": "github"
+ },
+ "original": {
+ "owner": "nixos",
+ "ref": "nixos-unstable",
+ "repo": "nixpkgs",
+ "type": "github"
+ }
+ },
+ "root": {
+ "inputs": {
+ "nixpkgs": "nixpkgs"
+ }
+ }
+ },
+ "root": "root",
+ "version": 7
+}
diff --git a/flake.nix b/flake.nix
new file mode 100644
index 0000000..bc3b622
--- /dev/null
+++ b/flake.nix
@@ -0,0 +1,37 @@
+{
+ description = "A very basic flake";
+
+ inputs = {
+ nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
+ };
+
+ outputs = { self, nixpkgs }:
+ let
+ pkgs = import nixpkgs { system = "x86_64-linux"; };
+ luap = pkgs.lua54Packages;
+ inputs = [
+ luap.lua
+ luap.inspect
+ ];
+ in
+ {
+
+ packages.x86_64-linux.blueprint = pkgs.stdenv.mkDerivation {
+ name = "blueprint";
+ src = self;
+
+ installPhase = ''
+ mkdir -p $out/bin;
+ install ./src/** $out/bin
+ '';
+
+ buildInputs = inputs;
+ };
+
+ packages.x86_64-linux.default = self.packages.x86_64-linux.blueprint;
+
+ devShells.x86_64-linux.default = pkgs.mkShell {
+ packages = inputs;
+ };
+ };
+}
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" },
+}