Skip to content

Commit 426424b

Browse files
committed
Improved CmdLine help message formatting
Before, the -h help flag wrote each option in its own line as follows: -opt1 This is my long winded help message which requires more space and makes it ugly [default option for -opt1] -myopt2 help for myopt2 [default option for -myopt2] These messages were extremely hard to read. This commit now breaks each option help message as follows: -opt1 This is my long winded help message which requires more space and makes it ugly [default option for -opt1] -myopt2 help for myopt2 [default option for -myopt2] This is done in a way to limit the help message to around 80 characters.
1 parent 9d30c3a commit 426424b

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

Diff for: CmdLine.lua

+37-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,34 @@ local function pad(str, sz)
88
return str .. string.rep(' ', sz-#str)
99
end
1010

11+
--[[
12+
This function takes a string, and inserts newlines wherever it exceeds a
13+
the given block_sz. It also pads each new line with pre_pad number of
14+
spaces.
15+
--]]
16+
local function block_txt(str, pre_pad, block_sz)
17+
local ret_str = ""
18+
local txt_width = block_sz - pre_pad
19+
local start_i = 0
20+
local end_i = 0
21+
22+
while end_i and str do
23+
-- start search for text ahead of previous last position
24+
start_i = end_i + 1
25+
-- newline if the string already has text
26+
if #ret_str > 0 then
27+
ret_str = ret_str .. "\n" .. string.rep(' ', pre_pad)
28+
end
29+
-- find the first non-space character
30+
local start_i = string.find(str, "[^%s]", start_i)
31+
-- find the first space character after allowed width is run over
32+
end_i = string.find(str, "%s", start_i + txt_width - 1)
33+
ret_str = ret_str .. string.sub(str, start_i, end_i)
34+
end
35+
36+
return ret_str
37+
end
38+
1139
function CmdLine:error(msg)
1240
print('')
1341
io.stderr:write(msg)
@@ -249,17 +277,24 @@ function CmdLine:help(arg)
249277
end
250278
end
251279

280+
-- set the allowable width of each option help
281+
local opt_str_width = 80
282+
if optsz > 70 then
283+
opt_str_width = optsz + 30
284+
end
285+
252286
-- second pass to print
253287
for _,option in ipairs(self.helplines) do
254288
if type(option) == 'table' then
255289
io.write(' ')
290+
local opt_help = block_txt(option.help, optsz+3, opt_str_width)
256291
if option.default ~= nil then -- it is an option
257292
io.write(pad(option.key, optsz))
258-
if option.help then io.write(' ' .. option.help) end
293+
if option.help then io.write(' ' .. opt_help) end
259294
io.write(' [' .. tostring(option.default) .. ']')
260295
else -- it is an argument
261296
io.write(pad('<' .. strip(option.key) .. '>', optsz))
262-
if option.help then io.write(' ' .. option.help) end
297+
if option.help then io.write(' ' .. opt_help) end
263298
end
264299
else
265300
io.write(option) -- just some additional help

0 commit comments

Comments
 (0)