fixed arguments from root parser not formatted

This commit is contained in:
mpeterv
2014-01-19 15:11:09 +04:00
parent f7a0703e73
commit e81aab8a5b
5 changed files with 71 additions and 56 deletions

View File

@@ -5,14 +5,14 @@ describe("tests related to positional arguments", function()
it("handles empty parser correctly", function() it("handles empty parser correctly", function()
local parser = argparse.parser() local parser = argparse.parser()
local args = parser:parse({}) local args = parser:parse({})
assert.same(args, {}) assert.same({}, args)
end) end)
it("handles one argument correctly", function() it("handles one argument correctly", function()
local parser = argparse.parser() local parser = argparse.parser()
parser:argument "foo" parser:argument "foo"
local args = parser:parse({"bar"}) local args = parser:parse({"bar"})
assert.same(args, {foo = "bar"}) assert.same({foo = "bar"}, args)
end) end)
it("handles several arguments correctly", function() it("handles several arguments correctly", function()
@@ -20,7 +20,7 @@ describe("tests related to positional arguments", function()
parser:argument "foo1" parser:argument "foo1"
parser:argument "foo2" parser:argument "foo2"
local args = parser:parse({"bar", "baz"}) local args = parser:parse({"bar", "baz"})
assert.same(args, {foo1 = "bar", foo2 = "baz"}) assert.same({foo1 = "bar", foo2 = "baz"}, args)
end) end)
it("handles multi-argument correctly", function() it("handles multi-argument correctly", function()
@@ -29,7 +29,7 @@ describe("tests related to positional arguments", function()
args = "*" args = "*"
}) })
local args = parser:parse({"bar", "baz", "qu"}) local args = parser:parse({"bar", "baz", "qu"})
assert.same(args, {foo = {"bar", "baz", "qu"}}) assert.same({foo = {"bar", "baz", "qu"}}, args)
end) end)
it("handles restrained multi-argument correctly", function() it("handles restrained multi-argument correctly", function()
@@ -38,7 +38,7 @@ describe("tests related to positional arguments", function()
args = "2-4" args = "2-4"
}) })
local args = parser:parse({"bar", "baz"}) local args = parser:parse({"bar", "baz"})
assert.same(args, {foo = {"bar", "baz"}}) assert.same({foo = {"bar", "baz"}}, args)
end) end)
it("handles several multi-arguments correctly", function() it("handles several multi-arguments correctly", function()
@@ -50,23 +50,23 @@ describe("tests related to positional arguments", function()
args = "*" args = "*"
}) })
local args = parser:parse({"bar"}) local args = parser:parse({"bar"})
assert.same(args, {foo1 = {"bar"}, foo2 = {}}) assert.same({foo1 = {"bar"}, foo2 = {}}, args)
args = parser:parse({"bar", "baz", "qu"}) args = parser:parse({"bar", "baz", "qu"})
assert.same(args, {foo1 = {"bar", "baz"}, foo2 = {"qu"}}) assert.same({foo1 = {"bar", "baz"}, foo2 = {"qu"}}, args)
end) end)
it("handles hyphen correctly", function() it("handles hyphen correctly", function()
local parser = argparse.parser() local parser = argparse.parser()
parser:argument "foo" parser:argument "foo"
local args = parser:parse({"-"}) local args = parser:parse({"-"})
assert.same(args, {foo = "-"}) assert.same({foo = "-"}, args)
end) end)
it("handles double hyphen correctly", function() it("handles double hyphen correctly", function()
local parser = argparse.parser() local parser = argparse.parser()
parser:argument "foo" parser:argument "foo"
local args = parser:parse({"--", "-q"}) local args = parser:parse({"--", "-q"})
assert.same(args, {foo = "-q"}) assert.same({foo = "-q"}, args)
end) end)
end) end)

13
spec/commands_spec.lua Normal file
View 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)

View File

