From 3e6dc542010182c3c4baa401bb7be11d071c2dbd Mon Sep 17 00:00:00 2001 From: mpeterv Date: Thu, 20 Feb 2014 12:48:51 +0400 Subject: [PATCH] If an option with a default value is underused, invoke it more --- spec/default_spec.lua | 9 +++++++++ src/argparse.lua | 13 ++++++++++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/spec/default_spec.lua b/spec/default_spec.lua index 777cc1a..770d59e 100644 --- a/spec/default_spec.lua +++ b/spec/default_spec.lua @@ -42,6 +42,15 @@ describe("tests related to default values", function() assert.same({foo = "bar"}, args) 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() local parser = Parser() parser:option("-f", "--foo", { diff --git a/src/argparse.lua b/src/argparse.lua index 66e9694..d649a19 100644 --- a/src/argparse.lua +++ b/src/argparse.lua @@ -761,9 +761,16 @@ function Parser:_parse(args, errhandler) end for _, option in ipairs(options) do - assert_(invocations[option] >= option._mincount, - "option '%s' must be used at least %d times", option._name, option._mincount - ) + if invocations[option] < option._mincount then + 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 for _, callback in ipairs(com_callbacks) do