elua: remove getopt-like interface from getopt.lua, use descs directly instead, and...
authorDaniel Kolesa <quaker66@gmail.com>
Thu, 24 Apr 2014 15:04:37 +0000 (16:04 +0100)
committerDaniel Kolesa <d.kolesa@samsung.com>
Tue, 10 Jun 2014 14:48:48 +0000 (15:48 +0100)
src/bin/elua/lualian.lua
src/bin/elua/modules/getopt.lua

index 5ec07a1..1af2412 100644 (file)
@@ -25,7 +25,7 @@ local arg_parser = {
     }
 }
 
-local opts, args = getopt.parse_desc(arg_parser)
+local opts, args = getopt.parse(arg_parser)
 
 if not opts then
     io.stderr:write(args, "\n")
@@ -41,26 +41,26 @@ local printv = function() end
 
 for i, opt in ipairs(opts) do
     local on = opt[1]
-    if on == "-h" or on == "--help" then
+    if on == "h" then
         getopt.help(arg_parser, io.stdout)
         return
     end
-    if on == "-l" or on == "--license" then
+    if on == "l" then
         print("Copyright (C) 2014 Daniel \"q66\" Kolesa, available under the "
            .. "terms of the MIT license.")
         return
     end
-    if on == "-v" or on == "--verbose" then
+    if on == "v" then
         printv = print
-    elseif on == "-I" or on == "--include" then
+    elseif on == "I" then
         include_dirs[#include_dirs + 1] = opt[2]
-    elseif on == "-L" or on == "--library" then
+    elseif on == "L" then
         libname = opt[2]
-    elseif on == "-M" or on == "--module" then
+    elseif on == "M" then
         modname = opt[2]
-    elseif on == "-P" or on == "--prefix" then
+    elseif on == "P" then
         cprefix = opt[2]
-    elseif on == "-o" or on == "--output" then
+    elseif on == "o" then
         output_files[#output_files + 1] = opt[2]
     end
 end
index a479cb4..48f469a 100644 (file)
@@ -2,30 +2,24 @@
 
 local M = {}
 
-local hasval_l = function(opt, long)
-    for i, v in ipairs(long) do
-        if v:sub(1, #opt) == opt then
-            local rest = v:sub(#opt + 1)
-            if #rest == 0 then
-                return false, opt
-            elseif rest == "=" then
-                return true, opt
-            end
+local get_desc = function(opt, j, descs)
+    for i, v in ipairs(descs) do
+        if v[j] == opt then
+            return v
         end
     end
     error("option --" .. opt .. " not recognized", 4)
 end
 
-local parse_l = function(opts, opt, long, args)
+local parse_l = function(opts, opt, descs, args)
     local optval
     local i = opt:find("=")
     if i then
         opt, optval = opt:sub(1, i - 1), opt:sub(i + 1)
     end
 
-    local has_val
-    hasval, opt = hasval_l(opt, long)
-    if hasval then
+    local desc = get_desc(opt, 2, descs)
+    if desc[3] then
         if not optval then
             if #args == 0 then
                 error("option --" .. opt .. " requires an argument", 3)
@@ -35,23 +29,17 @@ local parse_l = function(opts, opt, long, args)
     elseif optval then
         error("option --" .. opt .. " cannot have an argument", 3)
     end
-    opts[#opts + 1] = { "--" .. opt, optval }
+    opts[#opts + 1] = { desc.alias or desc[1] or desc[2], optval }
     return opts, args
 end
 
-local hasval_s = function(opt, short)
-    if short:find(opt, 1, true) then
-        return not not short:find(opt .. ":", 1, true)
-    end
-    error("option -" .. opt .. " not recognized", 4)
-end
-
-local parse_s = function(opts, optstr, short, args)
+local parse_s = function(opts, optstr, descs, args)
     while optstr ~= "" do
         local optval
         local opt = optstr:sub(1, 1)
         optstr = optstr:sub(2)
-        if hasval_s(opt, short) then
+        local desc = get_desc(opt, 1, descs)
+        if desc[3] then
             if optstr == "" then
                 if #args == 0 then
                     error("option -" .. opt .. " requires an argument", 3)
@@ -60,34 +48,33 @@ local parse_s = function(opts, optstr, short, args)
             end
             optval, optstr = optstr, ""
         end
-        opts[#opts + 1] = { "-" .. opt, optval }
+        opts[#opts + 1] = { desc.alias or desc[1] or desc[2], optval }
     end
     return opts, args
 end
 
-local getopt_u = function(args, short, long)
-    local opts = {}
-    if type(long) == "string" then
-        long = { long }
-    end
+local getopt_u  = function(parser)
+    local args  = parser.args
+    local descs = parser.descs
+    local opts  = {}
     while args and #args > 0 and args[1]:sub(1, 1) == "-" and args[1] ~= "-" do
         if args[1] == "--" then
             args = { unpack(args, 2) }
             break
         end
         if args[1]:sub(1, 2) == "--" then
-            opts, args = parse_l(opts, args[1]:sub(3), long,
+            opts, args = parse_l(opts, args[1]:sub(3), descs,
                 { unpack(args, 2) })
         else
-            opts, args = parse_s(opts, args[1]:sub(2), short,
+            opts, args = parse_s(opts, args[1]:sub(2), descs,
                 { unpack(args, 2) })
         end
     end
     return opts, args
 end
 
-M.parse = function(args, short, long)
-    local ret, opts, args = pcall(getopt_u, args, short, long)
+M.parse = function(parser)
+    local ret, opts, args = pcall(getopt_u, parser)
     if not ret then
         return nil, opts
     end
@@ -95,23 +82,6 @@ M.parse = function(args, short, long)
 end
 local parse = M.parse
 
-M.parse_desc = function(parser)
-    local args = parser.args
-    local short, long = {}, {}
-    for i, desc in ipairs(parser.descs) do
-        if desc[1] then short[#short + 1] = desc[1] end
-        local buf
-        if desc[2] then buf = desc[2] end
-        if desc[3] then
-            if buf then buf = buf .. "=" end
-            short[#short + 1] = ":"
-        end
-        if buf then long[#long + 1] = buf end
-    end
-    short = table.concat(short)
-    return parse(args, short, long)
-end
-
 M.help = function(parser, f)
     f = f or io.stderr
     local usage = parser.usage