add argparse library functions

develop
myk002 2021-07-02 23:11:17 -07:00
parent f9627a0cfb
commit 9535ff82bb
No known key found for this signature in database
GPG Key ID: 8A39CA0FA0C16E78
2 changed files with 370 additions and 0 deletions

@ -0,0 +1,198 @@
local _ENV = mkmodule('argparse')
local getopt = require('3rdparty.alt_getopt')
local guidm = require('gui.dwarfmode')
function processArgs(args, validArgs)
--[[
standardized argument processing for scripts
-argName value
-argName [list of values]
-argName [list of [nested values] -that can be [whatever] format of matched square brackets]
-arg1 \-arg3
escape sequences
--]]
local result = {}
local argName
local bracketDepth = 0
for i,arg in ipairs(args) do
if argName then
if arg == '[' then
if bracketDepth > 0 then
table.insert(result[argName], arg)
end
bracketDepth = bracketDepth+1
elseif arg == ']' then
bracketDepth = bracketDepth-1
if bracketDepth > 0 then
table.insert(result[argName], arg)
else
argName = nil
end
elseif string.sub(arg,1,1) == '\\' then
if bracketDepth == 0 then
result[argName] = string.sub(arg,2)
argName = nil
else
table.insert(result[argName], string.sub(arg,2))
end
else
if bracketDepth == 0 then
result[argName] = arg
argName = nil
else
table.insert(result[argName], arg)
end
end
elseif string.sub(arg,1,1) == '-' then
argName = string.sub(arg,2)
if validArgs and not validArgs[argName] then
error('error: invalid arg: ' .. i .. ': ' .. argName)
end
if result[argName] then
error('duplicate arg: ' .. i .. ': ' .. argName)
end
if i+1 > #args or string.sub(args[i+1],1,1) == '-' then
result[argName] = ''
argName = nil
else
result[argName] = {}
end
else
error('error parsing arg ' .. i .. ': ' .. arg)
end
end
return result
end
-- processes commandline options according to optionActions and returns all
-- 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.
--
-- Numbers cannot be options and negative numbers (e.g. -10) will be interpreted
-- as positional parameters and returned in the nonoptions list.
--
-- optionActions is a vector with elements in the following format:
-- {shortOptionName, longOptionAlias, hasArg=boolean, handler=fn}
-- shortOptionName and handler are required. If the option takes an argument,
-- it will be passed to the handler function.
-- longOptionAlias is optional.
-- hasArgument defaults to false.
--
-- example usage:
--
-- local filename = nil
-- local open_readonly = false
-- 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'} 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
local sh_opt,long_opt = optionAction[1], optionAction[2]
if not sh_opt or type(sh_opt) ~= 'string' or #sh_opt ~= 1 then
error('optionAction missing option letter at index 1')
end
if not optionAction.handler then
error(string.format('handler missing for option "%s"', sh_opt))
end
sh_opts = sh_opts .. sh_opt
if optionAction.hasArg then sh_opts = sh_opts .. ':' end
handlers[sh_opt] = optionAction.handler
if long_opt then
long_opts[long_opt] = sh_opt
handlers[long_opt] = optionAction.handler
end
end
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
return nonoptions
end
local function arg_error(arg_name, fmt, ...)
local prefix = ''
if arg_name and #arg_name > 0 then
prefix = arg_name .. ': '
end
qerror(('%s'..fmt):format(prefix, ...))
end
-- Parses a comma-separated sequence of strings and returns a lua list. Spaces
-- are trimmed from the strings. If <arg_name> is specified, it is used to make
-- error messages more useful. If <list_length> is specified and greater than 0,
-- exactly that number of elements must be found or the function will error.
-- Example:
-- stringSequence('hello , world,list', 'words') => {'hello', 'world', 'list'}
function stringList(arg, arg_name, list_length)
if not list_length then list_length = 0 end
local list = arg:split(',')
if list_length > 0 and #list ~= list_length then
arg_error(arg_name,
'expected %d elements; found %d', list_length, #list)
end
for i,element in ipairs(list) do
list[i] = element:trim()
end
return list
end
-- Parses a comma-separated sequence of numeric strings and returns a list of
-- the discovered numbers (as numbers, not strings). If <arg_name> is specified,
-- it is used to make error messages more useful. If <list_length> is specified
-- and greater than 0, exactly that number of elements must be found or the
-- function will error. Example:
-- numericSequence('10, -20 , 30.5') => {10, -20, 30.5}
function numberList(arg, arg_name, list_length)
local strings = stringList(arg, arg_name, list_length)
for i,str in ipairs(strings) do
local num = tonumber(str)
if not num then
arg_error(arg_name, 'invalid number: "%s"', str)
end
strings[i] = num
end
return strings
end
-- throws if val is not a positive integer; otherwise returns val
local function check_positive_int(val, arg_name)
if not val or val <= 0 or val ~= math.floor(val) then
arg_error(arg_name,
'expected positive integer; got "%s"', tostring(val))
end
return val
end
-- Parses a comma-separated coordinate string and returns a coordinate table of
-- {x=x, y=y, z=z}. If the string 'here' is passed, returns the coordinates of
-- the active game cursor, or throws an error if the cursor is not active.
function coords(arg, arg_name)
if arg == 'here' then
local cursor = guidm.getCursorPos()
if not cursor then
arg_error(arg_name,
'"here" was specified for coordinates, but the game' ..
' cursor is not active!')
end
return cursor
end
local numbers = numberList(arg, arg_name, 3)
return xyz2pos(check_positive_int(numbers[1]),
check_positive_int(numbers[2]),
check_positive_int(numbers[3]))
end
return _ENV

@ -0,0 +1,172 @@
local argparse = require('argparse')
local guidm = require('gui.dwarfmode')
function test.processArgsGetopt_happy_path()
local quiet, verbose, name
local function process(args, expected_q, expected_v, expected_n)
quiet, verbose, name = false, false, nil
local nonoptions = argparse.processArgsGetopt(args, {
{'q', handler=function() quiet = true end},
{'v', 'verbose', handler=function() verbose = true end},
{'n', 'name', hasArg=true,
handler=function(optarg) name = optarg end},
})
expect.eq(expected_q, quiet)
expect.eq(expected_v, verbose)
expect.eq(expected_n, name)
return nonoptions
end
local args = {}
expect.table_eq({}, process(args, false, false, nil))
args = {'-q'}
expect.table_eq({}, process(args, true, false, nil))
args = {'-v'}
expect.table_eq({}, process(args, false, true, nil))
args = {'--verbose'}
expect.table_eq({}, process(args, false, true, nil))
args = {'-n', 'foo'}
expect.table_eq({}, process(args, false, false, 'foo'))
args = {'-n', 'foo'}
expect.table_eq({}, process(args, false, false, 'foo'))
args = {'-nfoo'}
expect.table_eq({}, process(args, false, false, 'foo'))
args = {'--name', 'foo'}
expect.table_eq({}, process(args, false, false, 'foo'))
args = {'--name=foo'}
expect.table_eq({}, process(args, false, false, 'foo'))
args = {'-vqnfoo'}
expect.table_eq({}, process(args, true, true, 'foo'))
args = {'nonopt1', '-nfoo', 'nonopt2', '-1', '-10', '-0v'}
expect.table_eq({'nonopt1', 'nonopt2', '-1', '-10', '-0v'},
process(args, false, false, 'foo'))
args = {'nonopt1', '--', '-nfoo', '--nonopt2', 'nonopt3'}
expect.table_eq({'nonopt1', '-nfoo', '--nonopt2', 'nonopt3'},
process(args, false, false, nil))
end
function test.processArgsGetopt_action_errors()
expect.error_match('missing option letter',
function()
argparse.processArgsGetopt({}, {{handler=function() end}})
end)
expect.error_match('missing option letter',
function() argparse.processArgsGetopt({}, {{'notoneletter'}}) end)
expect.error_match('missing option letter',
function() argparse.processArgsGetopt({}, {{function() end}}) end)
expect.error_match('handler missing',
function() argparse.processArgsGetopt({}, {{'r'}}) end)
end
function test.processArgsGetopt_parsing_errors()
expect.error_match('Unknown option',
function() argparse.processArgsGetopt({'-abc'},
{{'a', handler=function() end}})
end,
'use undefined short option')
expect.error_match('Unknown option',
function() argparse.processArgsGetopt({'--abc'},
{{'a', handler=function() end}})
end,
'use undefined long option')
expect.error_match('Bad usage',
function() argparse.processArgsGetopt({'--ab=c'},
{{'a', 'ab', handler=function() end}})
end,
'pass value to param that does not take one')
expect.error_match('Missing value',
function() argparse.processArgsGetopt({'-a'},
{{'a', 'ab', hasArg=true,
handler=function() end}})
end,
'fail to pass value to short param that requires one')
expect.error_match('Missing value',
function() argparse.processArgsGetopt({'--ab'},
{{'a', 'ab', hasArg=true,
handler=function() end}})
end,
'fail to pass value to long param that requires one')
end
function test.stringList()
expect.table_eq({'happy', 'path'}, argparse.stringList(' happy , path'),
'ensure elements are trimmed')
expect.table_eq({'empty', '', 'elem'}, argparse.stringList('empty,,elem'),
'ensure empty elements are preserved')
expect.error_match('expected 5 elements',
function() argparse.stringList('a,b,c,d', '', 5) end,
'too few elements')
expect.error_match('expected 5 elements',
function() argparse.stringList('a,b,c,d,e,f', '', 5) end,
'too many elements')
expect.error_match('^expected',
function() argparse.stringList('', '', 5) end,
'no arg name printed when none supplied')
expect.error_match('^argname',
function() argparse.stringList('', 'argname', 5) end,
'arg name printed when supplied')
end
function test.numberList()
expect.table_eq({5, 4, 0, -1, 0.5, -0.5},
argparse.numberList(' 5,4, 0, -1 , 000.5000, -0.50'),
'happy path')
expect.error_match('invalid number',
function() argparse.numberList('1,b,3') end,
'letter not number')
expect.error_match('invalid number',
function() argparse.numberList('1,,3') end,
'blank number')
expect.error_match('invalid number',
function() argparse.numberList('1,2,') end,
'blank number at end')
expect.error_match('invalid number',
function() argparse.numberList('1-1') end,
'bad number format')
expect.error_match('^expected',
function() argparse.numberList('', '', 5) end,
'no arg name printed when none supplied')
expect.error_match('^argname',
function() argparse.numberList('', 'argname', 5) end,
'arg name printed when supplied')
end
function test.coords()
expect.table_eq({x=5, y=4, z=3}, argparse.coords('5,4 , 3'), 'happy path')
expect.error_match('expected positive integer',
function() argparse.coords('1,-2,3') end,
'negative coordinate')
mock.patch(guidm, 'getCursorPos', mock.func({x=1, y=2, z=3}),
function()
expect.table_eq({x=1, y=2, z=3}, argparse.coords('here'))
end)
mock.patch(guidm, 'getCursorPos', mock.func(),
function()
expect.error_match('cursor is not active',
function() argparse.coords('here') end,
'inactive cursor')
end)
end