From 98763319d1169ede18cb0470672cc7acb8f0d679 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Thu, 31 May 2018 17:20:12 +0100 Subject: [PATCH] [Project] Deprecate and remove getopt library --- lualib/getopt.lua | 50 ---------------------------------- lualib/rspamadm/confighelp.lua | 18 +++++++++--- lualib/rspamadm/fuzzy_stat.lua | 25 +++++++++++++++-- src/rspamadm/confighelp.c | 2 +- 4 files changed, 37 insertions(+), 58 deletions(-) delete mode 100644 lualib/getopt.lua diff --git a/lualib/getopt.lua b/lualib/getopt.lua deleted file mode 100644 index 20715c387..000000000 --- a/lualib/getopt.lua +++ /dev/null @@ -1,50 +0,0 @@ -local function insert_option(tab, name, value) - if tab[name] then - if type(tab[name]) == 'table' then - table.insert(tab[name], value) - else - local old_val = tab[name] - tab[name] = { - old_val, - value - } - end - else - tab[name] = value - end -end - -local function getopt(arg, options) - local tab = {} - for k, v in ipairs(arg) do - if string.sub(v, 1, 2) == "--" then - local x = string.find(v, "=", 1, true) - if x then insert_option(tab, string.sub(v, 3, x - 1), string.sub(v, x + 1)) - else tab[string.sub(v, 3)] = true - end - elseif string.sub(v, 1, 1) == "-" then - local y = 2 - local l = string.len(v) - local jopt - while (y <= l) do - jopt = string.sub(v, y, y) - if string.find(options, jopt, 1, true) then - if y < l then - insert_option(tab, jopt, string.sub(v, y + 1)) - y = l - else - insert_option(tab, jopt, arg[k + 1]) - end - else - tab[jopt] = true - end - y = y + 1 - end - end - end - return tab -end - -return { - getopt = getopt -} diff --git a/lualib/rspamadm/confighelp.lua b/lualib/rspamadm/confighelp.lua index 8f0fd2b7d..d477ff69b 100644 --- a/lualib/rspamadm/confighelp.lua +++ b/lualib/rspamadm/confighelp.lua @@ -1,4 +1,4 @@ -local opts = {} +local opts local known_attrs = { data = 1, example = 1, @@ -6,10 +6,20 @@ local known_attrs = { required = 1, default = 1, } - -local getopt = require "getopt" +local argparse = require "argparse" local ansicolors = require "ansicolors" +local parser = argparse() + :name "rspamadm confighelp" + :description "Shows help for the specified configuration options" + :help_description_margin(32) +parser:flag "--no-color" + :description "Disable coloured output" +parser:flag "--short" + :description "Show only option names" +parser:flag "--no-examples" + :description "Do not show examples (impied by --short)" + local function maybe_print_color(key) if not opts['no-color'] then return ansicolors.white .. key .. ansicolors.reset @@ -100,7 +110,7 @@ local function print_help(key, value, tabs) end return function(args, res) - opts = getopt.getopt(args, '') + opts = parser:parse(args) local sorted = sort_values(res) diff --git a/lualib/rspamadm/fuzzy_stat.lua b/lualib/rspamadm/fuzzy_stat.lua index 20945bd4a..45e13bb60 100644 --- a/lualib/rspamadm/fuzzy_stat.lua +++ b/lualib/rspamadm/fuzzy_stat.lua @@ -1,6 +1,27 @@ local util = require "rspamd_util" local opts = {} +local argparse = require "argparse" +local parser = argparse() + :name "rspamadm confighelp" + :description "Shows help for the specified configuration options" + :help_description_margin(32) +parser:flag "--no-ips" + :description "No IPs stats" +parser:flag "--no-keys" + :description "No keys stats" +parser:flag "--short" + :description "Short output mode" +parser:flag "-n --number" + :description "Disable numbers humanization" +parser:option "-s --sort" + :description "Sort order" + :convert { + matched = "matched", + errors = "errors", + ip = "ip" + } + local function add_data(target, src) for k,v in pairs(src) do if k ~= 'ips' then @@ -143,13 +164,11 @@ local function print_result(r) return print_num(r) end -local getopt = require "getopt" - return function(args, res) local res_ips = {} local res_databases = {} local wrk = res['workers'] - opts = getopt.getopt(args, '') + opts = parser:parse(args) if wrk then for _,pr in pairs(wrk) do diff --git a/src/rspamadm/confighelp.c b/src/rspamadm/confighelp.c index 08789a468..c77cb6091 100644 --- a/src/rspamadm/confighelp.c +++ b/src/rspamadm/confighelp.c @@ -69,7 +69,7 @@ rspamadm_confighelp_help (gboolean full_help, const struct rspamadm_command *cmd "-j: output pretty formatted JSON\n" "-k: search by keyword in doc string\n" "-P: use specific Lua plugins path\n" - "--no-color: show colored output\n" + "--no-color: disable coloured output\n" "--short: show only option names\n" "--no-examples: do not show examples (impied by --short)\n" "--help: shows available options and commands"; -- 2.39.5