@@ -8,7 +8,7 @@ describe("tests related to default values", function()
default = "bar" default = "bar"
}) })
local args = parser:parse({}) local args = parser:parse({})
assert.same(args, {foo = "bar"}) assert.same({foo = "bar"}, args)
end) end)
it("handles default multi-argument correctly", function() it("handles default multi-argument correctly", function()
@@ -18,7 +18,7 @@ describe("tests related to default values", function()
default = "bar" default = "bar"
}) })
local args = parser:parse({"baz"}) local args = parser:parse({"baz"})
assert.same(args, {foo = {"baz", "bar", "bar"}}) assert.same({foo = {"baz", "bar", "bar"}}, args)
end) end)
it("does not use default values if not needed", function() it("does not use default values if not needed", function()
@@ -28,7 +28,7 @@ describe("tests related to default values", function()
default = "bar" default = "bar"
}) })
local args = parser:parse({"baz"}) local args = parser:parse({"baz"})
assert.same(args, {foo = {"baz"}}) assert.same({foo = {"baz"}}, args)
end) end)
end) end)
@@ -39,7 +39,7 @@ describe("tests related to default values", function()
default = "bar" default = "bar"
}) })
local args = parser:parse({"-f"}) local args = parser:parse({"-f"})
assert.same(args, {foo = "bar"}) assert.same({foo = "bar"}, args)
end) end)
it("doesn't use default if option is not invoked", function() it("doesn't use default if option is not invoked", function()
@@ -48,7 +48,7 @@ describe("tests related to default values", function()
default = "bar" default = "bar"
}) })
local args = parser:parse({}) local args = parser:parse({})
assert.same(args, {}) assert.same({}, args)
end) end)
it("handles default multi-argument correctly", function() it("handles default multi-argument correctly", function()
@@ -58,7 +58,7 @@ describe("tests related to default values", function()
default = "bar" default = "bar"
}) })
local args = parser:parse({"--foo=baz"}) local args = parser:parse({"--foo=baz"})
assert.same(args, {foo = {"baz", "bar", "bar"}}) assert.same({foo = {"baz", "bar", "bar"}}, args)
end) end)
it("does not use default values if not needed", function() it("does not use default values if not needed", function()
@@ -68,7 +68,7 @@ describe("tests related to default values", function()
default = "bar" default = "bar"
}) })
local args = parser:parse({"-f", "baz"}) local args = parser:parse({"-f", "baz"})
assert.same(args, {foo = {"baz"}}) assert.same({foo = {"baz"}}, args)
end) end)
it("handles multi-count options with default value correctly", function() it("handles multi-count options with default value correctly", function()
@@ -78,7 +78,7 @@ describe("tests related to default values", function()
default = "bar" default = "bar"
}) })
local args = parser:parse({"-f", "--foo=baz", "--foo"}) 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) end)
end) end)

View File

