return Parser as the module

This commit is contained in:
mpeterv
2014-02-17 18:41:09 +04:00
parent 3788b9c1a6
commit ccad58ef50
12 changed files with 109 additions and 117 deletions

View File

@@ -1,11 +1,11 @@
local argparse = require "argparse" local Parser = require "argparse"
describe("tests related to actions", function() describe("tests related to actions", function()
it("calls actions for options", function() it("calls actions for options", function()
local action1 = spy.new(function(x) end) local action1 = spy.new(function(x) end)
local action2 = spy.new(function(x) end) local action2 = spy.new(function(x) end)
local parser = argparse.parser() local parser = Parser()
parser:option "-f" "--from" { parser:option "-f" "--from" {
action = action1 action = action1
} }
@@ -29,7 +29,7 @@ describe("tests related to actions", function()
local action2 = spy.new(function(x) end) local action2 = spy.new(function(x) end)
local action3 = spy.new(function(x) end) local action3 = spy.new(function(x) end)
local parser = argparse.parser() local parser = Parser()
parser:flag "-v" "--verbose" { parser:flag "-v" "--verbose" {
action = action1, action = action1,
count = "0-3" count = "0-3"
@@ -52,7 +52,7 @@ describe("tests related to actions", function()
local action1 = spy.new(function(x) end) local action1 = spy.new(function(x) end)
local action2 = spy.new(function(x) end) local action2 = spy.new(function(x) end)
local parser = argparse.parser() local parser = Parser()
parser:argument "input" { parser:argument "input" {
action = action1 action = action1
} }
@@ -73,7 +73,7 @@ describe("tests related to actions", function()
local action1 = spy.new(function(x) end) local action1 = spy.new(function(x) end)
local action2 = spy.new(function(x) end) local action2 = spy.new(function(x) end)
local parser = argparse.parser "name" { local parser = Parser "name" {
action = action1 action = action1
} }
parser:flag "-v" "--verbose" { parser:flag "-v" "--verbose" {

View File

@@ -1,22 +1,22 @@
local argparse = require "argparse" local Parser = require "argparse"
describe("tests related to positional arguments", function() describe("tests related to positional arguments", function()
describe("passing correct arguments", function() describe("passing correct arguments", function()
it("handles empty parser correctly", function() it("handles empty parser correctly", function()
local parser = argparse.parser() local parser = 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 = Parser()
parser:argument "foo" parser:argument "foo"
local args = parser:parse({"bar"}) local args = parser:parse({"bar"})
assert.same({foo = "bar"}, args) assert.same({foo = "bar"}, args)
end) end)
it("handles optional argument correctly", function() it("handles optional argument correctly", function()
local parser = argparse.parser() local parser = Parser()
parser:argument "foo" parser:argument "foo"
:args "?" :args "?"
local args = parser:parse({"bar"}) local args = parser:parse({"bar"})
@@ -24,7 +24,7 @@ describe("tests related to positional arguments", function()
end) end)
it("handles several arguments correctly", function() it("handles several arguments correctly", function()
local parser = argparse.parser() local parser = Parser()
parser:argument "foo1" parser:argument "foo1"
parser:argument "foo2" parser:argument "foo2"
local args = parser:parse({"bar", "baz"}) local args = parser:parse({"bar", "baz"})
@@ -32,7 +32,7 @@ describe("tests related to positional arguments", function()
end) end)
it("handles multi-argument correctly", function() it("handles multi-argument correctly", function()
local parser = argparse.parser() local parser = Parser()
parser:argument("foo", { parser:argument("foo", {
args = "*" args = "*"
}) })
@@ -41,7 +41,7 @@ describe("tests related to positional arguments", function()
end) end)
it("handles restrained multi-argument correctly", function() it("handles restrained multi-argument correctly", function()
local parser = argparse.parser() local parser = Parser()
parser:argument("foo", { parser:argument("foo", {
args = "2-4" args = "2-4"
}) })
@@ -50,7 +50,7 @@ describe("tests related to positional arguments", function()
end) end)
it("handles several multi-arguments correctly", function() it("handles several multi-arguments correctly", function()
local parser = argparse.parser() local parser = Parser()
parser:argument("foo1", { parser:argument("foo1", {
args = "1-2" args = "1-2"
}) })
@@ -64,14 +64,14 @@ describe("tests related to positional arguments", function()
end) end)
it("handles hyphen correctly", function() it("handles hyphen correctly", function()
local parser = argparse.parser() local parser = Parser()
parser:argument "foo" parser:argument "foo"
local args = parser:parse({"-"}) local args = parser:parse({"-"})
assert.same({foo = "-"}, args) assert.same({foo = "-"}, args)
end) end)
it("handles double hyphen correctly", function() it("handles double hyphen correctly", function()
local parser = argparse.parser() local parser = Parser()
parser:argument "foo" parser:argument "foo"
local args = parser:parse({"--", "-q"}) local args = parser:parse({"--", "-q"})
assert.same({foo = "-q"}, args) assert.same({foo = "-q"}, args)
@@ -80,27 +80,27 @@ describe("tests related to positional arguments", function()
describe("passing incorrect arguments", function() describe("passing incorrect arguments", function()
it("handles extra arguments with empty parser correctly", function() it("handles extra arguments with empty parser correctly", function()
local parser = argparse.parser() local parser = Parser()
assert.has_error(function() parser:parse{"foo"} end, "too many arguments") assert.has_error(function() parser:parse{"foo"} end, "too many arguments")
end) end)
it("handles extra arguments with one argument correctly", function() it("handles extra arguments with one argument correctly", function()
local parser = argparse.parser() local parser = Parser()
parser:argument "foo" parser:argument "foo"
assert.has_error(function() parser:parse{"bar", "baz"} end, "too many arguments") assert.has_error(function() parser:parse{"bar", "baz"} end, "too many arguments")
end) end)
it("handles too few arguments with one argument correctly", function() it("handles too few arguments with one argument correctly", function()
local parser = argparse.parser() local parser = Parser()
parser:argument "foo" parser:argument "foo"
assert.has_error(function() parser:parse{} end, "too few arguments") assert.has_error(function() parser:parse{} end, "too few arguments")
end) end)
it("handles extra arguments with several arguments correctly", function() it("handles extra arguments with several arguments correctly", function()
local parser = argparse.parser() local parser = Parser()
parser:argument "foo1" parser:argument "foo1"
parser:argument "foo2" parser:argument "foo2"
@@ -108,7 +108,7 @@ describe("tests related to positional arguments", function()
end) end)
it("handles too few arguments with several arguments correctly", function() it("handles too few arguments with several arguments correctly", function()
local parser = argparse.parser() local parser = Parser()
parser:argument "foo1" parser:argument "foo1"
parser:argument "foo2" parser:argument "foo2"
@@ -116,7 +116,7 @@ describe("tests related to positional arguments", function()
end) end)
it("handles too few arguments with multi-argument correctly", function() it("handles too few arguments with multi-argument correctly", function()
local parser = argparse.parser() local parser = Parser()
parser:argument "foo" { parser:argument "foo" {
args = "+" args = "+"
} }
@@ -124,7 +124,7 @@ describe("tests related to positional arguments", function()
end) end)
it("handles too many arguments with multi-argument correctly", function() it("handles too many arguments with multi-argument correctly", function()
local parser = argparse.parser() local parser = Parser()
parser:argument "foo" { parser:argument "foo" {
args = "2-4" args = "2-4"
} }
@@ -132,7 +132,7 @@ describe("tests related to positional arguments", function()
end) end)
it("handles too few arguments with multi-argument correctly", function() it("handles too few arguments with multi-argument correctly", function()
local parser = argparse.parser() local parser = Parser()
parser:argument("foo", { parser:argument("foo", {
args = "2-4" args = "2-4"
}) })
@@ -140,7 +140,7 @@ describe("tests related to positional arguments", function()
end) end)
it("handles too many arguments with several multi-arguments correctly", function() it("handles too many arguments with several multi-arguments correctly", function()
local parser = argparse.parser() local parser = Parser()
parser:argument("foo1", { parser:argument("foo1", {
args = "1-2" args = "1-2"
}) })
@@ -151,7 +151,7 @@ describe("tests related to positional arguments", function()
end) end)
it("handles too few arguments with several multi-arguments correctly", function() it("handles too few arguments with several multi-arguments correctly", function()
local parser = argparse.parser() local parser = Parser()
parser:argument("foo1", { parser:argument("foo1", {
args = "1-2" args = "1-2"
}) })

View File

@@ -1,8 +1,8 @@
local argparse = require "argparse" local Parser = require "argparse"
describe("tests related to commands", function() describe("tests related to commands", function()
it("handles commands after arguments", function() it("handles commands after arguments", function()
local parser = argparse.parser "name" local parser = Parser "name"
parser:argument "file" parser:argument "file"
parser:command "create" parser:command "create"
parser:command "remove" parser:command "remove"
@@ -12,7 +12,7 @@ describe("tests related to commands", function()
end) end)
it("switches context properly", function() it("switches context properly", function()
local parser = argparse.parser "name" local parser = Parser "name"
:add_help(false) :add_help(false)
local install = parser:command "install" local install = parser:command "install"
install:flag "-q" "--quiet" install:flag "-q" "--quiet"
@@ -23,7 +23,7 @@ describe("tests related to commands", function()
end) end)
it("allows to continue passing old options", function() it("allows to continue passing old options", function()
local parser = argparse.parser "name" local parser = Parser "name"
parser:flag "-v" "--verbose" { parser:flag "-v" "--verbose" {
count = "*" count = "*"
} }
@@ -34,7 +34,7 @@ describe("tests related to commands", function()
end) end)
it("handles nested commands", function() it("handles nested commands", function()
local parser = argparse.parser "name" local parser = Parser "name"
local foo = parser:command "foo" local foo = parser:command "foo"
local bar = foo:command "bar" local bar = foo:command "bar"
local baz = foo:command "baz" local baz = foo:command "baz"
@@ -44,7 +44,7 @@ describe("tests related to commands", function()
end) end)
it("handles no commands depending on parser.require_command", function() it("handles no commands depending on parser.require_command", function()
local parser = argparse.parser "name" local parser = Parser "name"
parser:command "install" parser:command "install"
local args = parser:parse{} local args = parser:parse{}
@@ -55,7 +55,7 @@ describe("tests related to commands", function()
end) end)
it("Detects wrong commands", function() it("Detects wrong commands", function()
local parser = argparse.parser "name" local parser = Parser "name"
local install = parser:command "install" local install = parser:command "install"
assert.has_error(function() parser:parse{"run"} end, "unknown command 'run'") assert.has_error(function() parser:parse{"run"} end, "unknown command 'run'")

View File

@@ -1,8 +1,8 @@
local argparse = require "argparse" local Parser = require "argparse"
describe("tests related to converters", function() describe("tests related to converters", function()
it("converts arguments", function() it("converts arguments", function()
local parser = argparse.parser() local parser = Parser()
parser:argument "numbers" { parser:argument "numbers" {
convert = tonumber, convert = tonumber,
args = "+" args = "+"
@@ -13,7 +13,7 @@ describe("tests related to converters", function()
end) end)
it("raises an error when it can't convert", function() it("raises an error when it can't convert", function()
local parser = argparse.parser() local parser = Parser()
parser:argument "numbers" { parser:argument "numbers" {
convert = tonumber, convert = tonumber,
args = "+" args = "+"
@@ -23,7 +23,7 @@ describe("tests related to converters", function()
end) end)
it("second return value is used as error message", function() it("second return value is used as error message", function()
local parser = argparse.parser() local parser = Parser()
parser:argument "numbers" { parser:argument "numbers" {
convert = function(x) return tonumber(x), x .. " is not a number" end convert = function(x) return tonumber(x), x .. " is not a number" end
} }

View File

@@ -1,9 +1,9 @@
local argparse = require "argparse" local Parser = require "argparse"
describe("tests related to default values", function() describe("tests related to default values", function()
describe("default values for arguments", function() describe("default values for arguments", function()
it("handles default argument correctly", function() it("handles default argument correctly", function()
local parser = argparse.parser() local parser = Parser()
parser:argument("foo", { parser:argument("foo", {
default = "bar" default = "bar"
}) })
@@ -12,7 +12,7 @@ describe("tests related to default values", function()
end) end)
it("handles default multi-argument correctly", function() it("handles default multi-argument correctly", function()
local parser = argparse.parser() local parser = Parser()
parser:argument("foo", { parser:argument("foo", {
args = 3, args = 3,
default = "bar" default = "bar"
@@ -22,7 +22,7 @@ describe("tests related to default values", function()
end) end)
it("does not use default values if not needed", function() it("does not use default values if not needed", function()
local parser = argparse.parser() local parser = Parser()
parser:argument("foo", { parser:argument("foo", {
args = "1-2", args = "1-2",
default = "bar" default = "bar"
@@ -34,7 +34,7 @@ describe("tests related to default values", function()
describe("default values for options", function() describe("default values for options", function()
it("handles option with default value correctly", function() it("handles option with default value correctly", function()
local parser = argparse.parser() local parser = Parser()
parser:option("-f", "--foo", { parser:option("-f", "--foo", {
default = "bar" default = "bar"
}) })
@@ -43,7 +43,7 @@ describe("tests related to default values", function()
end) end)
it("doesn't use default if option is not invoked", function() it("doesn't use default if option is not invoked", function()
local parser = argparse.parser() local parser = Parser()
parser:option("-f", "--foo", { parser:option("-f", "--foo", {
default = "bar" default = "bar"
}) })
@@ -52,7 +52,7 @@ describe("tests related to default values", function()
end) end)
it("handles default multi-argument correctly", function() it("handles default multi-argument correctly", function()
local parser = argparse.parser() local parser = Parser()
parser:option("-f", "--foo", { parser:option("-f", "--foo", {
args = 3, args = 3,
default = "bar" default = "bar"
@@ -62,7 +62,7 @@ describe("tests related to default values", function()
end) end)
it("does not use default values if not needed", function() it("does not use default values if not needed", function()
local parser = argparse.parser() local parser = Parser()
parser:option("-f", "--foo", { parser:option("-f", "--foo", {
args = "1-2", args = "1-2",
default = "bar" default = "bar"
@@ -72,7 +72,7 @@ describe("tests related to default values", function()
end) end)
it("handles multi-count options with default value correctly", function() it("handles multi-count options with default value correctly", function()
local parser = argparse.parser() local parser = Parser()
parser:option("-f", "--foo", { parser:option("-f", "--foo", {
count = "*", count = "*",
default = "bar" default = "bar"

View File

@@ -1,8 +1,8 @@
local argparse = require "argparse" local Parser = require "argparse"
describe("tests related to help message generation", function() describe("tests related to help message generation", function()
it("creates correct help message for empty parser", function() it("creates correct help message for empty parser", function()
local parser = argparse.parser "foo" local parser = Parser "foo"
assert.equal(table.concat({ assert.equal(table.concat({
"Usage: foo [-h]", "Usage: foo [-h]",
"", "",
@@ -12,7 +12,7 @@ describe("tests related to help message generation", function()
end) end)
it("does not create extra help options when :prepare is called several times", function() it("does not create extra help options when :prepare is called several times", function()
local parser = argparse.parser "foo" local parser = Parser "foo"
assert.equal(table.concat({ assert.equal(table.concat({
"Usage: foo [-h]", "Usage: foo [-h]",
"", "",
@@ -22,7 +22,7 @@ describe("tests related to help message generation", function()
end) end)
it("uses custom help option", function() it("uses custom help option", function()
local parser = argparse.parser "foo" local parser = Parser "foo"
:add_help {aliases = {"\\?"}} :add_help {aliases = {"\\?"}}
assert.equal(table.concat({ assert.equal(table.concat({
"Usage: foo [\\?]", "Usage: foo [\\?]",
@@ -33,7 +33,7 @@ describe("tests related to help message generation", function()
end) end)
it("creates correct help message for arguments", function() it("creates correct help message for arguments", function()
local parser = argparse.parser "foo" local parser = Parser "foo"
parser:argument "first" parser:argument "first"
parser:argument "second-and-third" parser:argument "second-and-third"
:args "2" :args "2"
@@ -58,7 +58,7 @@ describe("tests related to help message generation", function()
end) end)
it("creates correct help message for options", function() it("creates correct help message for options", function()
local parser = argparse.parser "foo" local parser = Parser "foo"
parser:flag "-q" "--quiet" parser:flag "-q" "--quiet"
parser:option "--from" parser:option "--from"
:count "1" :count "1"
@@ -77,7 +77,7 @@ describe("tests related to help message generation", function()
end) end)
it("adds margin for multiline descriptions", function() it("adds margin for multiline descriptions", function()
local parser = argparse.parser "foo" local parser = Parser "foo"
parser:flag "-v" parser:flag "-v"
:count "0-2" :count "0-2"
:target "verbosity" :target "verbosity"
@@ -98,7 +98,7 @@ Sets verbosity level.
end) end)
it("creates correct help message for commands", function() it("creates correct help message for commands", function()
local parser = argparse.parser "foo" local parser = Parser "foo"
parser:flag "-q" "--quiet" parser:flag "-q" "--quiet"
local run = parser:command "run" local run = parser:command "run"
:description "Run! " :description "Run! "
@@ -117,7 +117,7 @@ Sets verbosity level.
end) end)
it("creates correct help message for subcommands", function() it("creates correct help message for subcommands", function()
local parser = argparse.parser "foo" local parser = Parser "foo"
parser:flag "-q" "--quiet" parser:flag "-q" "--quiet"
local run = parser:command "run" local run = parser:command "run"
run:option "--where" run:option "--where"
@@ -134,7 +134,7 @@ Sets verbosity level.
end) end)
it("uses message provided by user", function() it("uses message provided by user", function()
local parser = argparse.parser "foo" local parser = Parser "foo"
:help "I don't like your format of help messages" :help "I don't like your format of help messages"
parser:flag "-q" "--quiet" parser:flag "-q" "--quiet"

View File

@@ -1,4 +1,4 @@
local argparse = require "argparse" local Parser = require "argparse"
describe("tests related to CLI behaviour #unsafe", function() describe("tests related to CLI behaviour #unsafe", function()
describe("error messages", function() describe("error messages", function()

View File

@@ -1,30 +1,30 @@
local argparse = require "argparse" local Parser = require "argparse"
describe("tests related to options", function() describe("tests related to options", function()
describe("passing correct options", function() describe("passing correct options", function()
it("handles no options passed correctly", function() it("handles no options passed correctly", function()
local parser = argparse.parser() local parser = 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 = Parser()
parser:option("-s", "--server") parser:option("-s", "--server")
local args = parser:parse({"--server", "foo"}) local args = parser:parse({"--server", "foo"})
assert.same({server = "foo"}, args) 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 = Parser()
parser:option("-s", "--server") parser:option("-s", "--server")
local args = parser:parse({"--server=foo"}) local args = parser:parse({"--server=foo"})
assert.same({server = "foo"}, args) 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()
local parser = argparse.parser() local parser = Parser()
parser:option("-s", "--server", { parser:option("-s", "--server", {
args = "*" args = "*"
}) })
@@ -33,7 +33,7 @@ describe("tests related to options", function()
end) end)
it("handles GNU-style long options for multi-argument options", function() it("handles GNU-style long options for multi-argument options", function()
local parser = argparse.parser() local parser = Parser()
parser:option("-s", "--server", { parser:option("-s", "--server", {
args = "1-2" args = "1-2"
}) })
@@ -42,14 +42,14 @@ describe("tests related to options", function()
end) end)
it("handles short option correclty", function() it("handles short option correclty", function()
local parser = argparse.parser() local parser = Parser()
parser:option("-s", "--server") parser:option("-s", "--server")
local args = parser:parse({"-s", "foo"}) local args = parser:parse({"-s", "foo"})
assert.same({server = "foo"}, args) assert.same({server = "foo"}, args)
end) end)
it("handles flag correclty", function() it("handles flag correclty", function()
local parser = argparse.parser() local parser = Parser()
parser:flag("-q", "--quiet") parser:flag("-q", "--quiet")
local args = parser:parse({"--quiet"}) local args = parser:parse({"--quiet"})
assert.same({quiet = true}, args) assert.same({quiet = true}, args)
@@ -58,7 +58,7 @@ describe("tests related to options", function()
end) end)
it("handles combined flags correclty", function() it("handles combined flags correclty", function()
local parser = argparse.parser() local parser = Parser()
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"})
@@ -66,14 +66,14 @@ describe("tests related to options", function()
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 = Parser()
parser:option("-s", "--server") parser:option("-s", "--server")
local args = parser:parse({"-sfoo"}) local args = parser:parse({"-sfoo"})
assert.same({server = "foo"}, args) 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()
local parser = argparse.parser() local parser = Parser()
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"})
@@ -82,7 +82,7 @@ describe("tests related to options", function()
describe("Special chars set", function() describe("Special chars set", function()
it("handles windows-style options", function() it("handles windows-style options", function()
local parser = argparse.parser() local parser = Parser()
:add_help(false) :add_help(false)
parser:option "\\I" parser:option "\\I"
:count "*" :count "*"
@@ -92,7 +92,7 @@ describe("tests related to options", function()
end) end)
it("corrects charset in commands", function() it("corrects charset in commands", function()
local parser = argparse.parser "name" local parser = Parser "name"
:add_help(false) :add_help(false)
parser:flag "-v" "--verbose" parser:flag "-v" "--verbose"
:count "*" :count "*"
@@ -106,7 +106,7 @@ describe("tests related to options", function()
describe("Options with optional argument", function() describe("Options with optional argument", function()
it("handles emptiness correctly", function() it("handles emptiness correctly", function()
local parser = argparse.parser() local parser = Parser()
parser:option("-p", "--password", { parser:option("-p", "--password", {
args = "?" args = "?"
}) })
@@ -115,7 +115,7 @@ describe("tests related to options", function()
end) end)
it("handles option without argument correctly", function() it("handles option without argument correctly", function()
local parser = argparse.parser() local parser = Parser()
parser:option("-p", "--password", { parser:option("-p", "--password", {
args = "?" args = "?"
}) })
@@ -124,7 +124,7 @@ describe("tests related to options", function()
end) end)
it("handles option with argument correctly", function() it("handles option with argument correctly", function()
local parser = argparse.parser() local parser = Parser()
parser:option("-p", "--password", { parser:option("-p", "--password", {
args = "?" args = "?"
}) })
@@ -134,7 +134,7 @@ describe("tests related to options", function()
end) end)
it("handles multi-argument options correctly", function() it("handles multi-argument options correctly", function()
local parser = argparse.parser() local parser = Parser()
parser:option("--pair", { parser:option("--pair", {
args = 2 args = 2
}) })
@@ -144,7 +144,7 @@ describe("tests related to options", function()
describe("Multi-count options", function() describe("Multi-count options", function()
it("handles multi-count option correctly", function() it("handles multi-count option correctly", function()
local parser = argparse.parser() local parser = Parser()
parser:option("-e", "--exclude", { parser:option("-e", "--exclude", {
count = "*" count = "*"
}) })
@@ -153,7 +153,7 @@ describe("tests related to options", function()
end) end)
it("handles not used multi-count option correctly", function() it("handles not used multi-count option correctly", function()
local parser = argparse.parser() local parser = Parser()
parser:option("-e", "--exclude", { parser:option("-e", "--exclude", {
count = "*" count = "*"
}) })
@@ -162,7 +162,7 @@ describe("tests related to options", function()
end) end)
it("handles multi-count multi-argument option correctly", function() it("handles multi-count multi-argument option correctly", function()
local parser = argparse.parser() local parser = Parser()
parser:option("-e", "--exclude", { parser:option("-e", "--exclude", {
count = "*", count = "*",
args = 2 args = 2
@@ -172,7 +172,7 @@ describe("tests related to options", function()
end) end)
it("handles multi-count flag correctly", function() it("handles multi-count flag correctly", function()
local parser = argparse.parser() local parser = Parser()
parser:flag("-q", "--quiet", { parser:flag("-q", "--quiet", {
count = "*" count = "*"
}) })
@@ -181,7 +181,7 @@ describe("tests related to options", function()
end) end)
it("overwrites old invocations", function() it("overwrites old invocations", function()
local parser = argparse.parser() local parser = Parser()
parser:option("-u", "--user", { parser:option("-u", "--user", {
count = "0-2" count = "0-2"
}) })
@@ -190,7 +190,7 @@ describe("tests related to options", function()
end) end)
it("handles not used multi-count flag correctly", function() it("handles not used multi-count flag correctly", function()
local parser = argparse.parser() local parser = Parser()
parser:flag("-q", "--quiet", { parser:flag("-q", "--quiet", {
count = "*" count = "*"
}) })
@@ -202,13 +202,13 @@ describe("tests related to options", function()
describe("passing incorrect options", function() describe("passing incorrect options", function()
it("handles lack of required argument correctly", function() it("handles lack of required argument correctly", function()
local parser = argparse.parser() local parser = Parser()
parser:option("-s", "--server") parser:option("-s", "--server")
assert.has_error(function() parser:parse{"--server"} end, "too few arguments") assert.has_error(function() parser:parse{"--server"} end, "too few arguments")
end) end)
it("handles unknown options correctly", function() it("handles unknown options correctly", function()
local parser = argparse.parser() local parser = Parser()
:add_help(false) :add_help(false)
parser:option "--option" parser:option "--option"
assert.has_error(function() parser:parse{"--server"} end, "unknown option '--server'") assert.has_error(function() parser:parse{"--server"} end, "unknown option '--server'")
@@ -218,19 +218,19 @@ describe("tests related to options", function()
end) end)
it("handles too many arguments correctly", function() it("handles too many arguments correctly", function()
local parser = argparse.parser() local parser = Parser()
parser:option("-s", "--server") parser:option("-s", "--server")
assert.has_error(function() parser:parse{"-sfoo", "bar"} end, "too many arguments") assert.has_error(function() parser:parse{"-sfoo", "bar"} end, "too many arguments")
end) end)
it("doesn't accept GNU-like long options when it doesn't need arguments", function() it("doesn't accept GNU-like long options when it doesn't need arguments", function()
local parser = argparse.parser() local parser = Parser()
parser:flag("-q", "--quiet") parser:flag("-q", "--quiet")
assert.has_error(function() parser:parse{"--quiet=very_quiet"} end, "option '--quiet' does not take arguments") assert.has_error(function() parser:parse{"--quiet=very_quiet"} end, "option '--quiet' does not take arguments")
end) end)
it("handles too many invocations correctly", function() it("handles too many invocations correctly", function()
local parser = argparse.parser() local parser = Parser()
parser:flag("-q", "--quiet", { parser:flag("-q", "--quiet", {
count = 1, count = 1,
overwrite = false overwrite = false
@@ -239,7 +239,7 @@ describe("tests related to options", function()
end) end)
it("handles too few invocations correctly", function() it("handles too few invocations correctly", function()
local parser = argparse.parser() local parser = Parser()
parser:option("-f", "--foo", { parser:option("-f", "--foo", {
count = "3-4" count = "3-4"
}) })

View File

@@ -1,7 +1,7 @@
#!/usr/bin/env lua #!/usr/bin/env lua
local argparse = require "argparse" local Parser = require "argparse"
local parser = argparse.parser "test" local parser = Parser "test"
:description "A testing program. " :description "A testing program. "
parser:argument "input" parser:argument "input"

View File

@@ -1,9 +1,9 @@
local argparse = require "argparse" local Parser = require "argparse"
describe("tests related to tips", function() describe("tests related to tips", function()
describe("provides tips when data is too long", function() describe("provides tips when data is too long", function()
it("for options", function() it("for options", function()
local parser = argparse.parser() local parser = Parser()
parser:option "-q" "--quiet" parser:option "-q" "--quiet"
assert.has_error(function() parser:parse{"--quiett=true"} end, assert.has_error(function() parser:parse{"--quiett=true"} end,
@@ -11,7 +11,7 @@ describe("tests related to tips", function()
end) end)
it("for commands", function() it("for commands", function()
local parser = argparse.parser "name" local parser = Parser "name"
parser:command "install" parser:command "install"
assert.has_error(function() parser:parse{"installq"} end, assert.has_error(function() parser:parse{"installq"} end,
@@ -21,7 +21,7 @@ describe("tests related to tips", function()
describe("provides tips when data is too short", function() describe("provides tips when data is too short", function()
it("for options", function() it("for options", function()
local parser = argparse.parser() local parser = Parser()
parser:option "-q" "--quiet" parser:option "-q" "--quiet"
assert.has_error(function() parser:parse{"--quet=true"} end, assert.has_error(function() parser:parse{"--quet=true"} end,
@@ -29,7 +29,7 @@ describe("tests related to tips", function()
end) end)
it("for commands", function() it("for commands", function()
local parser = argparse.parser "name" local parser = Parser "name"
parser:command "install" parser:command "install"
assert.has_error(function() parser:parse{"nstall"} end, assert.has_error(function() parser:parse{"nstall"} end,
@@ -39,7 +39,7 @@ describe("tests related to tips", function()
describe("provides tips on substitution", function() describe("provides tips on substitution", function()
it("for options", function() it("for options", function()
local parser = argparse.parser() local parser = Parser()
parser:option "-q" "--quiet" parser:option "-q" "--quiet"
assert.has_error(function() parser:parse{"--qriet=true"} end, assert.has_error(function() parser:parse{"--qriet=true"} end,
@@ -47,7 +47,7 @@ describe("tests related to tips", function()
end) end)
it("for commands", function() it("for commands", function()
local parser = argparse.parser "name" local parser = Parser "name"
parser:command "install" parser:command "install"
assert.has_error(function() parser:parse{"inntall"} end, assert.has_error(function() parser:parse{"inntall"} end,
@@ -57,7 +57,7 @@ describe("tests related to tips", function()
describe("provides tips on transpositions", function() describe("provides tips on transpositions", function()
it("for options", function() it("for options", function()
local parser = argparse.parser() local parser = Parser()
parser:option "-q" "--quiet" parser:option "-q" "--quiet"
assert.has_error(function() parser:parse{"--queit=true"} end, assert.has_error(function() parser:parse{"--queit=true"} end,
@@ -65,7 +65,7 @@ describe("tests related to tips", function()
end) end)
it("for commands", function() it("for commands", function()
local parser = argparse.parser "name" local parser = Parser "name"
parser:command "install" parser:command "install"
assert.has_error(function() parser:parse{"isntall"} end, assert.has_error(function() parser:parse{"isntall"} end,
@@ -75,7 +75,7 @@ describe("tests related to tips", function()
describe("provides multiple tips", function() describe("provides multiple tips", function()
it("for options", function() it("for options", function()
local parser = argparse.parser() local parser = Parser()
parser:option "-q" "--quiet" parser:option "-q" "--quiet"
parser:option "--quick" parser:option "--quick"
@@ -84,7 +84,7 @@ describe("tests related to tips", function()
end) end)
it("for commands", function() it("for commands", function()
local parser = argparse.parser "name" local parser = Parser "name"
parser:command "install" parser:command "install"
parser:command "instant" parser:command "instant"

View File

@@ -1,14 +1,14 @@
local argparse = require "argparse" local Parser = require "argparse"
describe("tests related to usage message generation", function() describe("tests related to usage message generation", function()
it("creates correct usage message for empty parser", function() it("creates correct usage message for empty parser", function()
local parser = argparse.parser "foo" local parser = Parser "foo"
:add_help(false) :add_help(false)
assert.equal(parser:prepare():get_usage(), "Usage: foo") assert.equal(parser:prepare():get_usage(), "Usage: foo")
end) end)
it("creates correct usage message for arguments", function() it("creates correct usage message for arguments", function()
local parser = argparse.parser "foo" local parser = Parser "foo"
:add_help(false) :add_help(false)
parser:argument "first" parser:argument "first"
parser:argument "second-and-third" parser:argument "second-and-third"
@@ -25,7 +25,7 @@ describe("tests related to usage message generation", function()
end) end)
it("creates correct usage message for options", function() it("creates correct usage message for options", function()
local parser = argparse.parser "foo" local parser = Parser "foo"
:add_help(false) :add_help(false)
parser:flag "-q" "--quiet" parser:flag "-q" "--quiet"
parser:option "--from" parser:option "--from"
@@ -40,7 +40,7 @@ describe("tests related to usage message generation", function()
end) end)
it("creates correct usage message for commands", function() it("creates correct usage message for commands", function()
local parser = argparse.parser "foo" local parser = Parser "foo"
:add_help(false) :add_help(false)
parser:flag "-q" "--quiet" parser:flag "-q" "--quiet"
local run = parser:command "run" local run = parser:command "run"
@@ -53,7 +53,7 @@ describe("tests related to usage message generation", function()
end) end)
it("creates correct usage message for subcommands", function() it("creates correct usage message for subcommands", function()
local parser = argparse.parser "foo" local parser = Parser "foo"
:add_help(false) :add_help(false)
parser:flag "-q" "--quiet" parser:flag "-q" "--quiet"
local run = parser:command "run" local run = parser:command "run"
@@ -70,7 +70,7 @@ describe("tests related to usage message generation", function()
describe("usage generation can be customized", function() describe("usage generation can be customized", function()
it("uses message provided by user", function() it("uses message provided by user", function()
local parser = argparse.parser "foo" local parser = Parser "foo"
:usage "Usage: obvious" :usage "Usage: obvious"
:add_help(false) :add_help(false)
parser:flag "-q" "--quiet" parser:flag "-q" "--quiet"
@@ -82,7 +82,7 @@ describe("tests related to usage message generation", function()
end) end)
it("uses per-option message provided by user", function() it("uses per-option message provided by user", function()
local parser = argparse.parser "foo" local parser = Parser "foo"
:add_help(false) :add_help(false)
parser:flag "-q" "--quiet" parser:flag "-q" "--quiet"
:usage "[-q | --quiet]" :usage "[-q | --quiet]"
@@ -94,7 +94,7 @@ describe("tests related to usage message generation", function()
end) end)
it("uses argnames provided by user", function() it("uses argnames provided by user", function()
local parser = argparse.parser "foo" local parser = Parser "foo"
:add_help(false) :add_help(false)
parser:argument "inputs" parser:argument "inputs"
:args "1-2" :args "1-2"

View File

@@ -1,5 +1,3 @@
local argparse = {}
local class = require "30log" local class = require "30log"
local Declarative = {} local Declarative = {}
@@ -637,11 +635,7 @@ function Parser:parse(args)
end end
local function get_option(name) local function get_option(name)
if opt_context[name] then return parser:assert(opt_context[name], "unknown option '%s'%s", name, get_tip(opt_context, name))
return opt_context[name]
else
parser:error("unknown option '%s'%s", name, get_tip(opt_context, name))
end
end end
local function handle_argument(data) local function handle_argument(data)
@@ -758,6 +752,4 @@ function Parser:parse(args)
return result return result
end end
argparse.parser = Parser return Parser
return argparse