Print \n instead of \r\n

This commit is contained in:
mpeterv
2015-03-13 15:12:06 +03:00
parent 9da3d150b1
commit 0da90dc597
5 changed files with 183 additions and 224 deletions

View File

@@ -4,33 +4,30 @@ getmetatable(Parser()).error = function(_, msg) error(msg) end
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 = Parser "foo" local parser = Parser "foo"
assert.equal(table.concat({ assert.equal([[
"Usage: foo [-h]", Usage: foo [-h]
"",
"Options: ", Options:
" -h, --help Show this help message and exit. " -h, --help Show this help message and exit. ]], parser:get_help())
}, "\r\n"), parser:get_help())
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 = Parser "foo" local parser = Parser "foo"
assert.equal(table.concat({ assert.equal([[
"Usage: foo [-h]", Usage: foo [-h]
"",
"Options: ", Options:
" -h, --help Show this help message and exit. " -h, --help Show this help message and exit. ]], parser:get_help())
}, "\r\n"), parser:get_help())
end) end)
it("uses custom help option", function() it("uses custom help option ", function()
local parser = Parser "foo" local parser = Parser "foo"
:add_help "/?" :add_help "/?"
assert.equal(table.concat({ assert.equal([[
"Usage: foo [/?]", Usage: foo [/?]
"",
"Options: ", Options:
" /? Show this help message and exit. " /? Show this help message and exit. ]], parser:get_help())
}, "\r\n"), parser:get_help())
end) end)
it("uses description and epilog", function() it("uses description and epilog", function()
@@ -38,16 +35,15 @@ describe("tests related to help message generation", function()
:description "A description. " :description "A description. "
:epilog "An epilog. " :epilog "An epilog. "
assert.equal(table.concat({ assert.equal([[
"Usage: foo [-h]", Usage: foo [-h]
"",
"A description. ", A description.
"",
"Options: ", Options:
" -h, --help Show this help message and exit. ", -h, --help Show this help message and exit.
"",
"An epilog. " An epilog. ]], parser:get_help())
}, "\r\n"), parser:get_help())
end) end)
it("creates correct help message for arguments", function() it("creates correct help message for arguments", function()
@@ -61,19 +57,18 @@ describe("tests related to help message generation", function()
:description "Optional. " :description "Optional. "
:args "*" :args "*"
assert.equal(table.concat({ assert.equal([[
"Usage: foo [-h] <first> <second-and-third> <second-and-third>", Usage: foo [-h] <first> <second-and-third> <second-and-third>
" [<maybe-fourth>] [<others>] ...", [<maybe-fourth>] [<others>] ...
"",
"Arguments: ", Arguments:
" first", first
" second-and-third", second-and-third
" maybe-fourth", maybe-fourth
" others Optional. ", others Optional.
"",
"Options: ", Options:
" -h, --help Show this help message and exit. " -h, --help Show this help message and exit. ]], parser:get_help())
}, "\r\n"), parser:get_help())
end) end)
it("creates correct help message for options", function() it("creates correct help message for options", function()
@@ -84,15 +79,14 @@ describe("tests related to help message generation", function()
:target "server" :target "server"
parser:option "--config" parser:option "--config"
assert.equal(table.concat({ assert.equal([[
"Usage: foo [-q] --from <from> [--config <config>] [-h]", Usage: foo [-q] --from <from> [--config <config>] [-h]
"",
"Options: ", Options:
" -q, --quiet", -q, --quiet
" --from <from>", --from <from>
" --config <config>", --config <config>
" -h, --help Show this help message and exit. " -h, --help Show this help message and exit. ]], parser:get_help())
}, "\r\n"), parser:get_help())
end) end)
it("adds margin for multiline descriptions", function() it("adds margin for multiline descriptions", function()
@@ -105,15 +99,14 @@ Sets verbosity level.
-v: Report all warnings. -v: Report all warnings.
-vv: Report all debugging information. ]] -vv: Report all debugging information. ]]
assert.equal(table.concat({ assert.equal([[
"Usage: foo [-v] [-h]", Usage: foo [-v] [-h]
"",
"Options: ", Options:
" -v Sets verbosity level. ", -v Sets verbosity level.
" -v: Report all warnings. ", -v: Report all warnings.
" -vv: Report all debugging information. ", -vv: Report all debugging information.
" -h, --help Show this help message and exit. " -h, --help Show this help message and exit. ]], parser:get_help())
}, "\r\n"), parser:get_help())
end) end)
it("shows default values", function() it("shows default values", function()
@@ -124,14 +117,13 @@ Sets verbosity level.
:default "8080" :default "8080"
:description "Port." :description "Port."
assert.equal(table.concat({ assert.equal([[
"Usage: foo [-o <o>] [-p <p>] [-h]", Usage: foo [-o <o>] [-p <p>] [-h]
"",
"Options: ", Options:
" -o <o> default: a.out", -o <o> default: a.out
" -p <p> Port. (default: 8080)", -p <p> Port. (default: 8080)
" -h, --help Show this help message and exit. " -h, --help Show this help message and exit. ]], parser:get_help())
}, "\r\n"), parser:get_help())
end) end)
it("does not show default value when show_default == false", function() it("does not show default value when show_default == false", function()
@@ -144,14 +136,13 @@ Sets verbosity level.
:show_default(false) :show_default(false)
:description "Port. " :description "Port. "
assert.equal(table.concat({ assert.equal([[
"Usage: foo [-o <o>] [-p <p>] [-h]", Usage: foo [-o <o>] [-p <p>] [-h]
"",
"Options: ", Options:
" -o <o>", -o <o>
" -p <p> Port. ", -p <p> Port.
" -h, --help Show this help message and exit. " -h, --help Show this help message and exit. ]], parser:get_help())
}, "\r\n"), parser:get_help())
end) end)
it("creates correct help message for commands", function() it("creates correct help message for commands", function()
@@ -161,16 +152,15 @@ Sets verbosity level.
:description "Run! " :description "Run! "
run:option "--where" run:option "--where"
assert.equal(table.concat({ assert.equal([[
"Usage: foo [-q] [-h] <command> ...", Usage: foo [-q] [-h] <command> ...
"",
"Options: ", Options:
" -q, --quiet", -q, --quiet
" -h, --help Show this help message and exit. ", -h, --help Show this help message and exit.
"",
"Commands: ", Commands:
" run Run! " run Run! ]], parser:get_help())
}, "\r\n"), parser:get_help())
end) end)
it("creates correct help message for subcommands", function() it("creates correct help message for subcommands", function()
@@ -179,13 +169,12 @@ Sets verbosity level.
local run = parser:command "run" local run = parser:command "run"
run:option "--where" run:option "--where"
assert.equal(table.concat({ assert.equal([[
"Usage: foo run [--where <where>] [-h]", Usage: foo run [--where <where>] [-h]
"",
"Options: ", Options:
" --where <where>", --where <where>
" -h, --help Show this help message and exit. ", -h, --help Show this help message and exit. ]], run:get_help())
}, "\r\n"), run:get_help())
end) end)
it("uses message provided by user", function() it("uses message provided by user", function()
@@ -193,9 +182,7 @@ Sets verbosity level.
: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"
assert.equal( assert.equal([[
[=[I don't like your format of help messages]=], I don't like your format of help messages]], parser:get_help())
parser:get_help()
)
end) end)
end) end)

