Skip to content

Commit a1f0cf9

Browse files
committed
Refactor saitama.lua: Use argparse and remove dead code
1 parent b4e9f69 commit a1f0cf9

1 file changed

Lines changed: 10 additions & 43 deletions

File tree

saitama.lua

Lines changed: 10 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -43,32 +43,16 @@ Examples::
4343
4444
]====]
4545

46-
-- Manual arg parsing to support: --squad 1 100 (flag + value + positional)
47-
local raw_args = {...}
46+
local argparse = require('argparse')
4847
local args = {}
49-
local positional = {}
50-
local i = 1
51-
while i <= #raw_args do
52-
local v = raw_args[i]
53-
if v == '--help' or v == '-help' then
54-
args.help = true
55-
elseif v == '--all' or v == '-all' then
56-
args.all = true
57-
elseif v == '--citizens' or v == '-citizens' then
58-
args.citizens = true
59-
elseif v == '--listsquads' or v == '-listsquads' then
60-
args.listsquads = true
61-
elseif v == '--squad' or v == '-squad' then
62-
i = i + 1
63-
args.squad = raw_args[i]
64-
elseif v == '--unit' or v == '-unit' then
65-
i = i + 1
66-
args.unit = raw_args[i]
67-
else
68-
table.insert(positional, v)
69-
end
70-
i = i + 1
71-
end
48+
local positional = argparse.processArgsGetopt({ ... }, {
49+
{'h', 'help', handler=function() args.help = true end},
50+
{'a', 'all', handler=function() args.all = true end},
51+
{'c', 'citizens', handler=function() args.citizens = true end},
52+
{'l', 'listsquads', handler=function() args.listsquads = true end},
53+
{'s', 'squad', hasArg=true, handler=function(optarg) args.squad = optarg end},
54+
{'u', 'unit', hasArg=true, handler=function(optarg) args.unit = optarg end},
55+
})
7256

7357
if args.help then
7458
print(dfhack.script_help())
@@ -153,24 +137,7 @@ local function list_squads()
153137
end
154138
end
155139

156-
-- ---------------------------------------------------------------------------
157-
-- Parse multiplier from remaining args
158-
-- ---------------------------------------------------------------------------
159-
local function get_multiplier(raw_args)
160-
-- The multiplier is the last positional argument
161-
local val = nil
162-
for _, v in ipairs(raw_args) do
163-
local n = tonumber(v)
164-
if n then val = n end
165-
end
166-
if not val then
167-
qerror('No multiplier provided. Usage: saitama <multiplier>')
168-
end
169-
if val < 1 then
170-
qerror('Multiplier must be >= 1.')
171-
end
172-
return math.floor(val)
173-
end
140+
174141

175142
-- ---------------------------------------------------------------------------
176143
-- Main

0 commit comments

Comments
 (0)