mirror of
https://github.com/TangentFoxy/argparse.git
synced 2025-07-28 11:02:20 +00:00
more tests, fixed some incorrect behaviour
This commit is contained in:
111
spec/options_spec.lua
Normal file
111
spec/options_spec.lua
Normal file
@@ -0,0 +1,111 @@
|
|||||||
|
local largparse = require "largparse"
|
||||||
|
|
||||||
|
describe("tests related to options", function()
|
||||||
|
local function curry(f, ...)
|
||||||
|
local args = {...}
|
||||||
|
return function() return f(table.unpack(args)) end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe("passing correct options", function()
|
||||||
|
it("handles no options passed correctly", function()
|
||||||
|
local parser = largparse.parser()
|
||||||
|
parser:option("-s", "--server")
|
||||||
|
local args = parser:parse({})
|
||||||
|
assert.same(args, {})
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("handles one option correctly", function()
|
||||||
|
local parser = largparse.parser()
|
||||||
|
parser:option("-s", "--server")
|
||||||
|
local args = parser:parse({"--server", "foo"})
|
||||||
|
assert.same(args, {server = "foo"})
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("handles GNU-style long options", function()
|
||||||
|
local parser = largparse.parser()
|
||||||
|
parser:option("-s", "--server")
|
||||||
|
local args = parser:parse({"--server=foo"})
|
||||||
|
assert.same(args, {server = "foo"})
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("handles short option correclty", function()
|
||||||
|
local parser = largparse.parser()
|
||||||
|
parser:option("-s", "--server")
|
||||||
|
local args = parser:parse({"-s", "foo"})
|
||||||
|
assert.same(args, {server = "foo"})
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("handles flag correclty", function()
|
||||||
|
local parser = largparse.parser()
|
||||||
|
parser:flag("-q", "--quiet")
|
||||||
|
local args = parser:parse({"--quiet"})
|
||||||
|
assert.same(args, {quiet = true})
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("handles combined flags correclty", function()
|
||||||
|
local parser = largparse.parser()
|
||||||
|
parser:flag("-q", "--quiet")
|
||||||
|
parser:flag("-f", "--fast")
|
||||||
|
local args = parser:parse({"-qf"})
|
||||||
|
assert.same(args, {quiet = true, fast = true})
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("handles short options without space between option and argument", function()
|
||||||
|
local parser = largparse.parser()
|
||||||
|
parser:option("-s", "--server")
|
||||||
|
local args = parser:parse({"-sfoo"})
|
||||||
|
assert.same(args, {server = "foo"})
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("handles flags combined with short option correclty", function()
|
||||||
|
local parser = largparse.parser()
|
||||||
|
parser:flag("-q", "--quiet")
|
||||||
|
parser:option("-s", "--server")
|
||||||
|
local args = parser:parse({"-qsfoo"})
|
||||||
|
assert.same(args, {quiet = true, server = "foo"})
|
||||||
|
end)
|
||||||
|
|
||||||
|
describe("Options with optional argument", function()
|
||||||
|
it("handles emptiness correctly", function()
|
||||||
|
local parser = largparse.parser()
|
||||||
|
parser:option("-p", "--password", {
|
||||||
|
args = "?"
|
||||||
|
})
|
||||||
|
local args = parser:parse({})
|
||||||
|
assert.same(args, {})
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("handles option without argument correctly", function()
|
||||||
|
local parser = largparse.parser()
|
||||||
|
parser:option("-p", "--password", {
|
||||||
|
args = "?"
|
||||||
|
})
|
||||||
|
local args = parser:parse({"-p"})
|
||||||
|
assert.same(args, {password = {}})
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("handles option with argument correctly", function()
|
||||||
|
local parser = largparse.parser()
|
||||||
|
parser:option("-p", "--password", {
|
||||||
|
args = "?"
|
||||||
|
})
|
||||||
|
local args = parser:parse({"-p", "password"})
|
||||||
|
assert.same(args, {password = {"password"}})
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
|
||||||
|
describe("passing incorrect options", function()
|
||||||
|
local old_parser = largparse.parser
|
||||||
|
|
||||||
|
setup(function()
|
||||||
|
largparse.parser = old_parser:extends()
|
||||||
|
function largparse.parser:error(fmt, ...)
|
||||||
|
error(fmt:format(...))
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
|
||||||
|
-- TODO
|
||||||
|
|
||||||
|
end)
|
||||||
|
end)
|
@@ -250,7 +250,7 @@ function Parser:parse(args)
|
|||||||
element = self:assert(state.context[name], "unknown option " .. name)
|
element = self:assert(state.context[name], "unknown option " .. name)
|
||||||
state:handle_option(name)
|
state:handle_option(name)
|
||||||
|
|
||||||
if i ~= #data and not (element:can_take(0) and data:sub(i+1, i+1):match "[a-zA-Z]") then
|
if i ~= #data and not (element.minargs == 0 and data:sub(i+1, i+1):match "[a-zA-Z]") then
|
||||||
state:handle_argument(data:sub(i+1))
|
state:handle_argument(data:sub(i+1))
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
|
@@ -65,7 +65,7 @@ function State:get_result()
|
|||||||
result[element.target] = invocations[1][1]
|
result[element.target] = invocations[1][1]
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
result[element.target] = invocations[1] or {}
|
result[element.target] = invocations[1]
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
if element.maxargs == 0 then
|
if element.maxargs == 0 then
|
||||||
|
Reference in New Issue
Block a user