-
Notifications
You must be signed in to change notification settings - Fork 464
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add types to argparse and alt_getopt #4497
Changes from all commits
d49e29b
83a47bf
3704b99
03ea9e7
09dcc8f
a559449
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,6 +45,9 @@ | |
|
||
local _ENV = mkmodule('3rdparty.alt_getopt') | ||
|
||
---@nodiscard | ||
---@param opts string | ||
---@return table<string, string|integer> | ||
local function get_opt_map(opts) | ||
local i = 1 | ||
local len = #opts | ||
|
@@ -57,11 +60,16 @@ local function get_opt_map(opts) | |
return options | ||
end | ||
|
||
---@param opt string|integer | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. opt can only be a string at this point (but I see that the more permissive type might be needed because it can be string|integer in other contexts) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As above, I believe. |
||
local function err_unknown_opt(opt) | ||
qerror(string.format('Unknown option "-%s%s"', #opt > 1 and '-' or '', opt)) | ||
end | ||
|
||
-- resolve aliases into their canonical forms | ||
---@nodiscard | ||
---@param options table<string, string|integer> | ||
---@param opt string|integer | ||
---@return integer | ||
local function canonicalize(options, opt) | ||
if not options[opt] then | ||
err_unknown_opt(opt) | ||
|
@@ -80,14 +88,26 @@ local function canonicalize(options, opt) | |
'Option "%s" resolves to non-number for has_arg flag', opt)) | ||
end | ||
|
||
---@type integer | ||
return opt | ||
end | ||
|
||
---@nodiscard | ||
---@param options table<string, string|integer> | ||
---@param opt string|integer | ||
---@return boolean | ||
local function has_arg(options, opt) | ||
return options[canonicalize(options, opt)] == 1 | ||
end | ||
|
||
-- returns vectors for opts, optargs, and nonoptions | ||
---@nodiscard | ||
---@param args string[] e.g, { ... } | ||
---@param sh_opts string e.g., 'ak:hv' | ||
---@param long_opts table<string, string|integer> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. isn't this a table<string, string>? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
---@return string[] opts | ||
---@return string[] optargs | ||
---@return string[] nonoptions | ||
function get_ordered_opts(args, sh_opts, long_opts) | ||
local optind, count, opts, optargs, nonoptions = 1, 1, {}, {}, {} | ||
|
||
|
@@ -159,7 +179,14 @@ end | |
|
||
-- returns a map of options to their optargs (or 1 if the option doesn't take an | ||
-- argument), and a vector for nonoptions | ||
---@nodiscard | ||
---@param args string[] | ||
---@param sh_opts string | ||
---@param long_opts string[] | ||
---@return table<string, string|integer> | ||
---@return string[] | ||
function get_opts(args, sh_opts, long_opts) | ||
---@type table<string, string|integer> | ||
local ret = {} | ||
|
||
local opts,optargs,nonoptions = get_ordered_opts(args, sh_opts, long_opts) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,10 @@ local _ENV = mkmodule('argparse') | |
|
||
local getopt = require('3rdparty.alt_getopt') | ||
|
||
---@nodiscard | ||
---@param args string[] Most commonly `{ ... }` | ||
---@param validArgs table<string, integer> Use `utils.invert` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the value of this table just has to be truthy. maybe use boolean instead of integer There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we do that the return value of Just to be clear: ---@param truthy boolean
function foo(truthy) end
local bar = foo("") -- Shows a warning
local caz = foo(1) -- Shows a warning |
||
---@return table<string, string|string[]> | ||
function processArgs(args, validArgs) | ||
local result = {} | ||
local argName | ||
|
@@ -58,25 +62,34 @@ function processArgs(args, validArgs) | |
return result | ||
end | ||
|
||
---@class argparse.OptionAction | ||
---@field [1] string|nil Short option (eg. "q") | ||
---@field [2] string|nil Long option (eg. "quiet") | ||
---@field handler fun(optarg?: string) | ||
---@field hasArg boolean|nil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we have to specify |nil here? nil is just a falsey boolean There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
-- See online docs for full usage info. | ||
-- | ||
-- Quick example: | ||
-- | ||
-- local args = {...} | ||
-- local open_readonly, filename = false, nil -- set defaults | ||
-- local args = {...} | ||
-- local open_readonly, filename = false, nil -- set defaults | ||
-- | ||
-- local positionals = argparse.processArgsGetopt(args, { | ||
-- {'r', handler=function() open_readonly = true end}, | ||
-- {'f', 'filename', hasArg=true, | ||
-- handler=function(optarg) filename = optarg end} | ||
-- }) | ||
-- local positionals = argparse.processArgsGetopt(args, { | ||
-- {'r', handler=function() open_readonly = true end}, | ||
-- {'f', 'filename', hasArg=true, | ||
-- handler=function(optarg) filename = optarg end} | ||
-- }) | ||
-- | ||
-- In this example, if args is {'first', '-rf', 'fname', 'second'} or, | ||
-- equivalently, {'first', '-r', '--filename', 'myfile.txt', 'second'} (note the | ||
-- double dash in front of the long option alias), then: | ||
-- open_readonly == true | ||
-- filename == 'myfile.txt' | ||
-- positionals == {'first', 'second'}. | ||
---@param args string[] Most commonly `{ ... }` | ||
---@param optionActions argparse.OptionAction[] | ||
---@return string[] positionals # Positional arguments | ||
function processArgsGetopt(args, optionActions) | ||
local sh_opts, long_opts = '', {} | ||
local handlers = {} | ||
|
@@ -120,6 +133,9 @@ function processArgsGetopt(args, optionActions) | |
return nonoptions | ||
end | ||
|
||
---@param arg_name? string | ||
---@param fmt string | ||
---@param ... any | ||
local function arg_error(arg_name, fmt, ...) | ||
local prefix = '' | ||
if arg_name and #arg_name > 0 then | ||
|
@@ -128,6 +144,11 @@ local function arg_error(arg_name, fmt, ...) | |
qerror(('%s'..fmt):format(prefix, ...)) | ||
end | ||
|
||
---@nodiscard | ||
---@param arg string | ||
---@param arg_name? string | ||
---@param list_length? integer | ||
---@return string[] | ||
function stringList(arg, arg_name, list_length) | ||
if not list_length then list_length = 0 end | ||
local list = arg and (arg):split(',') or {} | ||
|
@@ -141,18 +162,29 @@ function stringList(arg, arg_name, list_length) | |
return list | ||
end | ||
|
||
---@nodiscard | ||
---@param arg string | ||
---@param arg_name? string | ||
---@param list_length? integer | ||
---@return integer[] | ||
function numberList(arg, arg_name, list_length) | ||
local strings = stringList(arg, arg_name, list_length) | ||
---@type integer[] | ||
local numbers = {} | ||
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 | ||
numbers[i] = num | ||
end | ||
return strings | ||
return numbers | ||
end | ||
|
||
---@nodiscard | ||
---@param arg string|integer | ||
---@param arg_name? string | ||
---@return integer | ||
function positiveInt(arg, arg_name) | ||
local val = tonumber(arg) | ||
if not val or val <= 0 or val ~= math.floor(val) then | ||
|
@@ -162,6 +194,10 @@ function positiveInt(arg, arg_name) | |
return val | ||
end | ||
|
||
---@nodiscard | ||
---@param arg string|integer | ||
---@param arg_name? string | ||
---@return integer | ||
function nonnegativeInt(arg, arg_name) | ||
local val = tonumber(arg) | ||
if not val or val < 0 or val ~= math.floor(val) then | ||
|
@@ -171,6 +207,11 @@ function nonnegativeInt(arg, arg_name) | |
return val | ||
end | ||
|
||
---@nodiscard | ||
---@param arg string|'here' | ||
---@param arg_name? string | ||
---@param skip_validation? boolean | ||
---@return df.coord | ||
function coords(arg, arg_name, skip_validation) | ||
if arg == 'here' then | ||
local guidm = require('gui.dwarfmode') -- globals may not be available yet | ||
|
@@ -198,6 +239,10 @@ end | |
|
||
local toBool={["true"]=true,["yes"]=true,["y"]=true,["on"]=true,["1"]=true, | ||
["false"]=false,["no"]=false,["n"]=false,["off"]=false,["0"]=false} | ||
---@nodiscard | ||
---@param arg string | ||
---@param arg_name? string | ||
---@return boolean | ||
function boolean(arg, arg_name) | ||
local arg_lower = string.lower(arg) | ||
if toBool[arg_lower] == nil then | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
table value is always an integer (but I see that the more permissive type might be needed because it can be string|integer in other contexts)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah this is one of a few "just has to be truthy" fields.