2022-01-14 03:35:23 +00:00
|
|
|
-- Copyright (c) 2009 Aleksey Cheusov <vle@gmx.net>
|
|
|
|
-- Copyright info:
|
|
|
|
-- https://github.com/cheusov/lua-alt-getopt/blob/f495c21d6a203ab280603aa5799e636fb5651ae7/alt_getopt.lua#L3-L20
|
|
|
|
|
|
|
|
-- updated for neovim lua JIT by ray-x
|
|
|
|
|
2022-04-16 12:09:15 +00:00
|
|
|
local type, pairs, ipairs, os = type, pairs, ipairs, os
|
2022-01-14 03:35:23 +00:00
|
|
|
|
2023-01-17 22:20:11 +00:00
|
|
|
local utils = require('go.utils')
|
|
|
|
local log = utils.log
|
2022-04-16 12:09:15 +00:00
|
|
|
local alt_getopt = {}
|
2022-01-14 03:35:23 +00:00
|
|
|
|
|
|
|
local function convert_short2long(opts)
|
|
|
|
local ret = {}
|
|
|
|
|
|
|
|
for short_opt, accept_arg in opts:gmatch("(%w)(:?)") do
|
|
|
|
ret[short_opt] = #accept_arg
|
|
|
|
end
|
|
|
|
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
|
|
|
local function err_unknown_opt(opt)
|
2022-01-16 02:08:16 +00:00
|
|
|
-- vim.notify("Unknown option `-" .. (#opt > 1 and "-" or "") .. opt, vim.lsp.log_levels.INFO)
|
2022-01-14 03:35:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local function canonize(options, opt)
|
|
|
|
if not options[opt] then
|
|
|
|
err_unknown_opt(opt)
|
|
|
|
end
|
|
|
|
|
|
|
|
while type(options[opt]) == "string" do
|
|
|
|
opt = options[opt]
|
|
|
|
|
|
|
|
if not options[opt] then
|
|
|
|
err_unknown_opt(opt)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return opt
|
|
|
|
end
|
|
|
|
|
|
|
|
function alt_getopt.get_ordered_opts(arg, sh_opts, long_opts)
|
|
|
|
local i = 1
|
|
|
|
local count = 1
|
|
|
|
local opts = {}
|
|
|
|
local optarg = {}
|
|
|
|
|
|
|
|
local options = convert_short2long(sh_opts)
|
|
|
|
for k, v in pairs(long_opts) do
|
|
|
|
options[k] = v
|
|
|
|
end
|
|
|
|
|
2022-04-16 12:09:15 +00:00
|
|
|
local unparsed = {}
|
|
|
|
|
2022-01-14 03:35:23 +00:00
|
|
|
while i <= #arg do
|
|
|
|
local a = arg[i]
|
2023-01-17 22:20:11 +00:00
|
|
|
if type(a) ~= "string" then
|
|
|
|
log('failed to decode', type(a), a)
|
|
|
|
goto continue
|
|
|
|
end
|
2022-01-14 03:35:23 +00:00
|
|
|
if a == "--" then
|
|
|
|
i = i + 1
|
|
|
|
break
|
|
|
|
elseif a == "-" then
|
|
|
|
break
|
|
|
|
elseif a:sub(1, 2) == "--" then
|
|
|
|
local pos = a:find("=", 1, true)
|
|
|
|
|
|
|
|
if pos then
|
|
|
|
local opt = a:sub(3, pos - 1)
|
|
|
|
|
|
|
|
opt = canonize(options, opt)
|
|
|
|
|
|
|
|
if options[opt] == 0 then
|
|
|
|
vim.notify("Bad usage of option `" .. a, vim.lsp.log_levels.ERROR)
|
|
|
|
end
|
|
|
|
|
|
|
|
optarg[count] = a:sub(pos + 1)
|
|
|
|
opts[count] = opt
|
|
|
|
else
|
|
|
|
local opt = a:sub(3)
|
|
|
|
|
|
|
|
opt = canonize(options, opt)
|
|
|
|
|
|
|
|
if options[opt] == 0 then
|
|
|
|
opts[count] = opt
|
|
|
|
else
|
|
|
|
if i == #arg then
|
|
|
|
vim.notify("Missed value for option `" .. a, vim.lsp.log_levels.ERROR)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
optarg[count] = arg[i + 1]
|
|
|
|
opts[count] = opt
|
|
|
|
i = i + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
count = count + 1
|
|
|
|
elseif a:sub(1, 1) == "-" then
|
|
|
|
for j = 2, a:len() do
|
|
|
|
local opt = canonize(options, a:sub(j, j))
|
|
|
|
|
|
|
|
if options[opt] == 0 then
|
|
|
|
opts[count] = opt
|
|
|
|
count = count + 1
|
|
|
|
elseif a:len() == j then
|
|
|
|
if i == #arg then
|
|
|
|
vim.notify("Missed value for option `-" .. opt, vim.lsp.log_levels.ERROR)
|
|
|
|
end
|
|
|
|
|
|
|
|
optarg[count] = arg[i + 1]
|
|
|
|
opts[count] = opt
|
|
|
|
i = i + 1
|
|
|
|
count = count + 1
|
|
|
|
break
|
|
|
|
else
|
|
|
|
optarg[count] = a:sub(j + 1)
|
|
|
|
opts[count] = opt
|
|
|
|
count = count + 1
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
2022-04-16 12:09:15 +00:00
|
|
|
table.insert(unparsed, a)
|
2022-01-14 03:35:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
i = i + 1
|
2023-01-17 22:20:11 +00:00
|
|
|
::continue::
|
|
|
|
|
2022-01-14 03:35:23 +00:00
|
|
|
end
|
|
|
|
|
2022-04-16 12:09:15 +00:00
|
|
|
return opts, i, optarg, unparsed
|
2022-01-14 03:35:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function alt_getopt.get_opts(arg, sh_opts, long_opts)
|
|
|
|
local ret = {}
|
|
|
|
|
2022-04-16 12:09:15 +00:00
|
|
|
local opts, optind, optarg, unparsed = alt_getopt.get_ordered_opts(arg, sh_opts, long_opts)
|
2022-01-14 03:35:23 +00:00
|
|
|
for i, v in ipairs(opts) do
|
2023-01-17 22:20:11 +00:00
|
|
|
if optarg and optarg[i] then
|
2022-01-14 03:35:23 +00:00
|
|
|
ret[v] = optarg[i]
|
|
|
|
else
|
2023-01-17 22:20:11 +00:00
|
|
|
ret[v] = (optarg ~= nil)
|
2022-01-14 03:35:23 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-04-16 12:09:15 +00:00
|
|
|
return ret, optind, unparsed
|
2022-01-14 03:35:23 +00:00
|
|
|
end
|
|
|
|
|
2022-05-30 14:04:52 +00:00
|
|
|
function alt_getopt.rebuid_args(opts, reminder)
|
|
|
|
local ret = {}
|
|
|
|
|
|
|
|
for i, v in pairs(opts) do
|
|
|
|
if opts[i] == true then
|
|
|
|
table.insert(ret, "-" .. i)
|
|
|
|
else
|
|
|
|
table.insert(ret, "-" .. i)
|
|
|
|
table.insert(ret, v)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if not vim.tbl_isempty(reminder) then
|
|
|
|
vim.list_extend(ret, reminder)
|
|
|
|
end
|
|
|
|
if next(ret) == nil then
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
2022-01-14 03:35:23 +00:00
|
|
|
function test_arg(arg)
|
|
|
|
local long_opts = {
|
|
|
|
verbose = "v",
|
|
|
|
help = "h",
|
|
|
|
test = "t",
|
|
|
|
stop = "s",
|
|
|
|
restart = "r",
|
|
|
|
-- fake = 0,
|
|
|
|
-- len = 1,
|
|
|
|
tags = "g",
|
|
|
|
output = "o",
|
|
|
|
set_value = "S",
|
|
|
|
["set-output"] = "o",
|
|
|
|
}
|
|
|
|
|
|
|
|
local opts
|
2022-07-26 16:21:58 +00:00
|
|
|
local optarg, unparsed
|
2022-01-14 03:35:23 +00:00
|
|
|
local optind
|
2022-04-16 12:09:15 +00:00
|
|
|
arg = arg or { "-t", "-r", "-c", "path1", "-g", "unit,integration", "path" }
|
2022-05-10 01:03:15 +00:00
|
|
|
-- arg = arg or { "-t", "-r", "-c", "-g", "unit,integration" }
|
2022-04-16 12:09:15 +00:00
|
|
|
opts, optind, optarg, unparsed = alt_getopt.get_ordered_opts(arg, "cg:hvo:n:rS:st", long_opts)
|
2022-01-14 03:35:23 +00:00
|
|
|
|
|
|
|
print("ordered opts")
|
2022-03-07 06:09:42 +00:00
|
|
|
print(vim.inspect(opts))
|
|
|
|
print(vim.inspect(optind))
|
|
|
|
print(vim.inspect(optarg))
|
2022-04-16 12:09:15 +00:00
|
|
|
print(vim.inspect(unparsed))
|
2022-01-14 03:35:23 +00:00
|
|
|
print("ordered opts end")
|
|
|
|
for i, v in ipairs(opts) do
|
|
|
|
if optarg[i] then
|
2022-03-07 06:09:42 +00:00
|
|
|
print("option opts[i] " .. v .. ": " .. vim.inspect(optarg[i]))
|
2022-01-14 03:35:23 +00:00
|
|
|
else
|
2022-01-16 02:08:16 +00:00
|
|
|
print("option " .. v)
|
2022-01-14 03:35:23 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-04-16 12:09:15 +00:00
|
|
|
print("get_opts ")
|
|
|
|
optarg, optind, unparsed = alt_getopt.get_opts(arg, "cg:hVvo:n:rS:st", long_opts)
|
|
|
|
print("opts " .. vim.inspect(optarg))
|
|
|
|
print("optind " .. vim.inspect(optind))
|
|
|
|
print(vim.inspect(unparsed))
|
2022-01-14 03:35:23 +00:00
|
|
|
local fin_options = {}
|
|
|
|
for k, v in pairs(optarg) do
|
2022-01-16 02:08:16 +00:00
|
|
|
table.insert(fin_options, "fin-option " .. k .. ": " .. vim.inspect(v) .. "\n")
|
2022-01-14 03:35:23 +00:00
|
|
|
end
|
|
|
|
table.sort(fin_options)
|
|
|
|
|
2022-03-07 06:09:42 +00:00
|
|
|
print(table.concat(fin_options))
|
2022-01-14 03:35:23 +00:00
|
|
|
--
|
2022-03-07 06:09:42 +00:00
|
|
|
for i = optind, #arg do
|
|
|
|
print(string.format("ARGV [%s] = %s\n", i, arg[i]))
|
|
|
|
end
|
2022-01-14 03:35:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- test_arg()
|
|
|
|
--
|
|
|
|
-- print("test 2")
|
|
|
|
--
|
2022-03-07 06:09:42 +00:00
|
|
|
-- test_arg({ "-tr", "-c", "-g", "unit,integration" })
|
2022-01-14 03:35:23 +00:00
|
|
|
-- test_arg({ "--tags", "unit,integration", "--restart" })
|
2022-03-07 06:09:42 +00:00
|
|
|
-- test_arg({ "run", "restart" })
|
2022-01-16 02:08:16 +00:00
|
|
|
-- test_arg({ "--tags", "unit,integration", "-c", "--restart" })
|
2022-01-14 03:35:23 +00:00
|
|
|
|
|
|
|
return alt_getopt
|