aboutsummaryrefslogtreecommitdiff
path: root/opt_parser.lua
diff options
context:
space:
mode:
authorAlexander Pickering <alex@cogarr.net>2020-07-05 17:18:56 -0400
committerAlexander Pickering <alex@cogarr.net>2020-07-05 17:18:56 -0400
commite87b06ee0fe2a588b72a356bbb8378899365d626 (patch)
tree0d0acd945a70644835e8958425e6d5e6c10196a6 /opt_parser.lua
parentd2ba262c5307aa14c325ef53d8e4e56a5ece0376 (diff)
downloadmdoc-e87b06ee0fe2a588b72a356bbb8378899365d626.tar.gz
mdoc-e87b06ee0fe2a588b72a356bbb8378899365d626.tar.bz2
mdoc-e87b06ee0fe2a588b72a356bbb8378899365d626.zip
Add rockspec
Add a rockspec and move the files around so that luarocks can install it correctly
Diffstat (limited to 'opt_parser.lua')
-rw-r--r--opt_parser.lua188
1 files changed, 0 insertions, 188 deletions
diff --git a/opt_parser.lua b/opt_parser.lua
deleted file mode 100644
index d99c804..0000000
--- a/opt_parser.lua
+++ /dev/null
@@ -1,188 +0,0 @@
-
-local lfs = require("lfs")
-require("ext")
-local ret = {}
-
-ret.options = {
- paths = {
- type = "folder",
- multiple = true,
- short = "-p",
- long = "--path",
- consumes = 1,
- },
- output = {
- type = "folder",
- short = "-o",
- long = "--output",
- consumes = 1,
- default = ".",
- },
- title = {
- type = "string",
- short = "-t",
- long = "--tile",
- consumes = 1,
- default = "Mdoc Generated Page",
- },
- index = {
- type = "file",
- short = "-i",
- long = "--index",
- consumes = 1,
- },
- document_paths = {
- type = "folder",
- multiple = true,
- short = "-d",
- long = "--document",
- consumes = 1,
- },
- parser = {
- type = "executable",
- short = "-m",
- long = "--markup-parser",
- consumes = 1,
- default = "markdown",
- },
- --append = {
- --type = "file",
- --short = "-a",
- --long = "--append",
- --consumes = 1,
- --multiple = true,
- --},
- verbose = {
- type = "flag",
- short = "-v",
- long = "--verbose"
- },
- help = {
- type = "flag",
- short = "-h",
- long = "--help",
- }
-}
-
-local function check_file_type(path, type)
- local attr, err = lfs.attributes(path)
- if attr == nil then
- return attr, err
- end
- if attr.mode == type then
- return path
- else
- return false, string.format("%s was not a folder, it was a %s",path, attr.mode)
- end
-end
-
-ret.check_flag = function(...)
- return true
-end
-
-ret.check_folder = function(path)
- if path:match("/$") then
- path = path:sub(1,-2)
- end
- return check_file_type(path,"directory")
-end
-
-ret.check_string = function(str)
- if type(str) == "string" then
- return str
- else
- return false, string.format("%s was not a string, it was a %s",tostring(str), type(str))
- end
-end
-
-ret.check_file = function(path)
- return check_file_type(path,"file")
-end
-
-ret.check_executable = function(name)
- local tmpname = "./" .. os.tmpname()
- local pd = assert(io.popen(name .. " > " .. tmpname, "w"))
- pd:write("Hello, world!")
- pd:close()
- local id = assert(io.open(tmpname,"r"))
- local dat = id:read("*a")
- id:close()
- assert(os.remove(tmpname))
- if string.len(dat) > 0 then
- return name
- else
- return false, string.format("Tried to execute %q, but it did not create any data with the input 'Hello, world!', are you sure it's an executale on your PATH?", name)
- end
-end
-
-local option_lookup = {}
-for k,v in pairs(ret.options) do
- if v.short then
- option_lookup[v.short] = k
- end
- if v.long then
- option_lookup[v.long] = k
- end
-end
-
-ret.parse_options = function(args)
- print("parsing args:",args)
- local parsed = {}
- local i = 1
- while i <= #args do
- local option_name = option_lookup[args[i]]
- print("found option:",option_name)
- if not option_name then
- errorf("Unknown option #%d: %q",i, args[i])
- end
- local option = ret.options[option_name]
- assertf(#args > i + (option.consumes or 0) - 1, "Option #%d (%q) consumes %d arguments, but found end of arguments",i,args[i],option.consumes)
- local args_for_option = {}
- for j = i, i+(option.consumes or 0) do
- table.insert(args_for_option,args[j+1])
- end
- --special, if we only consume 1 option, just pass that.
- if option.consumes == 1 then
- args_for_option = args_for_option[1]
- end
- print("checking",option.type)
- print("with",args_for_option)
- local check = assert(ret["check_" .. option.type](args_for_option))
- if option.multiple then
- parsed[option_name] = parsed[option_name] or {}
- table.insert(parsed[option_name],check)
- else
- parsed[option_name] = check
- end
- i = i + 1 + (option.consumes or 0)
- end
- --Set defaults for things that don't have them yet
- for option_name, option in pairs(ret.options) do
- if parsed[option_name] == nil then
- if option.multiple and not option.default then
- parsed[option_name] = {}
- else
- parsed[option_name] = option.default
- end
- end
- end
- return parsed
-end
-
-ret.help = function()
- print([=[
-mdoc - lua documentation
-mdoc -p <folder> [-p <folder> ...][ -o <folder>][ -t "title"][ -i <file>][ -d <folder>[ -d <folder> ...]][ -m <executable>][ -h]
-
- -p | --path <folder> : Path to search for source files
- -o | --output <folder> = "." : Folder to output HTML files to (and a cache folder)
- -t | --title "name" = "Mdoc Generated Page" : Title for the html files
- -i | --index <file> : File to use for the index file
- -d | --document <folder> : Path to search for files to put inder the References section
- -m | --markup-parser <executable> : Executable to use to parse the descriptions and refrence documents.
- Executable should accept a file path as it's argument, and generate html as it's output.
- -h | --help : print this help
-]=])
-end
-
-return ret