View File

@@ -1,126 +1,106 @@
local function get_output(args)
local handler = io.popen("./spec/script " .. args .. " 2>&1", "r")
local output = handler:read("*a")
handler:close()
return output
end
describe("tests related to CLI behaviour #unsafe", function() describe("tests related to CLI behaviour #unsafe", function()
describe("error messages", function() describe("error messages", function()
it("generates correct error message without arguments", function() it("generates correct error message without arguments", function()
local handler = io.popen("./spec/script 2>&1", "r") assert.equal([[
assert.equal(table.concat({ Usage: ./spec/script [-v] [-h] <input> [<command>] ...
"Usage: ./spec/script [-v] [-h] <input> [<command>] ...",
"", Error: too few arguments
"Error: too few arguments", ]], get_output(""))
""
}, "\r\n"), handler:read "*a")
handler:close()
end) end)
it("generates correct error message with too many arguments", function() it("generates correct error message with too many arguments", function()
local handler = io.popen("./spec/script foo bar 2>&1", "r") assert.equal([[
assert.equal(table.concat({ Usage: ./spec/script [-v] [-h] <input> [<command>] ...
"Usage: ./spec/script [-v] [-h] <input> [<command>] ...",
"", Error: unknown command 'bar'
"Error: unknown command 'bar'", ]], get_output("foo bar"))
""
}, "\r\n"), handler:read "*a")
handler:close()
end) end)
it("generates correct error message with unexpected argument", function() it("generates correct error message with unexpected argument", function()
local handler = io.popen("./spec/script --verbose=true 2>&1", "r") assert.equal([[
assert.equal(table.concat({ Usage: ./spec/script [-v] [-h] <input> [<command>] ...
"Usage: ./spec/script [-v] [-h] <input> [<command>] ...",
"", Error: option '--verbose' does not take arguments
"Error: option '--verbose' does not take arguments", ]], get_output("--verbose=true"))
""
}, "\r\n"), handler:read "*a")
handler:close()
end) end)
it("generates correct error message with unexpected option", function() it("generates correct error message with unexpected option", function()
local handler = io.popen("./spec/script -vq 2>&1", "r") assert.equal([[
assert.equal(table.concat({ Usage: ./spec/script [-v] [-h] <input> [<command>] ...
"Usage: ./spec/script [-v] [-h] <input> [<command>] ...",
"", Error: unknown option '-q'
"Error: unknown option '-q'", Did you mean one of these: '-h' '-v'?
"Did you mean one of these: '-h' '-v'?", ]], get_output("-vq"))
""
}, "\r\n"), handler:read "*a")
handler:close()
end) end)
it("generates correct error message and tip with unexpected command", 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([[
assert.equal(table.concat({ Usage: ./spec/script [-v] [-h] <input> [<command>] ...
"Usage: ./spec/script [-v] [-h] <input> [<command>] ...",
"", Error: unknown command 'nstall'
"Error: unknown command 'nstall'", Did you mean 'install'?
"Did you mean 'install'?", ]], get_output("foo nstall"))
""
}, "\r\n"), handler:read "*a")
handler:close()
end) end)
it("generates correct error message without arguments in command", function() it("generates correct error message without arguments in command", function()
local handler = io.popen("./spec/script foo install 2>&1", "r") assert.equal([[
assert.equal(table.concat({ Usage: ./spec/script install [-f <from>] [-h] <rock> [<version>]
"Usage: ./spec/script install [-f <from>] [-h] <rock> [<version>]",
"", Error: too few arguments
"Error: too few arguments", ]], get_output("foo install"))
""
}, "\r\n"), handler:read "*a")
handler:close()
end) end)
it("generates correct error message and tip in command", 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([[
assert.equal(table.concat({ Usage: ./spec/script install [-f <from>] [-h] <rock> [<version>]
"Usage: ./spec/script install [-f <from>] [-h] <rock> [<version>]",
"", Error: unknown option '--form'
"Error: unknown option '--form'", Did you mean '--from'?
"Did you mean '--from'?", ]], get_output("foo install bar --form=there"))
""
}, "\r\n"), handler:read "*a")
handler:close()
end) end)
end) end)
describe("help messages", function() describe("help messages", function()
it("generates correct help message", function() it("generates correct help message", function()
local handler = io.popen("./spec/script --help 2>&1", "r") assert.equal([[
assert.equal(table.concat({ Usage: ./spec/script [-v] [-h] <input> [<command>] ...
"Usage: ./spec/script [-v] [-h] <input> [<command>] ...",
"", A testing program.
"A testing program. ",
"", Arguments:
"Arguments: ", input
" input",
"", Options:
"Options: ", -v, --verbose Sets verbosity level.
" -v, --verbose Sets verbosity level. ", -h, --help Show this help message and exit.
" -h, --help Show this help message and exit. ",
"", Commands:
"Commands: ", install Install a rock.
" install Install a rock. ", ]], get_output("--help"))
""
}, "\r\n"), handler:read "*a")
handler:close()
end) end)
it("generates correct help message for command", function() it("generates correct help message for command", function()
local handler = io.popen("./spec/script foo install --help 2>&1", "r") assert.equal([[
assert.equal(table.concat({ Usage: ./spec/script install [-f <from>] [-h] <rock> [<version>]
"Usage: ./spec/script install [-f <from>] [-h] <rock> [<version>]",
"", Install a rock.
"Install a rock. ",
"", Arguments:
"Arguments: ", rock Name of the rock.
" rock Name of the rock. ", version Version of the rock.
" version Version of the rock. ",
"", Options:
"Options: ", -f <from>, --from <from>
" -f <from>, --from <from>", Fetch the rock from this server.
" Fetch the rock from this server. ", -h, --help Show this help message and exit.
" -h, --help Show this help message and exit. ", ]], get_output("foo install --help"))
"",
}, "\r\n"), handler:read "*a")
handler:close()
end) end)
end) end)

