diff --git a/spec/usage_spec.lua b/spec/usage_spec.lua index d3eaf00..ca0bcfb 100644 --- a/spec/usage_spec.lua +++ b/spec/usage_spec.lua @@ -103,6 +103,23 @@ describe("tests related to usage message generation", function() ) 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 ]]=], + run:prepare():get_usage() + ) + end) + describe("usage generation can be customized", function() it("uses message provided by user", function() local parser = Parser "foo" diff --git a/src/argparse.lua b/src/argparse.lua index ff83e4f..9070991 100644 --- a/src/argparse.lua +++ b/src/argparse.lua @@ -267,6 +267,8 @@ function Parser:command(...) end function Parser:prepare() + self._fullname = self._fullname or self._name + if self._add_help and not self._help_option then self._help_option = self:flag(self._add_help) :action(function() @@ -295,7 +297,7 @@ function Parser:prepare() for _, command in ipairs(self._commands) do command._target = command._target or command._name - command._name = self._name .. " " .. command._name + command._fullname = self._fullname .. " " .. command._name end return self @@ -322,7 +324,7 @@ local usage_welcome = "Usage: " function Parser:get_usage() if not self._usage then - local lines = {usage_welcome .. self._name} + local lines = {usage_welcome .. self._fullname} local function add(s) if #lines[#lines]+1+#s <= max_usage_width then