mirror of
https://github.com/TangentFoxy/argparse.git
synced 2025-07-28 02:52:20 +00:00
Fixed options receiving arguments after --
This commit is contained in:
17
README.md
17
README.md
@@ -11,8 +11,6 @@ Almost everything is implemented, and a WIP version will be available soon.
|
|||||||
TODO till first release:
|
TODO till first release:
|
||||||
|
|
||||||
* Add `Content` section to this README.
|
* Add `Content` section to this README.
|
||||||
* Add information about the parsing algorithm to the tutorial.
|
|
||||||
* Add information about the way arguments are stored in the result table to the tutorial.
|
|
||||||
* Add a small example to the beginning of this README.
|
* Add a small example to the beginning of this README.
|
||||||
* Check the grammar in this README.
|
* Check the grammar in this README.
|
||||||
* Generate .html file from the tutorial part of this README and put it into `doc` directory.
|
* Generate .html file from the tutorial part of this README and put it into `doc` directory.
|
||||||
@@ -785,7 +783,20 @@ Error: option '-o' must be used at most 1 time
|
|||||||
|
|
||||||
#### Generating usage and help messages
|
#### Generating usage and help messages
|
||||||
|
|
||||||
`get_help` and `get_usage` methods of Parser and Command classes can be used to generate their help and usage messages.
|
`:get_help()` and `get_usage:()` methods of Parser and Command classes can be used to generate their help and usage messages.
|
||||||
|
|
||||||
|
#### Parsing algorithm
|
||||||
|
|
||||||
|
argparse interprets command-line arguments in the following way:
|
||||||
|
|
||||||
|
Argument | Interpretation
|
||||||
|
--- | ---
|
||||||
|
`foo` | An argument of an option or a positional argument.
|
||||||
|
`--foo` | An option.
|
||||||
|
`--foo=bar` | An option and its argument. The option must be able to take arguments.
|
||||||
|
`-f` | An option.
|
||||||
|
`-abcdef` | Letters are interpreted as options. If one of them can take an argument, the rest of the string is passed to it.
|
||||||
|
`--` | The rest of the command-line arguments will be interpreted as positional arguments.
|
||||||
|
|
||||||
## Documentation
|
## Documentation
|
||||||
|
|
||||||
|
@@ -88,6 +88,35 @@ describe("tests related to options", function()
|
|||||||
assert.same({quiet = true, server = "foo"}, args)
|
assert.same({quiet = true, server = "foo"}, args)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
it("interprets extra option arguments as positional arguments", function()
|
||||||
|
local parser = Parser()
|
||||||
|
parser:argument "input"
|
||||||
|
:args "2+"
|
||||||
|
parser:option "-s" "--server"
|
||||||
|
local args = parser:parse{"foo", "-sFROM", "bar"}
|
||||||
|
assert.same({input = {"foo", "bar"}, server = "FROM"}, args)
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("does not interpret extra option arguments as other option's arguments", function()
|
||||||
|
local parser = Parser()
|
||||||
|
parser:argument "output"
|
||||||
|
parser:option "--input"
|
||||||
|
:args "+"
|
||||||
|
parser:option "-s" "--server"
|
||||||
|
local args = parser:parse{"--input", "foo", "-sFROM", "bar"}
|
||||||
|
assert.same({input = {"foo"}, server = "FROM", output = "bar"}, args)
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("does not pass arguments to options after double hyphen", function()
|
||||||
|
local parser = Parser()
|
||||||
|
parser:argument "input"
|
||||||
|
:args "?"
|
||||||
|
parser:option "--exclude"
|
||||||
|
:args "*"
|
||||||
|
local args = parser:parse{"--exclude", "--", "foo"}
|
||||||
|
assert.same({input = "foo", exclude = {}}, args)
|
||||||
|
end)
|
||||||
|
|
||||||
describe("Special chars set", function()
|
describe("Special chars set", function()
|
||||||
it("handles windows-style options", function()
|
it("handles windows-style options", function()
|
||||||
local parser = Parser()
|
local parser = Parser()
|
||||||
@@ -106,8 +135,8 @@ describe("tests related to options", function()
|
|||||||
:count "*"
|
:count "*"
|
||||||
parser:command "deep"
|
parser:command "deep"
|
||||||
:add_help(false)
|
:add_help(false)
|
||||||
:option "\\s"
|
:option "/s"
|
||||||
local args = parser:parse{"-v", "deep", "\\s", "foo", "-vv"}
|
local args = parser:parse{"-v", "deep", "/s", "foo", "-vv"}
|
||||||
assert.same({verbose = 3, deep = true, s = "foo"}, args)
|
assert.same({verbose = 3, deep = true, s = "foo"}, args)
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
@@ -790,6 +790,10 @@ function Parser:_parse(args, errhandler)
|
|||||||
plain = false
|
plain = false
|
||||||
if data:sub(2, 2) == first then
|
if data:sub(2, 2) == first then
|
||||||
if #data == 2 then
|
if #data == 2 then
|
||||||
|
if cur_option then
|
||||||
|
close(cur_option)
|
||||||
|
end
|
||||||
|
|
||||||
handle_options = false
|
handle_options = false
|
||||||
else
|
else
|
||||||
local equal = data:find "="
|
local equal = data:find "="
|
||||||
|
Reference in New Issue
Block a user