View File

@@ -8,7 +8,7 @@ describe("tests related to tips", function()
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,
"unknown option '--quiett'\r\nDid you mean '--quiet'?") "unknown option '--quiett'\nDid you mean '--quiet'?")
end) end)
it("for commands", function() it("for commands", function()
@@ -16,7 +16,7 @@ describe("tests related to tips", function()
parser:command "install" parser:command "install"
assert.has_error(function() parser:parse{"installq"} end, assert.has_error(function() parser:parse{"installq"} end,
"unknown command 'installq'\r\nDid you mean 'install'?") "unknown command 'installq'\nDid you mean 'install'?")
end) end)
end) end)
@@ -26,7 +26,7 @@ describe("tests related to tips", function()
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,
"unknown option '--quet'\r\nDid you mean '--quiet'?") "unknown option '--quet'\nDid you mean '--quiet'?")
end) end)
it("for commands", function() it("for commands", function()
@@ -34,7 +34,7 @@ describe("tests related to tips", function()
parser:command "install" parser:command "install"
assert.has_error(function() parser:parse{"nstall"} end, assert.has_error(function() parser:parse{"nstall"} end,
"unknown command 'nstall'\r\nDid you mean 'install'?") "unknown command 'nstall'\nDid you mean 'install'?")
end) end)
end) end)
@@ -44,7 +44,7 @@ describe("tests related to tips", function()
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,
"unknown option '--qriet'\r\nDid you mean '--quiet'?") "unknown option '--qriet'\nDid you mean '--quiet'?")
end) end)
it("for commands", function() it("for commands", function()
@@ -52,7 +52,7 @@ describe("tests related to tips", function()
parser:command "install" parser:command "install"
assert.has_error(function() parser:parse{"inntall"} end, assert.has_error(function() parser:parse{"inntall"} end,
"unknown command 'inntall'\r\nDid you mean 'install'?") "unknown command 'inntall'\nDid you mean 'install'?")
end) end)
end) end)
@@ -62,7 +62,7 @@ describe("tests related to tips", function()
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,
"unknown option '--queit'\r\nDid you mean '--quiet'?") "unknown option '--queit'\nDid you mean '--quiet'?")
end) end)
it("for commands", function() it("for commands", function()
@@ -70,7 +70,7 @@ describe("tests related to tips", function()
parser:command "install" parser:command "install"
assert.has_error(function() parser:parse{"isntall"} end, assert.has_error(function() parser:parse{"isntall"} end,
"unknown command 'isntall'\r\nDid you mean 'install'?") "unknown command 'isntall'\nDid you mean 'install'?")
end) end)
end) end)
@@ -81,7 +81,7 @@ describe("tests related to tips", function()
parser:option "--quick" parser:option "--quick"
assert.has_error(function() parser:parse{"--quiec=true"} end, assert.has_error(function() parser:parse{"--quiec=true"} end,
"unknown option '--quiec'\r\nDid you mean one of these: '--quick' '--quiet'?") "unknown option '--quiec'\nDid you mean one of these: '--quick' '--quiet'?")
end) end)
it("for commands", function() it("for commands", function()
@@ -90,7 +90,7 @@ describe("tests related to tips", function()
parser:command "instant" parser:command "instant"
assert.has_error(function() parser:parse{"instanl"} end, assert.has_error(function() parser:parse{"instanl"} end,
"unknown command 'instanl'\r\nDid you mean one of these: 'install' 'instant'?") "unknown command 'instanl'\nDid you mean one of these: 'install' 'instant'?")
end) end)
end) end)
end) end)

