|
|
@ -1,17 +1,11 @@
|
|
|
|
|
|
|
|
-- Docs at https://docs.dfhack.org/en/stable/docs/Lua%20API.html#argparse
|
|
|
|
|
|
|
|
|
|
|
|
local _ENV = mkmodule('argparse')
|
|
|
|
local _ENV = mkmodule('argparse')
|
|
|
|
|
|
|
|
|
|
|
|
local getopt = require('3rdparty.alt_getopt')
|
|
|
|
local getopt = require('3rdparty.alt_getopt')
|
|
|
|
local guidm = require('gui.dwarfmode')
|
|
|
|
local guidm = require('gui.dwarfmode')
|
|
|
|
|
|
|
|
|
|
|
|
function processArgs(args, validArgs)
|
|
|
|
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 result = {}
|
|
|
|
local argName
|
|
|
|
local argName
|
|
|
|
local bracketDepth = 0
|
|
|
|
local bracketDepth = 0
|
|
|
@ -29,7 +23,7 @@ function processArgs(args, validArgs)
|
|
|
|
else
|
|
|
|
else
|
|
|
|
argName = nil
|
|
|
|
argName = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
elseif string.sub(arg,1,1) == '\\' then
|
|
|
|
elseif arg:startswith('\\') then
|
|
|
|
if bracketDepth == 0 then
|
|
|
|
if bracketDepth == 0 then
|
|
|
|
result[argName] = string.sub(arg,2)
|
|
|
|
result[argName] = string.sub(arg,2)
|
|
|
|
argName = nil
|
|
|
|
argName = nil
|
|
|
@ -44,59 +38,46 @@ function processArgs(args, validArgs)
|
|
|
|
table.insert(result[argName], arg)
|
|
|
|
table.insert(result[argName], arg)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
elseif string.sub(arg,1,1) == '-' then
|
|
|
|
elseif arg:startswith('-') then
|
|
|
|
argName = string.sub(arg,2)
|
|
|
|
argName = string.sub(arg, arg:startswith('--') and 3 or 2)
|
|
|
|
if validArgs and not validArgs[argName] then
|
|
|
|
if validArgs and not validArgs[argName] then
|
|
|
|
error('error: invalid arg: ' .. i .. ': ' .. argName)
|
|
|
|
qerror('error: invalid arg: ' .. i .. ': ' .. argName)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if result[argName] then
|
|
|
|
if result[argName] then
|
|
|
|
error('duplicate arg: ' .. i .. ': ' .. argName)
|
|
|
|
qerror('duplicate arg: ' .. i .. ': ' .. argName)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if i+1 > #args or string.sub(args[i+1],1,1) == '-' then
|
|
|
|
if i+1 > #args or args[i+1]:startswith('-') then
|
|
|
|
result[argName] = ''
|
|
|
|
result[argName] = ''
|
|
|
|
argName = nil
|
|
|
|
argName = nil
|
|
|
|
else
|
|
|
|
else
|
|
|
|
result[argName] = {}
|
|
|
|
result[argName] = {}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
else
|
|
|
|
error('error parsing arg ' .. i .. ': ' .. arg)
|
|
|
|
qerror('error parsing arg ' .. i .. ': ' .. arg)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return result
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- processes commandline options according to optionActions and returns all
|
|
|
|
-- See online docs for full usage info.
|
|
|
|
-- 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:
|
|
|
|
-- Quick example:
|
|
|
|
-- {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.
|
|
|
|
|
|
|
|
-- hasArg defaults to false.
|
|
|
|
|
|
|
|
-- To have an option that has only a long form, pass nil or '' as the
|
|
|
|
|
|
|
|
-- shortOptionName.
|
|
|
|
|
|
|
|
--
|
|
|
|
--
|
|
|
|
-- example usage:
|
|
|
|
-- local args = {...}
|
|
|
|
|
|
|
|
-- local open_readonly, filename = false, nil -- set defaults
|
|
|
|
--
|
|
|
|
--
|
|
|
|
-- local filename = nil
|
|
|
|
-- local positionals = argparse.processArgsGetopt(args, {
|
|
|
|
-- local open_readonly = false
|
|
|
|
|
|
|
|
-- local nonoptions = processArgsGetopt(args, {
|
|
|
|
|
|
|
|
-- {'r', handler=function() open_readonly = true end},
|
|
|
|
-- {'r', handler=function() open_readonly = true end},
|
|
|
|
-- {'f', 'filename', hasArg=true,
|
|
|
|
-- {'f', 'filename', hasArg=true,
|
|
|
|
-- handler=function(optarg) filename = optarg end}
|
|
|
|
-- handler=function(optarg) filename = optarg end}
|
|
|
|
-- })
|
|
|
|
-- })
|
|
|
|
--
|
|
|
|
--
|
|
|
|
-- when args is {'first', '-f', 'fname', 'second'} or, equivalently,
|
|
|
|
-- In this example, if args is {'first', '-rf', 'fname', 'second'} or,
|
|
|
|
-- {'first', '--filename', 'fname', 'second'} (note the double dash in front of
|
|
|
|
-- equivalently, {'first', '-r', '--filename', 'myfile.txt', 'second'} (note the
|
|
|
|
-- the long option alias), then filename will be fname and nonoptions will
|
|
|
|
-- double dash in front of the long option alias), then:
|
|
|
|
-- contain {'first', 'second'}.
|
|
|
|
-- open_readonly == true
|
|
|
|
|
|
|
|
-- filename == 'myfile.txt'
|
|
|
|
|
|
|
|
-- positionals == {'first', 'second'}.
|
|
|
|
function processArgsGetopt(args, optionActions)
|
|
|
|
function processArgsGetopt(args, optionActions)
|
|
|
|
local sh_opts, long_opts = '', {}
|
|
|
|
local sh_opts, long_opts = '', {}
|
|
|
|
local handlers = {}
|
|
|
|
local handlers = {}
|
|
|
@ -148,12 +129,6 @@ local function arg_error(arg_name, fmt, ...)
|
|
|
|
qerror(('%s'..fmt):format(prefix, ...))
|
|
|
|
qerror(('%s'..fmt):format(prefix, ...))
|
|
|
|
end
|
|
|
|
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:
|
|
|
|
|
|
|
|
-- stringList('hello , world,list', 'words') => {'hello', 'world', 'list'}
|
|
|
|
|
|
|
|
function stringList(arg, arg_name, list_length)
|
|
|
|
function stringList(arg, arg_name, list_length)
|
|
|
|
if not list_length then list_length = 0 end
|
|
|
|
if not list_length then list_length = 0 end
|
|
|
|
local list = arg:split(',')
|
|
|
|
local list = arg:split(',')
|
|
|
@ -167,12 +142,6 @@ function stringList(arg, arg_name, list_length)
|
|
|
|
return list
|
|
|
|
return list
|
|
|
|
end
|
|
|
|
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:
|
|
|
|
|
|
|
|
-- numberList('10, -20 , 30.5') => {10, -20, 30.5}
|
|
|
|
|
|
|
|
function numberList(arg, arg_name, list_length)
|
|
|
|
function numberList(arg, arg_name, list_length)
|
|
|
|
local strings = stringList(arg, arg_name, list_length)
|
|
|
|
local strings = stringList(arg, arg_name, list_length)
|
|
|
|
for i,str in ipairs(strings) do
|
|
|
|
for i,str in ipairs(strings) do
|
|
|
@ -194,11 +163,6 @@ local function check_nonnegative_int(val, arg_name)
|
|
|
|
return val
|
|
|
|
return val
|
|
|
|
end
|
|
|
|
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)
|
|
|
|
function coords(arg, arg_name, skip_validation)
|
|
|
|
if arg == 'here' then
|
|
|
|
if arg == 'here' then
|
|
|
|
local cursor = guidm.getCursorPos()
|
|
|
|
local cursor = guidm.getCursorPos()
|
|
|
|