@@ -6,21 +6,21 @@ describe("tests related to options", function()
local parser = argparse.parser() local parser = argparse.parser()
parser:option("-s", "--server") parser:option("-s", "--server")
local args = parser:parse({}) local args = parser:parse({})
assert.same(args, {}) assert.same({}, args)
end) end)
it("handles one option correctly", function() it("handles one option correctly", function()
local parser = argparse.parser() local parser = argparse.parser()
parser:option("-s", "--server") parser:option("-s", "--server")
local args = parser:parse({"--server", "foo"}) local args = parser:parse({"--server", "foo"})
assert.same(args, {server = "foo"}) assert.same({server = "foo"}, args)
end) end)
it("handles GNU-style long options", function() it("handles GNU-style long options", function()
local parser = argparse.parser() local parser = argparse.parser()
parser:option("-s", "--server") parser:option("-s", "--server")
local args = parser:parse({"--server=foo"}) local args = parser:parse({"--server=foo"})
assert.same(args, {server = "foo"}) assert.same({server = "foo"}, args)
end) end)
it("handles GNU-style long options even when it could take more arguments", function() 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 = "*" args = "*"
}) })
local args = parser:parse({"--server=foo"}) local args = parser:parse({"--server=foo"})
assert.same(args, {server = {"foo"}}) assert.same({server = {"foo"}}, args)
end) end)
it("handles GNU-style long options for multi-argument options", function() it("handles GNU-style long options for multi-argument options", function()
@@ -38,21 +38,23 @@ describe("tests related to options", function()
args = "1-2" args = "1-2"
}) })
local args = parser:parse({"--server=foo", "bar"}) local args = parser:parse({"--server=foo", "bar"})
assert.same(args, {server = {"foo", "bar"}}) assert.same({server = {"foo", "bar"}}, args)
end) end)
it("handles short option correclty", function() it("handles short option correclty", function()
local parser = argparse.parser() local parser = argparse.parser()
parser:option("-s", "--server") parser:option("-s", "--server")
local args = parser:parse({"-s", "foo"}) local args = parser:parse({"-s", "foo"})
assert.same(args, {server = "foo"}) assert.same({server = "foo"}, args)
end) end)
it("handles flag correclty", function() it("handles flag correclty", function()
local parser = argparse.parser() local parser = argparse.parser()
parser:flag("-q", "--quiet") parser:flag("-q", "--quiet")
local args = parser:parse({"--quiet"}) local args = parser:parse({"--quiet"})
assert.same(args, {quiet = true}) assert.same({quiet = true}, args)
local args = parser:parse({})
assert.same({}, args)
end) end)
it("handles combined flags correclty", function() it("handles combined flags correclty", function()
@@ -60,14 +62,14 @@ describe("tests related to options", function()
parser:flag("-q", "--quiet") parser:flag("-q", "--quiet")
parser:flag("-f", "--fast") parser:flag("-f", "--fast")
local args = parser:parse({"-qf"}) local args = parser:parse({"-qf"})
assert.same(args, {quiet = true, fast = true}) assert.same({quiet = true, fast = true}, args)
end) end)
it("handles short options without space between option and argument", function() it("handles short options without space between option and argument", function()
local parser = argparse.parser() local parser = argparse.parser()
parser:option("-s", "--server") parser:option("-s", "--server")
local args = parser:parse({"-sfoo"}) local args = parser:parse({"-sfoo"})
assert.same(args, {server = "foo"}) assert.same({server = "foo"}, args)
end) end)
it("handles flags combined with short option correclty", function() it("handles flags combined with short option correclty", function()
@@ -75,7 +77,7 @@ describe("tests related to options", function()
parser:flag("-q", "--quiet") parser:flag("-q", "--quiet")
parser:option("-s", "--server") parser:option("-s", "--server")
local args = parser:parse({"-qsfoo"}) local args = parser:parse({"-qsfoo"})
assert.same(args, {quiet = true, server = "foo"}) assert.same({quiet = true, server = "foo"}, args)
end) end)
describe("Options with optional argument", function() describe("Options with optional argument", function()
@@ -85,7 +87,7 @@ describe("tests related to options", function()
args = "?" args = "?"
}) })
local args = parser:parse({}) local args = parser:parse({})
assert.same(args, {}) assert.same({}, args)
end) end)
it("handles option without argument correctly", function() it("handles option without argument correctly", function()
@@ -94,7 +96,7 @@ describe("tests related to options", function()
args = "?" args = "?"
}) })
local args = parser:parse({"-p"}) local args = parser:parse({"-p"})
assert.same(args, {password = {}}) assert.same({password = {}}, args)
end) end)
it("handles option with argument correctly", function() it("handles option with argument correctly", function()
@@ -103,7 +105,7 @@ describe("tests related to options", function()
args = "?" args = "?"
}) })
local args = parser:parse({"-p", "password"}) local args = parser:parse({"-p", "password"})
assert.same(args, {password = {"password"}}) assert.same({password = {"password"}}, args)
end) end)
end) end)
@@ -113,7 +115,7 @@ describe("tests related to options", function()
args = 2 args = 2
}) })
local args = parser:parse({"--pair", "Alice", "Bob"}) local args = parser:parse({"--pair", "Alice", "Bob"})
assert.same(args, {pair = {"Alice", "Bob"}}) assert.same({pair = {"Alice", "Bob"}}, args)
end) end)
describe("Multi-count options", function() describe("Multi-count options", function()
@@ -123,7 +125,7 @@ describe("tests related to options", function()
count = "*" count = "*"
}) })
local args = parser:parse({"-efoo", "--exclude=bar", "-e", "baz"}) local args = parser:parse({"-efoo", "--exclude=bar", "-e", "baz"})
assert.same(args, {exclude = {"foo", "bar", "baz"}}) assert.same({exclude = {"foo", "bar", "baz"}}, args)
end) end)
it("handles not used multi-count option correctly", function() it("handles not used multi-count option correctly", function()
@@ -132,7 +134,7 @@ describe("tests related to options", function()
count = "*" count = "*"
}) })
local args = parser:parse({}) local args = parser:parse({})
assert.same(args, {exclude = {}}) assert.same({exclude = {}}, args)
end) end)
it("handles multi-count multi-argument option correctly", function() it("handles multi-count multi-argument option correctly", function()
@@ -142,7 +144,7 @@ describe("tests related to options", function()
args = 2 args = 2
}) })
local args = parser:parse({"-e", "Alice", "Bob", "-e", "Emma", "Jacob"}) 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) end)
it("handles multi-count flag correctly", function() it("handles multi-count flag correctly", function()
@@ -151,7 +153,7 @@ describe("tests related to options", function()
count = "*" count = "*"
}) })
local args = parser:parse({"-qq", "--quiet"}) local args = parser:parse({"-qq", "--quiet"})
assert.same(args, {quiet = 3}) assert.same({quiet = 3}, args)
end) end)
it("overwrites old invocations", function() it("overwrites old invocations", function()
@@ -160,7 +162,7 @@ describe("tests related to options", function()
count = "0-2" count = "0-2"
}) })
local args = parser:parse({"-uAlice", "--user=Bob", "--user", "John"}) local args = parser:parse({"-uAlice", "--user=Bob", "--user", "John"})
assert.same(args, {user = {"Bob", "John"}}) assert.same({user = {"Bob", "John"}}, args)
end) end)
it("handles not used multi-count flag correctly", function() it("handles not used multi-count flag correctly", function()
@@ -169,7 +171,7 @@ describe("tests related to options", function()
count = "*" count = "*"
}) })
local args = parser:parse({}) local args = parser:parse({})
assert.same(args, {quiet = 0}) assert.same({quiet = 0}, args)
end) end)
end) end)
end) end)