View File

@@ -19,10 +19,9 @@ describe("tests related to usage message generation", function()
parser:argument "others" parser:argument "others"
:args "*" :args "*"
assert.equal(table.concat({ assert.equal([[
"Usage: foo <first> <second-and-third> <second-and-third>", Usage: foo <first> <second-and-third> <second-and-third>
" [<maybe-fourth>] [<others>] ..." [<maybe-fourth>] [<others>] ...]], parser:get_usage()
}, "\r\n"), parser:get_usage()
) )
end) end)
@@ -189,10 +188,9 @@ describe("tests related to usage message generation", function()
) )
parser:option "--yet-another-option" parser:option "--yet-another-option"
assert.equal(table.concat({ assert.equal([=[
"Usage: foo ([-q] | [-v] | [-i]) ([-l] | [-f <from>])", Usage: foo ([-q] | [-v] | [-i]) ([-l] | [-f <from>])
" [--yet-another-option <yet_another_option>]" [--yet-another-option <yet_another_option>]]=], parser:get_usage()
}, "\r\n"), parser:get_usage()
) )
end) end)
end) end)

View File

@@ -169,7 +169,7 @@ do
local help = self:flag() local help = self:flag()
:description "Show this help message and exit. " :description "Show this help message and exit. "
:action(function() :action(function()
io.stdout:write(self:get_help() .. "\r\n") io.stdout:write(self:get_help() .. "\n")
os.exit(0) os.exit(0)
end)(param) end)(param)
@@ -548,7 +548,7 @@ function Parser:get_usage()
add("...") add("...")
end end
return table.concat(lines, "\r\n") return table.concat(lines, "\n")
end end
local margin_len = 3 local margin_len = 3
@@ -561,18 +561,12 @@ local function make_two_columns(s1, s2)
return margin .. s1 return margin .. s1
end end
s2 = s2:gsub("[\r\n][\r\n]?", function(sub) s2 = s2:gsub("\n", "\n" .. margin2)
if #sub == 1 or sub == "\r\n" then
return "\r\n" .. margin2
else
return "\r\n\r\n" .. margin2
end
end)
if #s1 < (margin_len2-margin_len) then if #s1 < (margin_len2-margin_len) then
return margin .. s1 .. (" "):rep(margin_len2-margin_len-#s1) .. s2 return margin .. s1 .. (" "):rep(margin_len2-margin_len-#s1) .. s2
else else
return margin .. s1 .. "\r\n" .. margin2 .. s2 return margin .. s1 .. "\n" .. margin2 .. s2
end end
end end
@@ -597,7 +591,7 @@ function Parser:get_help()
table.insert(buf, make_two_columns(element:_get_label(), element:_get_description())) table.insert(buf, make_two_columns(element:_get_label(), element:_get_description()))
end end
table.insert(blocks, table.concat(buf, "\r\n")) table.insert(blocks, table.concat(buf, "\n"))
end end
end end
@@ -605,7 +599,7 @@ function Parser:get_help()
table.insert(blocks, self._epilog) table.insert(blocks, self._epilog)
end end
return table.concat(blocks, "\r\n\r\n") return table.concat(blocks, "\n\n")
end end
local function get_tip(context, wrong_name) local function get_tip(context, wrong_name)
@@ -647,9 +641,9 @@ local function get_tip(context, wrong_name)
end end
table.sort(possible_names_arr) table.sort(possible_names_arr)
return "\r\nDid you mean one of these: " .. table.concat(possible_names_arr, " ") .. "?" return "\nDid you mean one of these: " .. table.concat(possible_names_arr, " ") .. "?"
else else
return "\r\nDid you mean '" .. first .. "'?" return "\nDid you mean '" .. first .. "'?"
end end
else else
return "" return ""
@@ -1002,7 +996,7 @@ function Parser:_parse(args, errhandler)
end end
function Parser:error(msg) function Parser:error(msg)
io.stderr:write(("%s\r\n\r\nError: %s\r\n"):format(self:get_usage(), msg)) io.stderr:write(("%s\n\nError: %s\n"):format(self:get_usage(), msg))
os.exit(1) os.exit(1)
end end