Skip to content

Commit

Permalink
Improved CmdLine help message formatting
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ahmadh84 committed Jul 3, 2017
1 parent 9d30c3a commit e633ad7
Showing 1 changed file with 38 additions and 2 deletions.
40 changes: 38 additions & 2 deletions CmdLine.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,35 @@ local function pad(str, sz)
return str .. string.rep(' ', sz-#str)
end

--[[
This function takes a string, and inserts newlines wherever it exceeds a
the given block_sz. It also pads each new line with pre_pad number of
spaces.
--]]
local function block_txt(str, pre_pad, block_sz)
local ret_str = ""
local txt_width = block_sz - pre_pad
local start_i = 0
local end_i = 0

while end_i and str do
-- start search for text ahead of previous last position
start_i = end_i + 1
-- newline if the string already has text
if #ret_str > 0 then
ret_str = ret_str .. "\n" .. string.rep(' ', pre_pad)
end
-- find the first non-space character
local start_i = string.find(str, "[^%s]", start_i)
if not start_i then break end
-- find the first space character after allowed width is run over
end_i = string.find(str, "%s", start_i + txt_width - 1)
ret_str = ret_str .. string.sub(str, start_i, end_i)
end

return ret_str
end

function CmdLine:error(msg)
print('')
io.stderr:write(msg)
Expand Down Expand Up @@ -249,17 +278,24 @@ function CmdLine:help(arg)
end
end

-- set the allowable width of each option help
local opt_str_width = 80
if optsz > 70 then
opt_str_width = optsz + 30
end

-- second pass to print
for _,option in ipairs(self.helplines) do
if type(option) == 'table' then
io.write(' ')
local opt_help = block_txt(option.help, optsz+3, opt_str_width)
if option.default ~= nil then -- it is an option
io.write(pad(option.key, optsz))
if option.help then io.write(' ' .. option.help) end
if option.help then io.write(' ' .. opt_help) end
io.write(' [' .. tostring(option.default) .. ']')
else -- it is an argument
io.write(pad('<' .. strip(option.key) .. '>', optsz))
if option.help then io.write(' ' .. option.help) end
if option.help then io.write(' ' .. opt_help) end
end
else
io.write(option) -- just some additional help
Expand Down

0 comments on commit e633ad7

Please sign in to comment.