Skip to content
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

Merged
merged 6 commits into from May 9, 2024
Merged

Conversation

vallode
Copy link
Contributor

@vallode vallode commented Apr 21, 2024

This PR adds type annotations for argparse (and to alt_getopt for further support) and a few odd functions that were beneficial.

  • If modifying alt_getopt is not wanted, we can do without the changes there
  • alt_getopt has dead code in the get_opts function, optargs are unused
  • getCursorPos uses the df-structures global even though it doesn't really fit there, this raises a topic about creating separate types for things like coords that can be re-used outside of the context of structures
  • I've chosen to namespace classes in modules ---@class argparse.class and will likely extend that to the structures, as well as other modules, in the future.

@vallode vallode marked this pull request as ready for review April 23, 2024 11:53
@vallode vallode changed the title WIP: add types to argparse and alt_getopt.lua Add types to argparse and alt_getopt Apr 23, 2024
@@ -36,6 +36,7 @@ function getPanelLayout()
}
end

---@return global.T_cursor|nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it doesn't actually return a T_cursor, it returns a standard Lua table with the same fields as T_cursor.

@@ -57,11 +60,16 @@ local function get_opt_map(opts)
return options
end

---@param opt string|integer
Copy link
Member

Choose a reason for hiding this comment

The 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)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above, I believe.

@@ -45,6 +45,9 @@

local _ENV = mkmodule('3rdparty.alt_getopt')

---@nodiscard
---@param opts string
---@return table<string, string|integer>
Copy link
Member

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)

Copy link
Contributor Author

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.

---@nodiscard
---@param args string[] e.g, { ... }
---@param sh_opts string e.g., 'ak:hv'
---@param long_opts table<string, string|integer>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't this a table<string, string>?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

processArgsGetopt assigns a truthy 1 or 0 to it. So I guess it kind of like the other "just has to be a truthy" fields.

---@param args string[]
---@param sh_opts string
---@param long_opts string[]
---@return table
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

specifically, table<string, string|integer>, right?

@@ -153,6 +180,10 @@ function numberList(arg, arg_name, list_length)
return strings
end

---@nodiscard
---@param arg integer
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

arg is string|integer (but is likely to be a string)

---@nodiscard
---@param arg integer
---@param arg_name? string
---@return number
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

specifically an integer, if that matters

@@ -162,6 +193,10 @@ function positiveInt(arg, arg_name)
return val
end

---@nodiscard
---@param arg integer
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

string|integer

---@nodiscard
---@param arg integer
---@param arg_name? string
---@return number
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

integer

---@param arg string|'here'
---@param arg_name? string
---@param skip_validation? boolean
---@return global.T_cursor|coord
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

always a coord, never a T_cursor

@vallode vallode requested a review from myk002 May 8, 2024 17:53
@myk002 myk002 merged commit a731f1b into DFHack:develop May 9, 2024
14 checks passed
@vallode vallode deleted the argparse-types branch May 9, 2024 06:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Status: Done
Development

Successfully merging this pull request may close these issues.

None yet

2 participants