mirror of
https://github.com/TangentFoxy/argparse.git
synced 2025-07-28 11:02:20 +00:00
renamed largparse -> argparse; imported testing setup from literal
This commit is contained in:
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
*~
|
||||||
|
*.luac
|
12
.travis.yml
12
.travis.yml
@@ -1,9 +1,15 @@
|
|||||||
language: c
|
language: c
|
||||||
|
|
||||||
|
env:
|
||||||
|
- LUA="Lua 5.1"
|
||||||
|
- LUA="Lua 5.2"
|
||||||
|
- LUA="LuaJIT 2.0"
|
||||||
|
|
||||||
|
before_install:
|
||||||
|
- bash .travis_setup.sh
|
||||||
|
|
||||||
install:
|
install:
|
||||||
- sudo apt-get install luarocks
|
- sudo luarocks make rockspecs/argparse-git-1.rockspec
|
||||||
- sudo luarocks install busted
|
- sudo luarocks install busted
|
||||||
- sudo luarocks install 30log
|
|
||||||
- sudo luarocks make rockspecs/largparse-lrcompat-1.rockspec
|
|
||||||
|
|
||||||
script: "busted spec"
|
script: "busted spec"
|
||||||
|
33
.travis_setup.sh
Normal file
33
.travis_setup.sh
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
# A script for setting up environment for travis-ci testing.
|
||||||
|
# Sets up Lua and Luarocks.
|
||||||
|
# LUA must be "Lua 5.1", "Lua 5.2" or "LuaJIT 2.0".
|
||||||
|
|
||||||
|
if [ "$LUA" == "LuaJIT 2.0" ]; then
|
||||||
|
curl http://luajit.org/download/LuaJIT-2.0.2.tar.gz | tar xz
|
||||||
|
cd LuaJIT-2.0.2
|
||||||
|
make && sudo make install
|
||||||
|
cd ..;
|
||||||
|
else
|
||||||
|
if [ "$LUA" == "Lua 5.1" ]; then
|
||||||
|
curl http://www.lua.org/ftp/lua-5.1.5.tar.gz | tar xz
|
||||||
|
cd lua-5.1.5;
|
||||||
|
elif [ "$LUA" == "Lua 5.2" ]; then
|
||||||
|
curl http://www.lua.org/ftp/lua-5.2.3.tar.gz | tar xz
|
||||||
|
cd lua-5.2.3;
|
||||||
|
fi
|
||||||
|
|
||||||
|
sudo make linux install
|
||||||
|
cd ..;
|
||||||
|
fi
|
||||||
|
|
||||||
|
curl http://luarocks.org/releases/luarocks-2.1.1.tar.gz | tar xz
|
||||||
|
cd luarocks-2.1.1
|
||||||
|
|
||||||
|
if [ "$LUA" == "LuaJIT 2.0" ]; then
|
||||||
|
./configure --lua-suffix=jit --with-lua-include=/usr/local/include/luajit-2.0;
|
||||||
|
else
|
||||||
|
./configure;
|
||||||
|
fi
|
||||||
|
|
||||||
|
make && sudo make install
|
||||||
|
cd ..
|
@@ -1,4 +1,4 @@
|
|||||||
largparse
|
argparse
|
||||||
=========
|
=========
|
||||||
|
|
||||||
Feature-rich command line parser for Lua inspired by argparse for Python.
|
Feature-rich command line parser for Lua inspired by argparse for Python.
|
||||||
@@ -9,9 +9,9 @@ Something already works:
|
|||||||
|
|
||||||
```lua
|
```lua
|
||||||
|
|
||||||
local largparse = require "largparse"
|
local argparse = require "argparse"
|
||||||
|
|
||||||
local parser = largparse.parser()
|
local parser = argparse.parser()
|
||||||
|
|
||||||
parser:argument("input", {
|
parser:argument("input", {
|
||||||
args = 2
|
args = 2
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
package = "largparse"
|
package = "argparse"
|
||||||
version = "wip-1"
|
version = "git-1"
|
||||||
source = {
|
source = {
|
||||||
url = "src"
|
url = "git://github.com/mpeterv/literal.git"
|
||||||
}
|
}
|
||||||
description = {
|
description = {
|
||||||
summary = "*** please specify description summary ***",
|
summary = "*** please specify description summary ***",
|
||||||
@@ -16,8 +16,8 @@ dependencies = {
|
|||||||
build = {
|
build = {
|
||||||
type = "builtin",
|
type = "builtin",
|
||||||
modules = {
|
modules = {
|
||||||
largparse = "src/largparse.lua",
|
argparse = "src/argparse.lua",
|
||||||
["largparse.state"] = "src/state.lua",
|
["argparse.state"] = "src/state.lua",
|
||||||
["largparse.utils"] = "src/utils.lua"
|
["argparse.utils"] = "src/utils.lua"
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -1,22 +0,0 @@
|
|||||||
package = "largparse"
|
|
||||||
version = "lrcompat-1"
|
|
||||||
source = {
|
|
||||||
url = "src"
|
|
||||||
}
|
|
||||||
description = {
|
|
||||||
summary = "*** please specify description summary ***",
|
|
||||||
detailed = "*** please enter a detailed description ***",
|
|
||||||
homepage = "*** please enter a project homepage ***",
|
|
||||||
license = "MIT/X11"
|
|
||||||
}
|
|
||||||
dependencies = {
|
|
||||||
"lua >= 5.1, < 5.3"
|
|
||||||
}
|
|
||||||
build = {
|
|
||||||
type = "builtin",
|
|
||||||
modules = {
|
|
||||||
largparse = "src/largparse.lua",
|
|
||||||
["largparse.state"] = "src/state.lua",
|
|
||||||
["largparse.utils"] = "src/utils.lua"
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,4 +1,4 @@
|
|||||||
local largparse = require "largparse"
|
local argparse = require "argparse"
|
||||||
|
|
||||||
describe("tests related to positional arguments", function()
|
describe("tests related to positional arguments", function()
|
||||||
local function curry(f, ...)
|
local function curry(f, ...)
|
||||||
@@ -8,20 +8,20 @@ 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 = largparse.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 = largparse.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(args, {foo = "bar"})
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it("handles several arguments correctly", function()
|
it("handles several arguments correctly", function()
|
||||||
local parser = largparse.parser()
|
local parser = argparse.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"})
|
||||||
@@ -29,7 +29,7 @@ describe("tests related to positional arguments", function()
|
|||||||
end)
|
end)
|
||||||
|
|
||||||
it("handles multi-argument correctly", function()
|
it("handles multi-argument correctly", function()
|
||||||
local parser = largparse.parser()
|
local parser = argparse.parser()
|
||||||
parser:argument("foo", {
|
parser:argument("foo", {
|
||||||
args = "*"
|
args = "*"
|
||||||
})
|
})
|
||||||
@@ -38,7 +38,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 = largparse.parser()
|
local parser = argparse.parser()
|
||||||
parser:argument("foo", {
|
parser:argument("foo", {
|
||||||
args = "2-4"
|
args = "2-4"
|
||||||
})
|
})
|
||||||
@@ -47,7 +47,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 = largparse.parser()
|
local parser = argparse.parser()
|
||||||
parser:argument("foo1", {
|
parser:argument("foo1", {
|
||||||
args = "1-2"
|
args = "1-2"
|
||||||
})
|
})
|
||||||
@@ -61,14 +61,14 @@ describe("tests related to positional arguments", function()
|
|||||||
end)
|
end)
|
||||||
|
|
||||||
it("handles hyphen correctly", function()
|
it("handles hyphen correctly", function()
|
||||||
local parser = largparse.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(args, {foo = "-"})
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it("handles double hyphen correctly", function()
|
it("handles double hyphen correctly", function()
|
||||||
local parser = largparse.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(args, {foo = "-q"})
|
||||||
@@ -76,45 +76,45 @@ describe("tests related to positional arguments", function()
|
|||||||
end)
|
end)
|
||||||
|
|
||||||
describe("passing incorrect arguments", function()
|
describe("passing incorrect arguments", function()
|
||||||
local old_parser = largparse.parser
|
local old_parser = argparse.parser
|
||||||
|
|
||||||
setup(function()
|
setup(function()
|
||||||
largparse.parser = old_parser:extends()
|
argparse.parser = old_parser:extends()
|
||||||
function largparse.parser:error(fmt, ...)
|
function argparse.parser:error(fmt, ...)
|
||||||
error(fmt:format(...))
|
error(fmt:format(...))
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
|
||||||
it("handles extra arguments with empty parser correctly", function()
|
it("handles extra arguments with empty parser correctly", function()
|
||||||
local parser = largparse.parser()
|
local parser = argparse.parser()
|
||||||
|
|
||||||
assert.has_error(curry(parser.parse, parser, {"foo"}), "too many arguments")
|
assert.has_error(curry(parser.parse, parser, {"foo"}), "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 = largparse.parser()
|
local parser = argparse.parser()
|
||||||
parser:argument "foo"
|
parser:argument "foo"
|
||||||
|
|
||||||
assert.has_error(curry(parser.parse, parser, {"bar", "baz"}), "too many arguments")
|
assert.has_error(curry(parser.parse, parser, {"bar", "baz"}), "too many arguments")
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it("handles sudden option correctly", function()
|
it("handles sudden option correctly", function()
|
||||||
local parser = largparse.parser()
|
local parser = argparse.parser()
|
||||||
parser:argument "foo"
|
parser:argument "foo"
|
||||||
|
|
||||||
assert.has_error(curry(parser.parse, parser, {"-q"}), "unknown option -q")
|
assert.has_error(curry(parser.parse, parser, {"-q"}), "unknown option -q")
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it("handles too few arguments with one argument correctly", function()
|
it("handles too few arguments with one argument correctly", function()
|
||||||
local parser = largparse.parser()
|
local parser = argparse.parser()
|
||||||
parser:argument "foo"
|
parser:argument "foo"
|
||||||
|
|
||||||
assert.has_error(curry(parser.parse, parser, {}), "too few arguments")
|
assert.has_error(curry(parser.parse, parser, {}), "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 = largparse.parser()
|
local parser = argparse.parser()
|
||||||
parser:argument "foo1"
|
parser:argument "foo1"
|
||||||
parser:argument "foo2"
|
parser:argument "foo2"
|
||||||
|
|
||||||
@@ -122,7 +122,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 = largparse.parser()
|
local parser = argparse.parser()
|
||||||
parser:argument "foo1"
|
parser:argument "foo1"
|
||||||
parser:argument "foo2"
|
parser:argument "foo2"
|
||||||
|
|
||||||
@@ -130,7 +130,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 = largparse.parser()
|
local parser = argparse.parser()
|
||||||
parser:argument("foo", {
|
parser:argument("foo", {
|
||||||
args = "+"
|
args = "+"
|
||||||
})
|
})
|
||||||
@@ -138,7 +138,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 = largparse.parser()
|
local parser = argparse.parser()
|
||||||
parser:argument("foo", {
|
parser:argument("foo", {
|
||||||
args = "2-4"
|
args = "2-4"
|
||||||
})
|
})
|
||||||
@@ -146,7 +146,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 = largparse.parser()
|
local parser = argparse.parser()
|
||||||
parser:argument("foo", {
|
parser:argument("foo", {
|
||||||
args = "2-4"
|
args = "2-4"
|
||||||
})
|
})
|
||||||
@@ -154,7 +154,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 = largparse.parser()
|
local parser = argparse.parser()
|
||||||
parser:argument("foo1", {
|
parser:argument("foo1", {
|
||||||
args = "1-2"
|
args = "1-2"
|
||||||
})
|
})
|
||||||
@@ -165,7 +165,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 = largparse.parser()
|
local parser = argparse.parser()
|
||||||
parser:argument("foo1", {
|
parser:argument("foo1", {
|
||||||
args = "1-2"
|
args = "1-2"
|
||||||
})
|
})
|
||||||
|
@@ -1,9 +1,9 @@
|
|||||||
local largparse = require "largparse"
|
local argparse = 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 = largparse.parser()
|
local parser = argparse.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 = largparse.parser()
|
local parser = argparse.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 = largparse.parser()
|
local parser = argparse.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 = largparse.parser()
|
local parser = argparse.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 = largparse.parser()
|
local parser = argparse.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 = largparse.parser()
|
local parser = argparse.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 = largparse.parser()
|
local parser = argparse.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 = largparse.parser()
|
local parser = argparse.parser()
|
||||||
parser:option("-f", "--foo", {
|
parser:option("-f", "--foo", {
|
||||||
count = "*",
|
count = "*",
|
||||||
default = "bar"
|
default = "bar"
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
local largparse = require "largparse"
|
local argparse = require "argparse"
|
||||||
|
|
||||||
describe("tests related to options", function()
|
describe("tests related to options", function()
|
||||||
local function curry(f, ...)
|
local function curry(f, ...)
|
||||||
@@ -8,28 +8,28 @@ 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 = largparse.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 = largparse.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(args, {server = "foo"})
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it("handles GNU-style long options", function()
|
it("handles GNU-style long options", function()
|
||||||
local parser = largparse.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(args, {server = "foo"})
|
||||||
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 = largparse.parser()
|
local parser = argparse.parser()
|
||||||
parser:option("-s", "--server", {
|
parser:option("-s", "--server", {
|
||||||
args = "*"
|
args = "*"
|
||||||
})
|
})
|
||||||
@@ -38,7 +38,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 = largparse.parser()
|
local parser = argparse.parser()
|
||||||
parser:option("-s", "--server", {
|
parser:option("-s", "--server", {
|
||||||
args = "1-2"
|
args = "1-2"
|
||||||
})
|
})
|
||||||
@@ -47,21 +47,21 @@ describe("tests related to options", function()
|
|||||||
end)
|
end)
|
||||||
|
|
||||||
it("handles short option correclty", function()
|
it("handles short option correclty", function()
|
||||||
local parser = largparse.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(args, {server = "foo"})
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it("handles flag correclty", function()
|
it("handles flag correclty", function()
|
||||||
local parser = largparse.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(args, {quiet = true})
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it("handles combined flags correclty", function()
|
it("handles combined flags correclty", function()
|
||||||
local parser = largparse.parser()
|
local parser = argparse.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"})
|
||||||
@@ -69,14 +69,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 = largparse.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(args, {server = "foo"})
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it("handles flags combined with short option correclty", function()
|
it("handles flags combined with short option correclty", function()
|
||||||
local parser = largparse.parser()
|
local parser = argparse.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"})
|
||||||
@@ -85,7 +85,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 = largparse.parser()
|
local parser = argparse.parser()
|
||||||
parser:option("-p", "--password", {
|
parser:option("-p", "--password", {
|
||||||
args = "?"
|
args = "?"
|
||||||
})
|
})
|
||||||
@@ -94,7 +94,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 = largparse.parser()
|
local parser = argparse.parser()
|
||||||
parser:option("-p", "--password", {
|
parser:option("-p", "--password", {
|
||||||
args = "?"
|
args = "?"
|
||||||
})
|
})
|
||||||
@@ -103,7 +103,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 = largparse.parser()
|
local parser = argparse.parser()
|
||||||
parser:option("-p", "--password", {
|
parser:option("-p", "--password", {
|
||||||
args = "?"
|
args = "?"
|
||||||
})
|
})
|
||||||
@@ -113,7 +113,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 = largparse.parser()
|
local parser = argparse.parser()
|
||||||
parser:option("--pair", {
|
parser:option("--pair", {
|
||||||
args = 2
|
args = 2
|
||||||
})
|
})
|
||||||
@@ -123,7 +123,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 = largparse.parser()
|
local parser = argparse.parser()
|
||||||
parser:option("-e", "--exclude", {
|
parser:option("-e", "--exclude", {
|
||||||
count = "*"
|
count = "*"
|
||||||
})
|
})
|
||||||
@@ -132,7 +132,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 = largparse.parser()
|
local parser = argparse.parser()
|
||||||
parser:option("-e", "--exclude", {
|
parser:option("-e", "--exclude", {
|
||||||
count = "*"
|
count = "*"
|
||||||
})
|
})
|
||||||
@@ -141,7 +141,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 = largparse.parser()
|
local parser = argparse.parser()
|
||||||
parser:option("-e", "--exclude", {
|
parser:option("-e", "--exclude", {
|
||||||
count = "*",
|
count = "*",
|
||||||
args = 2
|
args = 2
|
||||||
@@ -151,7 +151,7 @@ describe("tests related to options", function()
|
|||||||
end)
|
end)
|
||||||
|
|
||||||
it("handles multi-count option with optional argument correctly", function()
|
it("handles multi-count option with optional argument correctly", function()
|
||||||
local parser = largparse.parser()
|
local parser = argparse.parser()
|
||||||
parser:option("-w", "--why", "--why-would-someone-use-this", {
|
parser:option("-w", "--why", "--why-would-someone-use-this", {
|
||||||
count = "*",
|
count = "*",
|
||||||
args = "?"
|
args = "?"
|
||||||
@@ -161,7 +161,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 = largparse.parser()
|
local parser = argparse.parser()
|
||||||
parser:flag("-q", "--quiet", {
|
parser:flag("-q", "--quiet", {
|
||||||
count = "*"
|
count = "*"
|
||||||
})
|
})
|
||||||
@@ -170,7 +170,7 @@ describe("tests related to options", function()
|
|||||||
end)
|
end)
|
||||||
|
|
||||||
it("overwrites old invocations", function()
|
it("overwrites old invocations", function()
|
||||||
local parser = largparse.parser()
|
local parser = argparse.parser()
|
||||||
parser:option("-u", "--user", {
|
parser:option("-u", "--user", {
|
||||||
count = "0-2"
|
count = "0-2"
|
||||||
})
|
})
|
||||||
@@ -179,7 +179,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 = largparse.parser()
|
local parser = argparse.parser()
|
||||||
parser:flag("-q", "--quiet", {
|
parser:flag("-q", "--quiet", {
|
||||||
count = "*"
|
count = "*"
|
||||||
})
|
})
|
||||||
@@ -190,35 +190,35 @@ describe("tests related to options", function()
|
|||||||
end)
|
end)
|
||||||
|
|
||||||
describe("passing incorrect options", function()
|
describe("passing incorrect options", function()
|
||||||
local old_parser = largparse.parser
|
local old_parser = argparse.parser
|
||||||
|
|
||||||
setup(function()
|
setup(function()
|
||||||
largparse.parser = old_parser:extends()
|
argparse.parser = old_parser:extends()
|
||||||
function largparse.parser:error(fmt, ...)
|
function argparse.parser:error(fmt, ...)
|
||||||
error(fmt:format(...))
|
error(fmt:format(...))
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it("handles lack of required argument correctly", function()
|
it("handles lack of required argument correctly", function()
|
||||||
local parser = largparse.parser()
|
local parser = argparse.parser()
|
||||||
parser:option("-s", "--server")
|
parser:option("-s", "--server")
|
||||||
assert.has_error(curry(parser.parse, parser, {"--server"}), "too few arguments")
|
assert.has_error(curry(parser.parse, parser, {"--server"}), "too few arguments")
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it("handles too many arguments correctly", function()
|
it("handles too many arguments correctly", function()
|
||||||
local parser = largparse.parser()
|
local parser = argparse.parser()
|
||||||
parser:option("-s", "--server")
|
parser:option("-s", "--server")
|
||||||
assert.has_error(curry(parser.parse, parser, {"-sfoo", "bar"}), "too many arguments")
|
assert.has_error(curry(parser.parse, parser, {"-sfoo", "bar"}), "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 = largparse.parser()
|
local parser = argparse.parser()
|
||||||
parser:flag("-q", "--quiet")
|
parser:flag("-q", "--quiet")
|
||||||
assert.has_error(curry(parser.parse, parser, {"--quiet=very_quiet"}), "option --quiet doesn't take arguments")
|
assert.has_error(curry(parser.parse, parser, {"--quiet=very_quiet"}), "option --quiet doesn't take arguments")
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it("handles too many invocations correctly", function()
|
it("handles too many invocations correctly", function()
|
||||||
local parser = largparse.parser()
|
local parser = argparse.parser()
|
||||||
parser:flag("-q", "--quiet", {
|
parser:flag("-q", "--quiet", {
|
||||||
count = 1,
|
count = 1,
|
||||||
no_overwrite = true
|
no_overwrite = true
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
local utils = require "largparse.utils"
|
local utils = require "argparse.utils"
|
||||||
|
|
||||||
describe("tests related to utils.parse_boundaries", function()
|
describe("tests related to utils.parse_boundaries", function()
|
||||||
it("handles * correctly", function()
|
it("handles * correctly", function()
|
||||||
|
@@ -1,9 +1,9 @@
|
|||||||
local largparse = {}
|
local argparse = {}
|
||||||
|
|
||||||
local class = require "30log"
|
local class = require "30log"
|
||||||
|
|
||||||
local State = require "largparse.state"
|
local State = require "argparse.state"
|
||||||
local utils = require "largparse.utils"
|
local utils = require "argparse.utils"
|
||||||
|
|
||||||
local Parser = class()
|
local Parser = class()
|
||||||
|
|
||||||
@@ -262,6 +262,6 @@ function Parser:parse(args)
|
|||||||
return result
|
return result
|
||||||
end
|
end
|
||||||
|
|
||||||
largparse.parser = Parser
|
argparse.parser = Parser
|
||||||
|
|
||||||
return largparse
|
return argparse
|
@@ -1,21 +0,0 @@
|
|||||||
local largparse = require "largparse"
|
|
||||||
local serpent = require "serpent"
|
|
||||||
|
|
||||||
local parser = largparse.parser()
|
|
||||||
|
|
||||||
parser:argument("input", {
|
|
||||||
args = 2
|
|
||||||
})
|
|
||||||
|
|
||||||
parser:mutually_exclusive(
|
|
||||||
parser:flag("-q", "--quiet"),
|
|
||||||
parser:option("-s", "--server")
|
|
||||||
)
|
|
||||||
|
|
||||||
local run = parser:command "run"
|
|
||||||
|
|
||||||
run:flag("-f", "--fast")
|
|
||||||
|
|
||||||
local args = parser:parse()
|
|
||||||
|
|
||||||
print(serpent.block(args))
|
|
Reference in New Issue
Block a user