Merge remote-tracking branch 'myk002/myk_argparse' into develop

develop
lethosor 2021-07-05 15:21:59 -04:00
commit 3e6cecbbc6
No known key found for this signature in database
GPG Key ID: 76A269552F4F58C1
6 changed files with 410 additions and 230 deletions

@ -55,6 +55,9 @@ changelog.txt uses a syntax similar to RST, with a few special sequences:
## Lua
- new string utility function: ``string:wrap(width)`` wraps a string at space-separated word boundaries
- new string utility function: ``string:trim()`` removes whitespace characters from the beginning and end of the string
- new string utility function: ``string:split(delimiter, plain)`` splits a string with the given delimiter and returns a table of substrings. if ``plain`` is specified and set to ``true``, ``delimiter`` is interpreted as a literal string instead of as a pattern (the default)
- new library: ``argparse`` is a collection of commandline argument processing functions
- ``gui.Painter``: fixed error when calling ``viewport()`` method
- ``gui.dwarfmode``: new function: ``enterSidebarMode(sidebar_mode, max_esc)`` which uses keypresses to get into the specified sidebar mode from whatever the current screen is
- `reveal`: now exposes ``unhideFlood(pos)`` functionality to Lua

@ -0,0 +1,207 @@
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 nonnegative integer; otherwise returns val
local function check_nonnegative_int(val, arg_name)
if not val or val < 0 or val ~= math.floor(val) then
arg_error(arg_name,
'expected non-negative 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. This
-- function also verifies that the coordinates are valid for the current map and
-- throws if they are not (unless <skip_validation> is set to true).
function coords(arg, arg_name, skip_validation)
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
if not skip_validation and not dfhack.maps.isValidTilePos(cursor) then
arg_error(arg_name, 'cursor coordinates not on current map!')
end
return cursor
end
local numbers = numberList(arg, arg_name, 3)
local pos = xyz2pos(check_nonnegative_int(numbers[1]),
check_nonnegative_int(numbers[2]),
check_nonnegative_int(numbers[3]))
if not skip_validation and not dfhack.maps.isValidTilePos(pos) then
arg_error('specified coordinates not on current map: "%s"', arg)
end
return pos
end
return _ENV

@ -1,7 +1,6 @@
local _ENV = mkmodule('utils')
local df = df
local getopt = require('3rdparty.alt_getopt')
-- Comparator function
function compare(a,b)
@ -559,123 +558,14 @@ function invert(tab)
return result
end
-- processArgs() and processArgsGetopt() have been moved to argparse.lua.
-- The 'require' statements are within the functions to avoid adding hard
-- dependencies to utils.lua (which could lead to circular dependency issues).
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
return require('argparse').processArgs(args, validArgs)
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
return require('argparse').processArgsGetopt(args, optionActions)
end
function fillTable(table1,table2)

@ -0,0 +1,195 @@
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()
mock.patch(dfhack.maps, "isValidTilePos", mock.func(true),
function()
expect.table_eq({x=0, y=4, z=3}, argparse.coords('0,4 , 3'),
'happy path')
expect.error_match('expected non%-negative 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)
mock.patch(dfhack.maps, "isValidTilePos", mock.func(false),
function()
expect.error_match('not on current map',
function() argparse.coords('0,4,300') end)
expect.table_eq({x=0, y=4, z=300},
argparse.coords('0,4,300', nil, true))
mock.patch(guidm, 'getCursorPos', mock.func({x=1, y=2, z=300}),
function()
expect.error_match('not on current map',
function() argparse.coords('here') end)
end)
mock.patch(guidm, 'getCursorPos', mock.func({x=1, y=2, z=300}),
function()
expect.table_eq({x=1, y=2, z=300},
argparse.coords('here', nil, true))
end)
end)
end

@ -55,106 +55,3 @@ function test.invert_overwrite()
expect.eq(i.a, 3)
end
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 = utils.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() utils.processArgsGetopt({}, {{handler=function() end}}) end)
expect.error_match('missing option letter',
function() utils.processArgsGetopt({}, {{'notoneletter'}}) end)
expect.error_match('missing option letter',
function() utils.processArgsGetopt({}, {{function() end}}) end)
expect.error_match('handler missing',
function() utils.processArgsGetopt({}, {{'r'}}) end)
end
function test.processArgsGetopt_parsing_errors()
expect.error_match('Unknown option',
function() utils.processArgsGetopt({'-abc'},
{{'a', handler=function() end}})
end,
'use undefined short option')
expect.error_match('Unknown option',
function() utils.processArgsGetopt({'--abc'},
{{'a', handler=function() end}})
end,
'use undefined long option')
expect.error_match('Bad usage',
function() utils.processArgsGetopt({'--ab=c'},
{{'a', 'ab', handler=function() end}})
end,
'pass value to param that does not take one')
expect.error_match('Missing value',
function() utils.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() utils.processArgsGetopt({'--ab'},
{{'a', 'ab', hasArg=true,
handler=function() end}})
end,
'fail to pass value to long param that requires one')
end

@ -23,18 +23,6 @@ function test.parse_gui_commandline()
expect.table_eq({auto_phase=true, name='blueprint', start={x=1,y=2,z=3}},
opts)
opts = {}
expect.error_match('invalid argument',
function() b.parse_gui_commandline(
opts, {'--cursor=-1,2,3'}) end,
'negative coordinate')
opts = {}
expect.error_match('invalid argument',
function() b.parse_gui_commandline(
opts, {'--cursor=1,b,3'}) end,
'non-numeric coordinate')
opts = {}
b.parse_gui_commandline(opts, {'imaname'})
expect.table_eq({auto_phase=true, name='imaname'}, opts)