From 39d274b00e7474033c2e6f644ca18486f1009364 Mon Sep 17 00:00:00 2001 From: myk002 Date: Tue, 12 Jan 2021 23:42:53 -0800 Subject: [PATCH] address review comments --- LICENSE.rst | 2 ++ library/lua/utils.lua | 20 +++++++++++++------- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/LICENSE.rst b/LICENSE.rst index f1e07d111..48bcfbe95 100644 --- a/LICENSE.rst +++ b/LICENSE.rst @@ -34,6 +34,7 @@ tinythread_ Zlib \(c\) 2010, Marcus Geelnard tinyxml_ Zlib \(c\) 2000-2006, Lee Thomason UTF-8-decoder_ MIT \(c\) 2008-2010, Bjoern Hoehrmann xlsxio_ MIT \(c\) 2016-2020, Brecht Sanders +alt-getopt_ MIT \(c\) 2009 Aleksey Cheusov =============== ============= ================================================= .. _DFHack: https://github.com/DFHack/dfhack @@ -52,6 +53,7 @@ xlsxio_ MIT \(c\) 2016-2020, Brecht Sanders .. _tinyxml: http://www.sourceforge.net/projects/tinyxml .. _UTF-8-decoder: http://bjoern.hoehrmann.de/utf-8/decoder/dfa .. _xlsxio: https://github.com/brechtsanders/xlsxio +.. _alt-getopt: https://github.com/LuaDist/alt-getopt .. _CC-BY-SA: http://creativecommons.org/licenses/by/3.0/deed.en_US diff --git a/library/lua/utils.lua b/library/lua/utils.lua index ee2cadd29..c652e54d4 100644 --- a/library/lua/utils.lua +++ b/library/lua/utils.lua @@ -1,6 +1,7 @@ local _ENV = mkmodule('utils') local df = df +local getopt = require('3rdparty.alt_getopt') -- Comparator function function compare(a,b) @@ -614,7 +615,10 @@ function processArgs(args, validArgs) end -- processes commandline options according to optionActions and returns all --- argument strings that are not options. +-- argument strings that are not options. Options and non-option strings can +-- appear in any order, and single-letter options that do not take arguments +-- can be combined into a single option string (e.g. '-abc' is the same as +-- '-a -b -c' if options 'a' and 'b' do not take arguments. -- -- optionActions is a vector with elements in the following format: -- {shortOptionName, longOptionAlias, hasArg=boolean, handler=fn} @@ -627,15 +631,17 @@ end -- -- local filename = nil -- local open_readonly = false --- local nonoptions = processArgs2(args, { +-- local nonoptions = processArgsGetopt(args, { -- {'r', handler=function() open_readonly = true end}, -- {'f', 'filename', hasArg=true, -- handler=function(optarg) filename = optarg end} -- }) -- --- when args is {'first', '-f', 'fname', 'second'} --- then filename will be fname and nonoptions will contain {'first', 'second'} -function processArgs2(args, optionActions) +-- when args is {'first', '-f', 'fname', 'second'} or, equivalently, +-- {'first', '--filename', 'fname', 'second'} (note the double dash in front of +-- the long option alias), then filename will be fname and nonoptions will +-- contain {'first', 'second'}. +function processArgsGetopt(args, optionActions) local sh_opts, long_opts = '', {} local handlers = {} for _,optionAction in ipairs(optionActions) do @@ -654,8 +660,8 @@ function processArgs2(args, optionActions) handlers[long_opt] = optionAction.handler end end - local getopt = require('3rdparty.alt_getopt').get_ordered_opts - local opts, optargs, nonoptions = getopt(args, sh_opts, long_opts) + local opts, optargs, nonoptions = + getopt.get_ordered_opts(args, sh_opts, long_opts) for i,v in ipairs(opts) do handlers[v](optargs[i]) end