mirror of
https://github.com/TangentFoxy/argparse.git
synced 2025-07-28 11:02:20 +00:00
fixed arguments from root parser not formatted
This commit is contained in:
@@ -5,14 +5,14 @@ describe("tests related to positional arguments", function()
|
||||
it("handles empty parser correctly", function()
|
||||
local parser = argparse.parser()
|
||||
local args = parser:parse({})
|
||||
assert.same(args, {})
|
||||
assert.same({}, args)
|
||||
end)
|
||||
|
||||
it("handles one argument correctly", function()
|
||||
local parser = argparse.parser()
|
||||
parser:argument "foo"
|
||||
local args = parser:parse({"bar"})
|
||||
assert.same(args, {foo = "bar"})
|
||||
assert.same({foo = "bar"}, args)
|
||||
end)
|
||||
|
||||
it("handles several arguments correctly", function()
|
||||
@@ -20,7 +20,7 @@ describe("tests related to positional arguments", function()
|
||||
parser:argument "foo1"
|
||||
parser:argument "foo2"
|
||||
local args = parser:parse({"bar", "baz"})
|
||||
assert.same(args, {foo1 = "bar", foo2 = "baz"})
|
||||
assert.same({foo1 = "bar", foo2 = "baz"}, args)
|
||||
end)
|
||||
|
||||
it("handles multi-argument correctly", function()
|
||||
@@ -29,7 +29,7 @@ describe("tests related to positional arguments", function()
|
||||
args = "*"
|
||||
})
|
||||
local args = parser:parse({"bar", "baz", "qu"})
|
||||
assert.same(args, {foo = {"bar", "baz", "qu"}})
|
||||
assert.same({foo = {"bar", "baz", "qu"}}, args)
|
||||
end)
|
||||
|
||||
it("handles restrained multi-argument correctly", function()
|
||||
@@ -38,7 +38,7 @@ describe("tests related to positional arguments", function()
|
||||
args = "2-4"
|
||||
})
|
||||
local args = parser:parse({"bar", "baz"})
|
||||
assert.same(args, {foo = {"bar", "baz"}})
|
||||
assert.same({foo = {"bar", "baz"}}, args)
|
||||
end)
|
||||
|
||||
it("handles several multi-arguments correctly", function()
|
||||
@@ -50,23 +50,23 @@ describe("tests related to positional arguments", function()
|
||||
args = "*"
|
||||
})
|
||||
local args = parser:parse({"bar"})
|
||||
assert.same(args, {foo1 = {"bar"}, foo2 = {}})
|
||||
assert.same({foo1 = {"bar"}, foo2 = {}}, args)
|
||||
args = parser:parse({"bar", "baz", "qu"})
|
||||
assert.same(args, {foo1 = {"bar", "baz"}, foo2 = {"qu"}})
|
||||
assert.same({foo1 = {"bar", "baz"}, foo2 = {"qu"}}, args)
|
||||
end)
|
||||
|
||||
it("handles hyphen correctly", function()
|
||||
local parser = argparse.parser()
|
||||
parser:argument "foo"
|
||||
local args = parser:parse({"-"})
|
||||
assert.same(args, {foo = "-"})
|
||||
assert.same({foo = "-"}, args)
|
||||
end)
|
||||
|
||||
it("handles double hyphen correctly", function()
|
||||
local parser = argparse.parser()
|
||||
parser:argument "foo"
|
||||
local args = parser:parse({"--", "-q"})
|
||||
assert.same(args, {foo = "-q"})
|
||||
assert.same({foo = "-q"}, args)
|
||||
end)
|
||||
end)
|
||||
|
||||
|
13
spec/commands_spec.lua
Normal file
13
spec/commands_spec.lua
Normal file
@@ -0,0 +1,13 @@
|
||||
local argparse = require "argparse"
|
||||
|
||||
describe("tests related to commands", function()
|
||||
it("handles commands after arguments", function()
|
||||
local parser = argparse.parser "name"
|
||||
parser:argument "file"
|
||||
parser:command "create"
|
||||
parser:command "remove"
|
||||
|
||||
local args = parser:parse{"temp.txt", "remove"}
|
||||
assert.same({file = "temp.txt", remove = true}, args)
|
||||
end)
|
||||
end)
|
@@ -8,7 +8,7 @@ describe("tests related to default values", function()
|
||||
default = "bar"
|
||||
})
|
||||
local args = parser:parse({})
|
||||
assert.same(args, {foo = "bar"})
|
||||
assert.same({foo = "bar"}, args)
|
||||
end)
|
||||
|
||||
it("handles default multi-argument correctly", function()
|
||||
@@ -18,7 +18,7 @@ describe("tests related to default values", function()
|
||||
default = "bar"
|
||||
})
|
||||
local args = parser:parse({"baz"})
|
||||
assert.same(args, {foo = {"baz", "bar", "bar"}})
|
||||
assert.same({foo = {"baz", "bar", "bar"}}, args)
|
||||
end)
|
||||
|
||||
it("does not use default values if not needed", function()
|
||||
@@ -28,7 +28,7 @@ describe("tests related to default values", function()
|
||||
default = "bar"
|
||||
})
|
||||
local args = parser:parse({"baz"})
|
||||
assert.same(args, {foo = {"baz"}})
|
||||
assert.same({foo = {"baz"}}, args)
|
||||
end)
|
||||
end)
|
||||
|
||||
@@ -39,7 +39,7 @@ describe("tests related to default values", function()
|
||||
default = "bar"
|
||||
})
|
||||
local args = parser:parse({"-f"})
|
||||
assert.same(args, {foo = "bar"})
|
||||
assert.same({foo = "bar"}, args)
|
||||
end)
|
||||
|
||||
it("doesn't use default if option is not invoked", function()
|
||||
@@ -48,7 +48,7 @@ describe("tests related to default values", function()
|
||||
default = "bar"
|
||||
})
|
||||
local args = parser:parse({})
|
||||
assert.same(args, {})
|
||||
assert.same({}, args)
|
||||
end)
|
||||
|
||||
it("handles default multi-argument correctly", function()
|
||||
@@ -58,7 +58,7 @@ describe("tests related to default values", function()
|
||||
default = "bar"
|
||||
})
|
||||
local args = parser:parse({"--foo=baz"})
|
||||
assert.same(args, {foo = {"baz", "bar", "bar"}})
|
||||
assert.same({foo = {"baz", "bar", "bar"}}, args)
|
||||
end)
|
||||
|
||||
it("does not use default values if not needed", function()
|
||||
@@ -68,7 +68,7 @@ describe("tests related to default values", function()
|
||||
default = "bar"
|
||||
})
|
||||
local args = parser:parse({"-f", "baz"})
|
||||
assert.same(args, {foo = {"baz"}})
|
||||
assert.same({foo = {"baz"}}, args)
|
||||
end)
|
||||
|
||||
it("handles multi-count options with default value correctly", function()
|
||||
@@ -78,7 +78,7 @@ describe("tests related to default values", function()
|
||||
default = "bar"
|
||||
})
|
||||
local args = parser:parse({"-f", "--foo=baz", "--foo"})
|
||||
assert.same(args, {foo = {"bar", "baz", "bar"}})
|
||||
assert.same({foo = {"bar", "baz", "bar"}}, args)
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
|
@@ -6,21 +6,21 @@ describe("tests related to options", function()
|
||||
local parser = argparse.parser()
|
||||
parser:option("-s", "--server")
|
||||
local args = parser:parse({})
|
||||
assert.same(args, {})
|
||||
assert.same({}, args)
|
||||
end)
|
||||
|
||||
it("handles one option correctly", function()
|
||||
local parser = argparse.parser()
|
||||
parser:option("-s", "--server")
|
||||
local args = parser:parse({"--server", "foo"})
|
||||
assert.same(args, {server = "foo"})
|
||||
assert.same({server = "foo"}, args)
|
||||
end)
|
||||
|
||||
it("handles GNU-style long options", function()
|
||||
local parser = argparse.parser()
|
||||
parser:option("-s", "--server")
|
||||
local args = parser:parse({"--server=foo"})
|
||||
assert.same(args, {server = "foo"})
|
||||
assert.same({server = "foo"}, args)
|
||||
end)
|
||||
|
||||
it("handles GNU-style long options even when it could take more arguments", function()
|
||||
@@ -29,7 +29,7 @@ describe("tests related to options", function()
|
||||
args = "*"
|
||||
})
|
||||
local args = parser:parse({"--server=foo"})
|
||||
assert.same(args, {server = {"foo"}})
|
||||
assert.same({server = {"foo"}}, args)
|
||||
end)
|
||||
|
||||
it("handles GNU-style long options for multi-argument options", function()
|
||||
@@ -38,21 +38,23 @@ describe("tests related to options", function()
|
||||
args = "1-2"
|
||||
})
|
||||
local args = parser:parse({"--server=foo", "bar"})
|
||||
assert.same(args, {server = {"foo", "bar"}})
|
||||
assert.same({server = {"foo", "bar"}}, args)
|
||||
end)
|
||||
|
||||
it("handles short option correclty", function()
|
||||
local parser = argparse.parser()
|
||||
parser:option("-s", "--server")
|
||||
local args = parser:parse({"-s", "foo"})
|
||||
assert.same(args, {server = "foo"})
|
||||
assert.same({server = "foo"}, args)
|
||||
end)
|
||||
|
||||
it("handles flag correclty", function()
|
||||
local parser = argparse.parser()
|
||||
parser:flag("-q", "--quiet")
|
||||
local args = parser:parse({"--quiet"})
|
||||
assert.same(args, {quiet = true})
|
||||
assert.same({quiet = true}, args)
|
||||
local args = parser:parse({})
|
||||
assert.same({}, args)
|
||||
end)
|
||||
|
||||
it("handles combined flags correclty", function()
|
||||
@@ -60,14 +62,14 @@ describe("tests related to options", function()
|
||||
parser:flag("-q", "--quiet")
|
||||
parser:flag("-f", "--fast")
|
||||
local args = parser:parse({"-qf"})
|
||||
assert.same(args, {quiet = true, fast = true})
|
||||
assert.same({quiet = true, fast = true}, args)
|
||||
end)
|
||||
|
||||
it("handles short options without space between option and argument", function()
|
||||
local parser = argparse.parser()
|
||||
parser:option("-s", "--server")
|
||||
local args = parser:parse({"-sfoo"})
|
||||
assert.same(args, {server = "foo"})
|
||||
assert.same({server = "foo"}, args)
|
||||
end)
|
||||
|
||||
it("handles flags combined with short option correclty", function()
|
||||
@@ -75,7 +77,7 @@ describe("tests related to options", function()
|
||||
parser:flag("-q", "--quiet")
|
||||
parser:option("-s", "--server")
|
||||
local args = parser:parse({"-qsfoo"})
|
||||
assert.same(args, {quiet = true, server = "foo"})
|
||||
assert.same({quiet = true, server = "foo"}, args)
|
||||
end)
|
||||
|
||||
describe("Options with optional argument", function()
|
||||
@@ -85,7 +87,7 @@ describe("tests related to options", function()
|
||||
args = "?"
|
||||
})
|
||||
local args = parser:parse({})
|
||||
assert.same(args, {})
|
||||
assert.same({}, args)
|
||||
end)
|
||||
|
||||
it("handles option without argument correctly", function()
|
||||
@@ -94,7 +96,7 @@ describe("tests related to options", function()
|
||||
args = "?"
|
||||
})
|
||||
local args = parser:parse({"-p"})
|
||||
assert.same(args, {password = {}})
|
||||
assert.same({password = {}}, args)
|
||||
end)
|
||||
|
||||
it("handles option with argument correctly", function()
|
||||
@@ -103,7 +105,7 @@ describe("tests related to options", function()
|
||||
args = "?"
|
||||
})
|
||||
local args = parser:parse({"-p", "password"})
|
||||
assert.same(args, {password = {"password"}})
|
||||
assert.same({password = {"password"}}, args)
|
||||
end)
|
||||
end)
|
||||
|
||||
@@ -113,7 +115,7 @@ describe("tests related to options", function()
|
||||
args = 2
|
||||
})
|
||||
local args = parser:parse({"--pair", "Alice", "Bob"})
|
||||
assert.same(args, {pair = {"Alice", "Bob"}})
|
||||
assert.same({pair = {"Alice", "Bob"}}, args)
|
||||
end)
|
||||
|
||||
describe("Multi-count options", function()
|
||||
@@ -123,7 +125,7 @@ describe("tests related to options", function()
|
||||
count = "*"
|
||||
})
|
||||
local args = parser:parse({"-efoo", "--exclude=bar", "-e", "baz"})
|
||||
assert.same(args, {exclude = {"foo", "bar", "baz"}})
|
||||
assert.same({exclude = {"foo", "bar", "baz"}}, args)
|
||||
end)
|
||||
|
||||
it("handles not used multi-count option correctly", function()
|
||||
@@ -132,7 +134,7 @@ describe("tests related to options", function()
|
||||
count = "*"
|
||||
})
|
||||
local args = parser:parse({})
|
||||
assert.same(args, {exclude = {}})
|
||||
assert.same({exclude = {}}, args)
|
||||
end)
|
||||
|
||||
it("handles multi-count multi-argument option correctly", function()
|
||||
@@ -142,7 +144,7 @@ describe("tests related to options", function()
|
||||
args = 2
|
||||
})
|
||||
local args = parser:parse({"-e", "Alice", "Bob", "-e", "Emma", "Jacob"})
|
||||
assert.same(args, {exclude = {{"Alice", "Bob"}, {"Emma", "Jacob"}}})
|
||||
assert.same({exclude = {{"Alice", "Bob"}, {"Emma", "Jacob"}}}, args)
|
||||
end)
|
||||
|
||||
it("handles multi-count flag correctly", function()
|
||||
@@ -151,7 +153,7 @@ describe("tests related to options", function()
|
||||
count = "*"
|
||||
})
|
||||
local args = parser:parse({"-qq", "--quiet"})
|
||||
assert.same(args, {quiet = 3})
|
||||
assert.same({quiet = 3}, args)
|
||||
end)
|
||||
|
||||
it("overwrites old invocations", function()
|
||||
@@ -160,7 +162,7 @@ describe("tests related to options", function()
|
||||
count = "0-2"
|
||||
})
|
||||
local args = parser:parse({"-uAlice", "--user=Bob", "--user", "John"})
|
||||
assert.same(args, {user = {"Bob", "John"}})
|
||||
assert.same({user = {"Bob", "John"}}, args)
|
||||
end)
|
||||
|
||||
it("handles not used multi-count flag correctly", function()
|
||||
@@ -169,7 +171,7 @@ describe("tests related to options", function()
|
||||
count = "*"
|
||||
})
|
||||
local args = parser:parse({})
|
||||
assert.same(args, {quiet = 0})
|
||||
assert.same({quiet = 0}, args)
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
|
@@ -221,11 +221,14 @@ function Parser:parse(args)
|
||||
local parser
|
||||
local charset
|
||||
local options = {}
|
||||
local arguments, commands
|
||||
local arguments = {}
|
||||
local commands
|
||||
local opt_context = {}
|
||||
local com_context
|
||||
local result = {}
|
||||
local cur_option, cur_arg_i, cur_arg
|
||||
local cur_option
|
||||
local cur_arg_i = 1
|
||||
local cur_arg
|
||||
|
||||
local function close(element)
|
||||
local invocations = result[element.target]
|
||||
@@ -291,15 +294,13 @@ function Parser:parse(args)
|
||||
result[option.target] = {}
|
||||
end
|
||||
|
||||
arguments = p.arguments
|
||||
cur_arg_i = 1
|
||||
cur_arg = arguments[cur_arg_i]
|
||||
|
||||
for _, argument in ipairs(arguments) do
|
||||
for _, argument in ipairs(p.arguments) do
|
||||
table.insert(arguments, argument)
|
||||
result[argument.target] = {}
|
||||
invoke(argument)
|
||||
end
|
||||
|
||||
cur_arg = arguments[cur_arg_i]
|
||||
commands = p.commands
|
||||
com_context = {}
|
||||
|
||||
@@ -396,7 +397,6 @@ function Parser:parse(args)
|
||||
end
|
||||
|
||||
local function format()
|
||||
local new_result = {}
|
||||
local invocations
|
||||
|
||||
for _, elements in ipairs{options, arguments} do
|
||||
@@ -409,32 +409,32 @@ function Parser:parse(args)
|
||||
if element.maxcount == 1 then
|
||||
if element.maxargs == 0 then
|
||||
if #invocations > 0 then
|
||||
new_result[element.target] = true
|
||||
result[element.target] = true
|
||||
else
|
||||
result[element.target] = nil
|
||||
end
|
||||
elseif element.maxargs == 1 and element.minargs == 1 then
|
||||
if #invocations > 0 then
|
||||
new_result[element.target] = invocations[1][1]
|
||||
result[element.target] = invocations[1][1]
|
||||
else
|
||||
result[element.target] = nil
|
||||
end
|
||||
else
|
||||
new_result[element.target] = invocations[1]
|
||||
result[element.target] = invocations[1]
|
||||
end
|
||||
else
|
||||
if element.maxargs == 0 then
|
||||
new_result[element.target] = #invocations
|
||||
result[element.target] = #invocations
|
||||
elseif element.maxargs == 1 and element.minargs == 1 then
|
||||
new_result[element.target] = {}
|
||||
|
||||
for _, passed in ipairs(invocations) do
|
||||
table.insert(new_result[element.target], passed[1])
|
||||
for i=1, #invocations do
|
||||
invocations[i] = invocations[i][1]
|
||||
end
|
||||
else
|
||||
new_result[element.target] = invocations
|
||||
result[element.target] = invocations
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
result = new_result
|
||||
end
|
||||
|
||||
switch(self)
|
||||
|
Reference in New Issue
Block a user