Skip to content

Commit e633ad7

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 e633ad7

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

CmdLine.lua

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,35 @@ 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+
if not start_i then break end
32+
-- find the first space character after allowed width is run over
33+
end_i = string.find(str, "%s", start_i + txt_width - 1)
34+
ret_str = ret_str .. string.sub(str, start_i, end_i)
35+
end
36+
37+
return ret_str
38+
end
39+
1140
function CmdLine:error(msg)
1241
print('')
1342
io.stderr:write(msg)
@@ -249,17 +278,24 @@ function CmdLine:help(arg)
249278
end
250279
end
251280

281+
-- set the allowable width of each option help
282+
local opt_str_width = 80
283+
if optsz > 70 then
284+
opt_str_width = optsz + 30
285+
end
286+
252287
-- second pass to print
253288
for _,option in ipairs(self.helplines) do
254289
if type(option) == 'table' then
255290
io.write(' ')
291+
local opt_help = block_txt(option.help, optsz+3, opt_str_width)
256292
if option.default ~= nil then -- it is an option
257293
io.write(pad(option.key, optsz))
258-
if option.help then io.write(' ' .. option.help) end
294+
if option.help then io.write(' ' .. opt_help) end
259295
io.write(' [' .. tostring(option.default) .. ']')
260296
else -- it is an argument
261297
io.write(pad('<' .. strip(option.key) .. '>', optsz))
262-
if option.help then io.write(' ' .. option.help) end
298+
if option.help then io.write(' ' .. opt_help) end
263299
end
264300
else
265301
io.write(option) -- just some additional help

0 commit comments

Comments
 (0)