diff --git a/README.md b/README.md index 16a9277..2c902b6 100644 --- a/README.md +++ b/README.md @@ -471,11 +471,11 @@ produces nothing. ### Default values -For arguments and options, if `default` field is set, its value is used as default argument. +For elements such as arguments and options, if `default` field is set, its value is stored in case the element was not used. ```lua -parser:argument "input" - :default "input.txt" +parser:option "-o" "--output" + :default "a.out" ``` ```bash @@ -483,7 +483,7 @@ $ lua script.lua ``` ``` -input input.txt +output a.out ``` The existence of a default value is reflected in help message. @@ -493,22 +493,72 @@ $ lua script.lua --help ``` ``` -Usage: script.lua [-h] [] - -Arguments: - input default: input.txt +Usage: script [-o ] [-h] Options: + -o , --output + default: a.out -h, --help Show this help message and exit. ``` -#### Default values and options +Note that invocation without required arguments is still an error. -Note that if an option is not invoked, its default value will not be stored. +```bash +$ lua script.lua -o +``` -```lua -parser:option "-o" "--output" +``` +Usage: script [-o ] [-h] + +Error: too few arguments +``` + +#### Default mode + +The `defmode` field regulates how argparse should use the default value of an element. + +If `defmode` contains `"u"`(for `unused`), the default value will be automatically passed to the element if it was not invoked at all. This is the default behavior. + +If `defmode` contains `"a"`(for `argument`), the default value will be automatically passed to the element if not enough arguments were passed, or not enough invocations were made. + +Consider the difference: + +``` +parser:option "-o" :default "a.out" +parser:option "-p" + :default "password" + :defmode "arg" +``` + +```bash +$ lua script.lua -h +``` + +``` +Usage: script [-o ] [-p [

]] [-h] + +Options: + -o default: a.out + -p [

] default: password + -h, --help Show this help message and exit. +``` + +```bash +$ lua script.lua +``` + +``` +o a.out +``` + +```bash +$ lua script.lua -p +``` + +``` +o a.out +p password ``` ```bash @@ -516,31 +566,9 @@ $ lua script.lua -o ``` ``` -output a.out -``` +Usage: script [-o ] [-p [

]] [-h] -But - -```bash -$ lua script.lua -``` - -produces nothing. - -That is because by default options can be not used at all. If default value must be used even when the option is not invoked, make the invocation obligatory. - -```lua -parser:option "-o" "--output" - :default "a.out" - :count(1) -``` - -```bash -$ lua script.lua -``` - -``` -output a.out +Error: too few arguments ``` ### Converters diff --git a/spec/default_spec.lua b/spec/default_spec.lua index 8f8d61c..a80433e 100644 --- a/spec/default_spec.lua +++ b/spec/default_spec.lua @@ -49,6 +49,7 @@ describe("tests related to default values", function() local parser = Parser() parser:option "-o" "--output" :default "a.out" + :defmode "unused" local args = parser:parse{} assert.same({output = "a.out"}, args) args = parser:parse{"--output", "foo.txt"} diff --git a/src/argparse.lua b/src/argparse.lua index de70a70..ff83e4f 100644 --- a/src/argparse.lua +++ b/src/argparse.lua @@ -70,7 +70,7 @@ local Argument = class { __name = "Argument", _args = 1, _count = 1, - _defmode = "count", + _defmode = "unused", _fields = { "name", "description", "target", "args", "minargs", "maxargs", "default", "defmode", @@ -133,7 +133,7 @@ function Argument:get_usage() if not self._usage then self._usage = table.concat(self:get_arg_usage("<" .. self._name .. ">"), " ") - if self._default and self._defmode:find "c" then + if self._default and self._defmode:find "u" then if self._maxargs > 1 or (self._minargs == 1 and not self._defmode:find "a") then self._usage = "[" .. self._usage .. "]" end @@ -772,7 +772,7 @@ function Parser:_parse(args, errhandler) end while cur_arg do - if passed[cur_arg] == 0 and cur_arg._default and cur_arg._defmode:find "c" then + if passed[cur_arg] == 0 and cur_arg._default and cur_arg._defmode:find "u" then complete_invocation(cur_arg) else close(cur_arg) @@ -785,7 +785,7 @@ function Parser:_parse(args, errhandler) for _, option in ipairs(options) do if invocations[option] == 0 then - if option._default and option._defmode:find "c" then + if option._default and option._defmode:find "u" then invoke(option) complete_invocation(option) close(option)