If an option with a default value is underused, invoke it more

This commit is contained in:
mpeterv
2014-02-20 12:48:51 +04:00
parent e99e047c4d
commit 3e6dc54201
2 changed files with 19 additions and 3 deletions

View File

@@ -42,6 +42,15 @@ describe("tests related to default values", function()
assert.same({foo = "bar"}, args) assert.same({foo = "bar"}, args)
end) end)
it("handles underused option with default value correctly", function()
local parser = Parser()
parser:option "-o" "--output"
:count(1)
:default "a.out"
local args = parser:parse{}
assert.same({output = "a.out"}, args)
end)
it("doesn't use default if option is not invoked", function() it("doesn't use default if option is not invoked", function()
local parser = Parser() local parser = Parser()
parser:option("-f", "--foo", { parser:option("-f", "--foo", {

View File

@@ -761,9 +761,16 @@ function Parser:_parse(args, errhandler)
end end
for _, option in ipairs(options) do for _, option in ipairs(options) do
assert_(invocations[option] >= option._mincount, if invocations[option] < option._mincount then
"option '%s' must be used at least %d times", option._name, option._mincount if option._default then
) while invocations[option] < option._mincount do
invoke(option)
close(option)
end
else
error_("option '%s' must be used at least %d times", option._name, option._mincount)
end
end
end end
for _, callback in ipairs(com_callbacks) do for _, callback in ipairs(com_callbacks) do