mirror of
https://github.com/TangentFoxy/argparse.git
synced 2025-07-28 11:02:20 +00:00
added cli test
This commit is contained in:
166
spec/integrity_spec.lua
Normal file
166
spec/integrity_spec.lua
Normal file
@@ -0,0 +1,166 @@
|
|||||||
|
local argparse = require "argparse"
|
||||||
|
|
||||||
|
describe("tests related to CLI behaviour #unsafe", function()
|
||||||
|
describe("error messages", function()
|
||||||
|
it("generates correct error message without arguments", function()
|
||||||
|
local handler = io.popen("./spec/script 2>&1", "r")
|
||||||
|
assert.equal(table.concat({
|
||||||
|
"Usage: test [-v] [-h] <input> [<command>] ...",
|
||||||
|
"",
|
||||||
|
"Error: too few arguments",
|
||||||
|
""
|
||||||
|
}, "\r\n"), handler:read "*a")
|
||||||
|
handler:close()
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("generates correct error message with too many arguments", function()
|
||||||
|
local handler = io.popen("./spec/script foo bar 2>&1", "r")
|
||||||
|
assert.equal(table.concat({
|
||||||
|
"Usage: test [-v] [-h] <input> [<command>] ...",
|
||||||
|
"",
|
||||||
|
"Error: unknown command 'bar'",
|
||||||
|
""
|
||||||
|
}, "\r\n"), handler:read "*a")
|
||||||
|
handler:close()
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("generates correct error message with unexpected argument", function()
|
||||||
|
local handler = io.popen("./spec/script --verbose=true 2>&1", "r")
|
||||||
|
assert.equal(table.concat({
|
||||||
|
"Usage: test [-v] [-h] <input> [<command>] ...",
|
||||||
|
"",
|
||||||
|
"Error: option '--verbose' does not take arguments",
|
||||||
|
""
|
||||||
|
}, "\r\n"), handler:read "*a")
|
||||||
|
handler:close()
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("generates correct error message with unexpected option", function()
|
||||||
|
local handler = io.popen("./spec/script -vq 2>&1", "r")
|
||||||
|
assert.equal(table.concat({
|
||||||
|
"Usage: test [-v] [-h] <input> [<command>] ...",
|
||||||
|
"",
|
||||||
|
"Error: unknown option '-q'",
|
||||||
|
"Did you mean one of these: '-h' '-v'?",
|
||||||
|
""
|
||||||
|
}, "\r\n"), handler:read "*a")
|
||||||
|
handler:close()
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("generates correct error message and tip with unexpected command", function()
|
||||||
|
local handler = io.popen("./spec/script foo nstall 2>&1", "r")
|
||||||
|
assert.equal(table.concat({
|
||||||
|
"Usage: test [-v] [-h] <input> [<command>] ...",
|
||||||
|
"",
|
||||||
|
"Error: unknown command 'nstall'",
|
||||||
|
"Did you mean 'install'?",
|
||||||
|
""
|
||||||
|
}, "\r\n"), handler:read "*a")
|
||||||
|
handler:close()
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("generates correct error message without arguments in command", function()
|
||||||
|
local handler = io.popen("./spec/script foo install 2>&1", "r")
|
||||||
|
assert.equal(table.concat({
|
||||||
|
"Usage: test install [-f <server>] [-h] <rock> [<version>]",
|
||||||
|
"",
|
||||||
|
"Error: too few arguments",
|
||||||
|
""
|
||||||
|
}, "\r\n"), handler:read "*a")
|
||||||
|
handler:close()
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("generates correct error message and tip in command", function()
|
||||||
|
local handler = io.popen("./spec/script foo install bar --form=there 2>&1", "r")
|
||||||
|
assert.equal(table.concat({
|
||||||
|
"Usage: test install [-f <server>] [-h] <rock> [<version>]",
|
||||||
|
"",
|
||||||
|
"Error: unknown option '--form'",
|
||||||
|
"Did you mean '--from'?",
|
||||||
|
""
|
||||||
|
}, "\r\n"), handler:read "*a")
|
||||||
|
handler:close()
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
|
||||||
|
describe("help messages", function()
|
||||||
|
it("generates correct help message", function()
|
||||||
|
local handler = io.popen("./spec/script --help 2>&1", "r")
|
||||||
|
assert.equal(table.concat({
|
||||||
|
"Usage: test [-v] [-h] <input> [<command>] ...",
|
||||||
|
"",
|
||||||
|
"A testing program. ",
|
||||||
|
"",
|
||||||
|
"Arguments: ",
|
||||||
|
" input",
|
||||||
|
"",
|
||||||
|
"Options: ",
|
||||||
|
" -v, --verbose Sets verbosity level. ",
|
||||||
|
" -h, --help Show this help message and exit. ",
|
||||||
|
"",
|
||||||
|
"Commands: ",
|
||||||
|
" install Install a rock. ",
|
||||||
|
""
|
||||||
|
}, "\r\n"), handler:read "*a")
|
||||||
|
handler:close()
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("generates correct help message for command", function()
|
||||||
|
local handler = io.popen("./spec/script foo install --help 2>&1", "r")
|
||||||
|
assert.equal(table.concat({
|
||||||
|
"Usage: test install [-f <server>] [-h] <rock> [<version>]",
|
||||||
|
"",
|
||||||
|
"Install a rock. ",
|
||||||
|
"",
|
||||||
|
"Arguments: ",
|
||||||
|
" rock Name of the rock. ",
|
||||||
|
" version Version of the rock. ",
|
||||||
|
"",
|
||||||
|
"Options: ",
|
||||||
|
" -f <server>, --from <server>",
|
||||||
|
" Fetch the rock from this server. ",
|
||||||
|
" -h, --help Show this help message and exit. ",
|
||||||
|
"",
|
||||||
|
}, "\r\n"), handler:read "*a")
|
||||||
|
handler:close()
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
|
||||||
|
describe("data flow", function()
|
||||||
|
it("works with one argument", function()
|
||||||
|
local handler = io.popen("./spec/script foo 2>&1", "r")
|
||||||
|
assert.equal("foo", handler:read "*l")
|
||||||
|
assert.equal("0", handler:read "*l")
|
||||||
|
handler:close()
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("works with one argument and a flag", function()
|
||||||
|
local handler = io.popen("./spec/script -v foo --verbose 2>&1", "r")
|
||||||
|
assert.equal("foo", handler:read "*l")
|
||||||
|
assert.equal("2", handler:read "*l")
|
||||||
|
handler:close()
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("works with command", function()
|
||||||
|
local handler = io.popen("./spec/script foo -v install bar 2>&1", "r")
|
||||||
|
assert.equal("foo", handler:read "*l")
|
||||||
|
assert.equal("1", handler:read "*l")
|
||||||
|
assert.equal("true", handler:read "*l")
|
||||||
|
assert.equal("bar", handler:read "*l")
|
||||||
|
assert.equal("nil", handler:read "*l")
|
||||||
|
assert.equal("nil", handler:read "*l")
|
||||||
|
handler:close()
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("works with command and options", function()
|
||||||
|
local handler = io.popen("./spec/script foo --verbose install bar 0.1 --from=there -vv 2>&1", "r")
|
||||||
|
assert.equal("foo", handler:read "*l")
|
||||||
|
assert.equal("2", handler:read "*l")
|
||||||
|
assert.equal("true", handler:read "*l")
|
||||||
|
assert.equal("bar", handler:read "*l")
|
||||||
|
assert.equal("0.1", handler:read "*l")
|
||||||
|
assert.equal("there", handler:read "*l")
|
||||||
|
handler:close()
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
end)
|
@@ -201,7 +201,7 @@ describe("tests related to options", function()
|
|||||||
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 = argparse.parser()
|
||||||
parser:flag("-q", "--quiet")
|
parser:flag("-q", "--quiet")
|
||||||
assert.has_error(function() parser:parse{"--quiet=very_quiet"} end, "option '--quiet' doesn't 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()
|
||||||
|
35
spec/script
Executable file
35
spec/script
Executable file
@@ -0,0 +1,35 @@
|
|||||||
|
#!/usr/bin/env lua
|
||||||
|
local argparse = require "argparse"
|
||||||
|
|
||||||
|
local parser = argparse.parser "test"
|
||||||
|
:description "A testing program. "
|
||||||
|
|
||||||
|
parser:argument "input"
|
||||||
|
|
||||||
|
parser:flag "-v" "--verbose"
|
||||||
|
:description "Sets verbosity level. "
|
||||||
|
:target "verbosity"
|
||||||
|
:count "0-2"
|
||||||
|
|
||||||
|
local install = parser:command "install"
|
||||||
|
:description "Install a rock. "
|
||||||
|
|
||||||
|
install:argument "rock"
|
||||||
|
:description "Name of the rock. "
|
||||||
|
|
||||||
|
install:argument "version"
|
||||||
|
:description "Version of the rock. "
|
||||||
|
:args "?"
|
||||||
|
|
||||||
|
install:option "-f" "--from"
|
||||||
|
:description "Fetch the rock from this server. "
|
||||||
|
:target "server"
|
||||||
|
|
||||||
|
local args = parser:parse()
|
||||||
|
|
||||||
|
print(args.input)
|
||||||
|
print(args.verbosity)
|
||||||
|
print(args.install)
|
||||||
|
print(args.rock)
|
||||||
|
print(args.version)
|
||||||
|
print(args.server)
|
@@ -299,7 +299,7 @@ function Parser:prepare()
|
|||||||
self:flag "-h" "--help"
|
self:flag "-h" "--help"
|
||||||
:description "Show this help message and exit. "
|
:description "Show this help message and exit. "
|
||||||
:action(function()
|
:action(function()
|
||||||
print(self:get_help())
|
io.stdout:write(self:get_help() .. "\r\n")
|
||||||
os.exit(0)
|
os.exit(0)
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
@@ -700,7 +700,7 @@ function Parser:parse(args)
|
|||||||
if equal then
|
if equal then
|
||||||
name = data:sub(1, equal-1)
|
name = data:sub(1, equal-1)
|
||||||
option = get_option(name)
|
option = get_option(name)
|
||||||
parser:assert(option._maxargs > 0, "option '%s' doesn't take arguments", name)
|
parser:assert(option._maxargs > 0, "option '%s' does not take arguments", name)
|
||||||
|
|
||||||
handle_option(data:sub(1, equal-1))
|
handle_option(data:sub(1, equal-1))
|
||||||
handle_argument(data:sub(equal+1))
|
handle_argument(data:sub(equal+1))
|
||||||
|
Reference in New Issue
Block a user