Better name inference

This commit is contained in:
mpeterv
2014-03-08 16:25:54 +04:00
parent 7961d68807
commit c605f248ad
3 changed files with 14 additions and 23 deletions

View File

@@ -5,7 +5,7 @@ describe("tests related to CLI behaviour #unsafe", 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>] ...",
"Usage: ./spec/script [-v] [-h] <input> [<command>] ...",
"",
"Error: too few arguments",
""
@@ -16,7 +16,7 @@ describe("tests related to CLI behaviour #unsafe", function()
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>] ...",
"Usage: ./spec/script [-v] [-h] <input> [<command>] ...",
"",
"Error: unknown command 'bar'",
""
@@ -27,7 +27,7 @@ describe("tests related to CLI behaviour #unsafe", function()
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>] ...",
"Usage: ./spec/script [-v] [-h] <input> [<command>] ...",
"",
"Error: option '--verbose' does not take arguments",
""
@@ -38,7 +38,7 @@ describe("tests related to CLI behaviour #unsafe", function()
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>] ...",
"Usage: ./spec/script [-v] [-h] <input> [<command>] ...",
"",
"Error: unknown option '-q'",
"Did you mean one of these: '-h' '-v'?",
@@ -50,7 +50,7 @@ describe("tests related to CLI behaviour #unsafe", function()
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>] ...",
"Usage: ./spec/script [-v] [-h] <input> [<command>] ...",
"",
"Error: unknown command 'nstall'",
"Did you mean 'install'?",
@@ -62,7 +62,7 @@ describe("tests related to CLI behaviour #unsafe", function()
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>]",
"Usage: ./spec/script install [-f <server>] [-h] <rock> [<version>]",
"",
"Error: too few arguments",
""
@@ -73,7 +73,7 @@ describe("tests related to CLI behaviour #unsafe", function()
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>]",
"Usage: ./spec/script install [-f <server>] [-h] <rock> [<version>]",
"",
"Error: unknown option '--form'",
"Did you mean '--from'?",
@@ -87,7 +87,7 @@ describe("tests related to CLI behaviour #unsafe", 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>] ...",
"Usage: ./spec/script [-v] [-h] <input> [<command>] ...",
"",
"A testing program. ",
"",
@@ -108,7 +108,7 @@ describe("tests related to CLI behaviour #unsafe", function()
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>]",
"Usage: ./spec/script install [-f <server>] [-h] <rock> [<version>]",
"",
"Install a rock. ",
"",

View File

@@ -1,7 +1,7 @@
#!/usr/bin/env lua
local Parser = require "argparse"
local parser = Parser "test"
local parser = Parser()
:description "A testing program. "
:require_command(false)
@@ -26,6 +26,8 @@ install:option "-f" "--from"
:description "Fetch the rock from this server. "
:target "server"
local usage = parser:get_usage()
local help = parser:get_help()
local args = parser:parse()
print(args.input)

View File

@@ -27,7 +27,7 @@ do -- Create classes with setters
table.insert(self._aliases, name_or_options)
end
if not self._name then
if not self._aliases or not self._name then
self._name = name_or_options
end
elseif type(name_or_options) == "table" then
@@ -576,13 +576,6 @@ end
function Parser:_parse(args, errhandler)
args = args or arg
local noname
if not self._name then
noname = true
self._name = args[0]
end
local parser
local charset
local options = {}
@@ -889,10 +882,6 @@ function Parser:_parse(args, errhandler)
end
end
if noname then
self._name = nil
end
return result
end
@@ -927,5 +916,5 @@ function Parser:pparse(args)
end
return function(...)
return Parser():add_help(true)(...)
return Parser(arg[0]):add_help(true)(...)
end