] [-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)