From 38b6eb139234b65057e558aba9643e26ff214eb2 Mon Sep 17 00:00:00 2001 From: mpeterv Date: Sat, 31 Oct 2015 19:19:56 +0300 Subject: [PATCH] Add :init() property Allows setting initial stored value. Can be also set using :default() with a non-string argument. --- spec/actions_spec.lua | 18 ++++++++++++++++++ src/argparse.lua | 41 +++++++++++++++++++++++++++++++---------- 2 files changed, 49 insertions(+), 10 deletions(-) diff --git a/spec/actions_spec.lua b/spec/actions_spec.lua index 57b58ca..4128dae 100644 --- a/spec/actions_spec.lua +++ b/spec/actions_spec.lua @@ -105,6 +105,24 @@ describe("actions", function() assert.same({foo = false}, args) end) + it("for options allow setting initial stored value", function() + local parser = Parser() + parser:flag("--no-foo"):target("foo"):action("store_false"):init(true) + parser:flag("--no-bar"):target("bar"):action("store_false"):init(true) + + local args = parser:parse{"--no-foo"} + assert.same({foo = false, bar = true}, args) + end) + + it("for options allow setting initial stored value as non-string argument to default", function() + local parser = Parser() + parser:flag("--no-foo", "Foo the bar.", true):target("foo"):action("store_false") + parser:flag("--no-bar", "Bar the foo.", true):target("bar"):action("store_false") + + local args = parser:parse{"--no-foo"} + assert.same({foo = false, bar = true}, args) + end) + it("pass overwrite flag as the fourth argument", function() local parser = Parser() local overwrites = {} diff --git a/src/argparse.lua b/src/argparse.lua index c46019f..1d4a44c 100644 --- a/src/argparse.lua +++ b/src/argparse.lua @@ -187,6 +187,18 @@ local option_action = {"action", function(_, value) end end} +local option_init = {"init", function(self) + self._has_init = true +end} + +local option_default = {"default", function(self, value) + if type(value) ~= "string" then + self._init = value + self._has_init = true + return true + end +end} + local add_help = {"add_help", function(self, value) typecheck("add_help", {"boolean", "string", "table"}, value) @@ -262,14 +274,15 @@ local Argument = class({ args = 5, typechecked("name", "string"), typechecked("description", "string"), - typechecked("default", "string"), + option_default, typechecked("convert", "function", "table"), boundaries("args"), typechecked("target", "string"), typechecked("defmode", "string"), typechecked("show_default", "boolean"), typechecked("argname", "string", "table"), - option_action + option_action, + option_init }) local Option = class({ @@ -280,7 +293,7 @@ local Option = class({ args = 6, multiname, typechecked("description", "string"), - typechecked("default", "string"), + option_default, typechecked("convert", "function", "table"), boundaries("args"), boundaries("count"), @@ -289,7 +302,8 @@ local Option = class({ typechecked("show_default", "boolean"), typechecked("overwrite", "boolean"), typechecked("argname", "string", "table"), - option_action + option_action, + option_init }, Argument) function Argument:_get_argument_list() @@ -362,23 +376,30 @@ function actions.append(result, target, argument, overwrite) end function Argument:_get_action() - local action = self._action - local init + local action, init if self._maxcount == 1 then if self._maxargs == 0 then - action, init = action or "store_true", nil + action, init = "store_true", nil else - action, init = action or "store", nil + action, init = "store", nil end else if self._maxargs == 0 then - action, init = action or "count", 0 + action, init = "count", 0 else - action, init = action or "append", {} + action, init = "append", {} end end + if self._action then + action = self._action + end + + if self._has_init then + init = self._init + end + if type(action) == "string" then action = actions[action] end