Fixed usage messages in subcommands corrupted after several usages

This commit is contained in:
mpeterv
2014-03-01 16:41:14 +04:00
parent 8cefeb1ef7
commit 73467e2836
2 changed files with 21 additions and 2 deletions

View File

@@ -103,6 +103,23 @@ describe("tests related to usage message generation", function()
) )
end) end)
it("usage messages for commands are correct after several :prepare() invocations", function()
local parser = Parser "foo"
:add_help(false)
parser:flag "-q" "--quiet"
local run = parser:command "run"
:add_help(false)
run:option "--where"
parser:prepare()
parser:prepare()
assert.equal(
[=[Usage: foo run [--where <where>]]=],
run:prepare():get_usage()
)
end)
describe("usage generation can be customized", function() describe("usage generation can be customized", function()
it("uses message provided by user", function() it("uses message provided by user", function()
local parser = Parser "foo" local parser = Parser "foo"

View File

@@ -267,6 +267,8 @@ function Parser:command(...)
end end
function Parser:prepare() function Parser:prepare()
self._fullname = self._fullname or self._name
if self._add_help and not self._help_option then if self._add_help and not self._help_option then
self._help_option = self:flag(self._add_help) self._help_option = self:flag(self._add_help)
:action(function() :action(function()
@@ -295,7 +297,7 @@ function Parser:prepare()
for _, command in ipairs(self._commands) do for _, command in ipairs(self._commands) do
command._target = command._target or command._name command._target = command._target or command._name
command._name = self._name .. " " .. command._name command._fullname = self._fullname .. " " .. command._name
end end
return self return self
@@ -322,7 +324,7 @@ local usage_welcome = "Usage: "
function Parser:get_usage() function Parser:get_usage()
if not self._usage then if not self._usage then
local lines = {usage_welcome .. self._name} local lines = {usage_welcome .. self._fullname}
local function add(s) local function add(s)
if #lines[#lines]+1+#s <= max_usage_width then if #lines[#lines]+1+#s <= max_usage_width then