View File

@@ -221,11 +221,14 @@ function Parser:parse(args)
local parser local parser
local charset local charset
local options = {} local options = {}
local arguments, commands local arguments = {}
local commands
local opt_context = {} local opt_context = {}
local com_context local com_context
local result = {} 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 function close(element)
local invocations = result[element.target] local invocations = result[element.target]
@@ -291,15 +294,13 @@ function Parser:parse(args)
result[option.target] = {} result[option.target] = {}
end end
arguments = p.arguments for _, argument in ipairs(p.arguments) do
cur_arg_i = 1 table.insert(arguments, argument)
cur_arg = arguments[cur_arg_i]
for _, argument in ipairs(arguments) do
result[argument.target] = {} result[argument.target] = {}
invoke(argument) invoke(argument)
end end
cur_arg = arguments[cur_arg_i]
commands = p.commands commands = p.commands
com_context = {} com_context = {}
@@ -396,7 +397,6 @@ function Parser:parse(args)
end end
local function format() local function format()
local new_result = {}
local invocations local invocations
for _, elements in ipairs{options, arguments} do for _, elements in ipairs{options, arguments} do
@@ -409,32 +409,32 @@ function Parser:parse(args)
if element.maxcount == 1 then if element.maxcount == 1 then
if element.maxargs == 0 then if element.maxargs == 0 then
if #invocations > 0 then if #invocations > 0 then
new_result[element.target] = true result[element.target] = true
else
result[element.target] = nil
end end
elseif element.maxargs == 1 and element.minargs == 1 then elseif element.maxargs == 1 and element.minargs == 1 then
if #invocations > 0 then if #invocations > 0 then
new_result[element.target] = invocations[1][1] result[element.target] = invocations[1][1]
else
result[element.target] = nil
end end
else else
new_result[element.target] = invocations[1] result[element.target] = invocations[1]
end end
else else
if element.maxargs == 0 then if element.maxargs == 0 then
new_result[element.target] = #invocations result[element.target] = #invocations
elseif element.maxargs == 1 and element.minargs == 1 then elseif element.maxargs == 1 and element.minargs == 1 then
new_result[element.target] = {} for i=1, #invocations do
invocations[i] = invocations[i][1]
for _, passed in ipairs(invocations) do
table.insert(new_result[element.target], passed[1])
end end
else else
new_result[element.target] = invocations result[element.target] = invocations
end end
end end
end end
end end
result = new_result
end end
switch(self) switch(self)