address review comments

develop
myk002 2021-01-12 23:42:53 -08:00
parent 4300c185d7
commit 39d274b00e
No known key found for this signature in database
GPG Key ID: 8A39CA0FA0C16E78
2 changed files with 15 additions and 7 deletions

@ -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

@ -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