mirror of
https://github.com/TangentFoxy/argparse.git
synced 2025-07-28 02:52:20 +00:00
Add :init() property
Allows setting initial stored value. Can be also set using :default() with a non-string argument.
This commit is contained in:
@@ -105,6 +105,24 @@ describe("actions", function()
|
|||||||
assert.same({foo = false}, args)
|
assert.same({foo = false}, args)
|
||||||
end)
|
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()
|
it("pass overwrite flag as the fourth argument", function()
|
||||||
local parser = Parser()
|
local parser = Parser()
|
||||||
local overwrites = {}
|
local overwrites = {}
|
||||||
|
@@ -187,6 +187,18 @@ local option_action = {"action", function(_, value)
|
|||||||
end
|
end
|
||||||
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)
|
local add_help = {"add_help", function(self, value)
|
||||||
typecheck("add_help", {"boolean", "string", "table"}, value)
|
typecheck("add_help", {"boolean", "string", "table"}, value)
|
||||||
|
|
||||||
@@ -262,14 +274,15 @@ local Argument = class({
|
|||||||
args = 5,
|
args = 5,
|
||||||
typechecked("name", "string"),
|
typechecked("name", "string"),
|
||||||
typechecked("description", "string"),
|
typechecked("description", "string"),
|
||||||
typechecked("default", "string"),
|
option_default,
|
||||||
typechecked("convert", "function", "table"),
|
typechecked("convert", "function", "table"),
|
||||||
boundaries("args"),
|
boundaries("args"),
|
||||||
typechecked("target", "string"),
|
typechecked("target", "string"),
|
||||||
typechecked("defmode", "string"),
|
typechecked("defmode", "string"),
|
||||||
typechecked("show_default", "boolean"),
|
typechecked("show_default", "boolean"),
|
||||||
typechecked("argname", "string", "table"),
|
typechecked("argname", "string", "table"),
|
||||||
option_action
|
option_action,
|
||||||
|
option_init
|
||||||
})
|
})
|
||||||
|
|
||||||
local Option = class({
|
local Option = class({
|
||||||
@@ -280,7 +293,7 @@ local Option = class({
|
|||||||
args = 6,
|
args = 6,
|
||||||
multiname,
|
multiname,
|
||||||
typechecked("description", "string"),
|
typechecked("description", "string"),
|
||||||
typechecked("default", "string"),
|
option_default,
|
||||||
typechecked("convert", "function", "table"),
|
typechecked("convert", "function", "table"),
|
||||||
boundaries("args"),
|
boundaries("args"),
|
||||||
boundaries("count"),
|
boundaries("count"),
|
||||||
@@ -289,7 +302,8 @@ local Option = class({
|
|||||||
typechecked("show_default", "boolean"),
|
typechecked("show_default", "boolean"),
|
||||||
typechecked("overwrite", "boolean"),
|
typechecked("overwrite", "boolean"),
|
||||||
typechecked("argname", "string", "table"),
|
typechecked("argname", "string", "table"),
|
||||||
option_action
|
option_action,
|
||||||
|
option_init
|
||||||
}, Argument)
|
}, Argument)
|
||||||
|
|
||||||
function Argument:_get_argument_list()
|
function Argument:_get_argument_list()
|
||||||
@@ -362,23 +376,30 @@ function actions.append(result, target, argument, overwrite)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function Argument:_get_action()
|
function Argument:_get_action()
|
||||||
local action = self._action
|
local action, init
|
||||||
local init
|
|
||||||
|
|
||||||
if self._maxcount == 1 then
|
if self._maxcount == 1 then
|
||||||
if self._maxargs == 0 then
|
if self._maxargs == 0 then
|
||||||
action, init = action or "store_true", nil
|
action, init = "store_true", nil
|
||||||
else
|
else
|
||||||
action, init = action or "store", nil
|
action, init = "store", nil
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
if self._maxargs == 0 then
|
if self._maxargs == 0 then
|
||||||
action, init = action or "count", 0
|
action, init = "count", 0
|
||||||
else
|
else
|
||||||
action, init = action or "append", {}
|
action, init = "append", {}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if self._action then
|
||||||
|
action = self._action
|
||||||
|
end
|
||||||
|
|
||||||
|
if self._has_init then
|
||||||
|
init = self._init
|
||||||
|
end
|
||||||
|
|
||||||
if type(action) == "string" then
|
if type(action) == "string" then
|
||||||
action = actions[action]
|
action = actions[action]
|
||||||
end
|
end
|
||||||
|
Reference in New Issue
Block a user