mirror of
https://github.com/TangentFoxy/argparse.git
synced 2025-07-28 11:02:20 +00:00
Updated README; use u
in defmode
as a flag instead of c
This commit is contained in:
100
README.md
100
README.md
@@ -471,11 +471,11 @@ produces nothing.
|
|||||||
|
|
||||||
### Default values
|
### 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
|
```lua
|
||||||
parser:argument "input"
|
parser:option "-o" "--output"
|
||||||
:default "input.txt"
|
:default "a.out"
|
||||||
```
|
```
|
||||||
|
|
||||||
```bash
|
```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.
|
The existence of a default value is reflected in help message.
|
||||||
@@ -493,22 +493,72 @@ $ lua script.lua --help
|
|||||||
```
|
```
|
||||||
|
|
||||||
```
|
```
|
||||||
Usage: script.lua [-h] [<input>]
|
Usage: script [-o <output>] [-h]
|
||||||
|
|
||||||
Arguments:
|
|
||||||
input default: input.txt
|
|
||||||
|
|
||||||
Options:
|
Options:
|
||||||
|
-o <output>, --output <output>
|
||||||
|
default: a.out
|
||||||
-h, --help Show this help message and exit.
|
-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 <output>] [-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"
|
:default "a.out"
|
||||||
|
parser:option "-p"
|
||||||
|
:default "password"
|
||||||
|
:defmode "arg"
|
||||||
|
```
|
||||||
|
|
||||||
|
```bash
|
||||||
|
$ lua script.lua -h
|
||||||
|
```
|
||||||
|
|
||||||
|
```
|
||||||
|
Usage: script [-o <o>] [-p [<p>]] [-h]
|
||||||
|
|
||||||
|
Options:
|
||||||
|
-o <o> default: a.out
|
||||||
|
-p [<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
|
```bash
|
||||||
@@ -516,31 +566,9 @@ $ lua script.lua -o
|
|||||||
```
|
```
|
||||||
|
|
||||||
```
|
```
|
||||||
output a.out
|
Usage: script [-o <o>] [-p [<p>]] [-h]
|
||||||
```
|
|
||||||
|
|
||||||
But
|
Error: too few arguments
|
||||||
|
|
||||||
```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
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### Converters
|
### Converters
|
||||||
|
@@ -49,6 +49,7 @@ describe("tests related to default values", function()
|
|||||||
local parser = Parser()
|
local parser = Parser()
|
||||||
parser:option "-o" "--output"
|
parser:option "-o" "--output"
|
||||||
:default "a.out"
|
:default "a.out"
|
||||||
|
:defmode "unused"
|
||||||
local args = parser:parse{}
|
local args = parser:parse{}
|
||||||
assert.same({output = "a.out"}, args)
|
assert.same({output = "a.out"}, args)
|
||||||
args = parser:parse{"--output", "foo.txt"}
|
args = parser:parse{"--output", "foo.txt"}
|
||||||
|
@@ -70,7 +70,7 @@ local Argument = class {
|
|||||||
__name = "Argument",
|
__name = "Argument",
|
||||||
_args = 1,
|
_args = 1,
|
||||||
_count = 1,
|
_count = 1,
|
||||||
_defmode = "count",
|
_defmode = "unused",
|
||||||
_fields = {
|
_fields = {
|
||||||
"name", "description", "target", "args",
|
"name", "description", "target", "args",
|
||||||
"minargs", "maxargs", "default", "defmode",
|
"minargs", "maxargs", "default", "defmode",
|
||||||
@@ -133,7 +133,7 @@ function Argument:get_usage()
|
|||||||
if not self._usage then
|
if not self._usage then
|
||||||
self._usage = table.concat(self:get_arg_usage("<" .. self._name .. ">"), " ")
|
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
|
if self._maxargs > 1 or (self._minargs == 1 and not self._defmode:find "a") then
|
||||||
self._usage = "[" .. self._usage .. "]"
|
self._usage = "[" .. self._usage .. "]"
|
||||||
end
|
end
|
||||||
@@ -772,7 +772,7 @@ function Parser:_parse(args, errhandler)
|
|||||||
end
|
end
|
||||||
|
|
||||||
while cur_arg do
|
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)
|
complete_invocation(cur_arg)
|
||||||
else
|
else
|
||||||
close(cur_arg)
|
close(cur_arg)
|
||||||
@@ -785,7 +785,7 @@ function Parser:_parse(args, errhandler)
|
|||||||
|
|
||||||
for _, option in ipairs(options) do
|
for _, option in ipairs(options) do
|
||||||
if invocations[option] == 0 then
|
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)
|
invoke(option)
|
||||||
complete_invocation(option)
|
complete_invocation(option)
|
||||||
close(option)
|
close(option)
|
||||||
|
Reference in New Issue
Block a user