diff --git a/spec/help_spec.lua b/spec/help_spec.lua index 7100a8b..5de40cd 100644 --- a/spec/help_spec.lua +++ b/spec/help_spec.lua @@ -4,33 +4,30 @@ getmetatable(Parser()).error = function(_, msg) error(msg) end describe("tests related to help message generation", function() it("creates correct help message for empty parser", function() local parser = Parser "foo" - assert.equal(table.concat({ - "Usage: foo [-h]", - "", - "Options: ", - " -h, --help Show this help message and exit. " - }, "\r\n"), parser:get_help()) + assert.equal([[ +Usage: foo [-h] + +Options: + -h, --help Show this help message and exit. ]], parser:get_help()) end) it("does not create extra help options when :prepare is called several times", function() local parser = Parser "foo" - assert.equal(table.concat({ - "Usage: foo [-h]", - "", - "Options: ", - " -h, --help Show this help message and exit. " - }, "\r\n"), parser:get_help()) + assert.equal([[ +Usage: foo [-h] + +Options: + -h, --help Show this help message and exit. ]], parser:get_help()) end) - it("uses custom help option", function() + it("uses custom help option ", function() local parser = Parser "foo" :add_help "/?" - assert.equal(table.concat({ - "Usage: foo [/?]", - "", - "Options: ", - " /? Show this help message and exit. " - }, "\r\n"), parser:get_help()) + assert.equal([[ +Usage: foo [/?] + +Options: + /? Show this help message and exit. ]], parser:get_help()) end) it("uses description and epilog", function() @@ -38,16 +35,15 @@ describe("tests related to help message generation", function() :description "A description. " :epilog "An epilog. " - assert.equal(table.concat({ - "Usage: foo [-h]", - "", - "A description. ", - "", - "Options: ", - " -h, --help Show this help message and exit. ", - "", - "An epilog. " - }, "\r\n"), parser:get_help()) + assert.equal([[ +Usage: foo [-h] + +A description. + +Options: + -h, --help Show this help message and exit. + +An epilog. ]], parser:get_help()) end) it("creates correct help message for arguments", function() @@ -61,19 +57,18 @@ describe("tests related to help message generation", function() :description "Optional. " :args "*" - assert.equal(table.concat({ - "Usage: foo [-h] ", - " [] [] ...", - "", - "Arguments: ", - " first", - " second-and-third", - " maybe-fourth", - " others Optional. ", - "", - "Options: ", - " -h, --help Show this help message and exit. " - }, "\r\n"), parser:get_help()) + assert.equal([[ +Usage: foo [-h] + [] [] ... + +Arguments: + first + second-and-third + maybe-fourth + others Optional. + +Options: + -h, --help Show this help message and exit. ]], parser:get_help()) end) it("creates correct help message for options", function() @@ -84,15 +79,14 @@ describe("tests related to help message generation", function() :target "server" parser:option "--config" - assert.equal(table.concat({ - "Usage: foo [-q] --from [--config ] [-h]", - "", - "Options: ", - " -q, --quiet", - " --from ", - " --config ", - " -h, --help Show this help message and exit. " - }, "\r\n"), parser:get_help()) + assert.equal([[ +Usage: foo [-q] --from [--config ] [-h] + +Options: + -q, --quiet + --from + --config + -h, --help Show this help message and exit. ]], parser:get_help()) end) it("adds margin for multiline descriptions", function() @@ -105,15 +99,14 @@ Sets verbosity level. -v: Report all warnings. -vv: Report all debugging information. ]] - assert.equal(table.concat({ - "Usage: foo [-v] [-h]", - "", - "Options: ", - " -v Sets verbosity level. ", - " -v: Report all warnings. ", - " -vv: Report all debugging information. ", - " -h, --help Show this help message and exit. " - }, "\r\n"), parser:get_help()) + assert.equal([[ +Usage: foo [-v] [-h] + +Options: + -v Sets verbosity level. + -v: Report all warnings. + -vv: Report all debugging information. + -h, --help Show this help message and exit. ]], parser:get_help()) end) it("shows default values", function() @@ -124,14 +117,13 @@ Sets verbosity level. :default "8080" :description "Port." - assert.equal(table.concat({ - "Usage: foo [-o ] [-p

] [-h]", - "", - "Options: ", - " -o default: a.out", - " -p

Port. (default: 8080)", - " -h, --help Show this help message and exit. " - }, "\r\n"), parser:get_help()) + assert.equal([[ +Usage: foo [-o ] [-p

] [-h] + +Options: + -o default: a.out + -p

Port. (default: 8080) + -h, --help Show this help message and exit. ]], parser:get_help()) end) it("does not show default value when show_default == false", function() @@ -144,14 +136,13 @@ Sets verbosity level. :show_default(false) :description "Port. " - assert.equal(table.concat({ - "Usage: foo [-o ] [-p

] [-h]", - "", - "Options: ", - " -o ", - " -p

Port. ", - " -h, --help Show this help message and exit. " - }, "\r\n"), parser:get_help()) + assert.equal([[ +Usage: foo [-o ] [-p

] [-h] + +Options: + -o + -p

Port. + -h, --help Show this help message and exit. ]], parser:get_help()) end) it("creates correct help message for commands", function() @@ -161,16 +152,15 @@ Sets verbosity level. :description "Run! " run:option "--where" - assert.equal(table.concat({ - "Usage: foo [-q] [-h] ...", - "", - "Options: ", - " -q, --quiet", - " -h, --help Show this help message and exit. ", - "", - "Commands: ", - " run Run! " - }, "\r\n"), parser:get_help()) + assert.equal([[ +Usage: foo [-q] [-h] ... + +Options: + -q, --quiet + -h, --help Show this help message and exit. + +Commands: + run Run! ]], parser:get_help()) end) it("creates correct help message for subcommands", function() @@ -179,13 +169,12 @@ Sets verbosity level. local run = parser:command "run" run:option "--where" - assert.equal(table.concat({ - "Usage: foo run [--where ] [-h]", - "", - "Options: ", - " --where ", - " -h, --help Show this help message and exit. ", - }, "\r\n"), run:get_help()) + assert.equal([[ +Usage: foo run [--where ] [-h] + +Options: + --where + -h, --help Show this help message and exit. ]], run:get_help()) end) it("uses message provided by user", function() @@ -193,9 +182,7 @@ Sets verbosity level. :help "I don't like your format of help messages" parser:flag "-q" "--quiet" - assert.equal( - [=[I don't like your format of help messages]=], - parser:get_help() - ) + assert.equal([[ +I don't like your format of help messages]], parser:get_help()) end) end) diff --git a/spec/integrity_spec.lua b/spec/integrity_spec.lua index af4112f..6928735 100644 --- a/spec/integrity_spec.lua +++ b/spec/integrity_spec.lua @@ -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("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: ./spec/script [-v] [-h] [] ...", - "", - "Error: too few arguments", - "" - }, "\r\n"), handler:read "*a") - handler:close() + assert.equal([[ +Usage: ./spec/script [-v] [-h] [] ... + +Error: too few arguments +]], get_output("")) 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: ./spec/script [-v] [-h] [] ...", - "", - "Error: unknown command 'bar'", - "" - }, "\r\n"), handler:read "*a") - handler:close() + assert.equal([[ +Usage: ./spec/script [-v] [-h] [] ... + +Error: unknown command 'bar' +]], get_output("foo bar")) 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: ./spec/script [-v] [-h] [] ...", - "", - "Error: option '--verbose' does not take arguments", - "" - }, "\r\n"), handler:read "*a") - handler:close() + assert.equal([[ +Usage: ./spec/script [-v] [-h] [] ... + +Error: option '--verbose' does not take arguments +]], get_output("--verbose=true")) 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: ./spec/script [-v] [-h] [] ...", - "", - "Error: unknown option '-q'", - "Did you mean one of these: '-h' '-v'?", - "" - }, "\r\n"), handler:read "*a") - handler:close() + assert.equal([[ +Usage: ./spec/script [-v] [-h] [] ... + +Error: unknown option '-q' +Did you mean one of these: '-h' '-v'? +]], get_output("-vq")) 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: ./spec/script [-v] [-h] [] ...", - "", - "Error: unknown command 'nstall'", - "Did you mean 'install'?", - "" - }, "\r\n"), handler:read "*a") - handler:close() + assert.equal([[ +Usage: ./spec/script [-v] [-h] [] ... + +Error: unknown command 'nstall' +Did you mean 'install'? +]], get_output("foo nstall")) 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: ./spec/script install [-f ] [-h] []", - "", - "Error: too few arguments", - "" - }, "\r\n"), handler:read "*a") - handler:close() + assert.equal([[ +Usage: ./spec/script install [-f ] [-h] [] + +Error: too few arguments +]], get_output("foo install")) 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: ./spec/script install [-f ] [-h] []", - "", - "Error: unknown option '--form'", - "Did you mean '--from'?", - "" - }, "\r\n"), handler:read "*a") - handler:close() + assert.equal([[ +Usage: ./spec/script install [-f ] [-h] [] + +Error: unknown option '--form' +Did you mean '--from'? +]], get_output("foo install bar --form=there")) 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: ./spec/script [-v] [-h] [] ...", - "", - "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() + assert.equal([[ +Usage: ./spec/script [-v] [-h] [] ... + +A testing program. + +Arguments: + input + +Options: + -v, --verbose Sets verbosity level. + -h, --help Show this help message and exit. + +Commands: + install Install a rock. +]], get_output("--help")) 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: ./spec/script install [-f ] [-h] []", - "", - "Install a rock. ", - "", - "Arguments: ", - " rock Name of the rock. ", - " version Version of the rock. ", - "", - "Options: ", - " -f , --from ", - " Fetch the rock from this server. ", - " -h, --help Show this help message and exit. ", - "", - }, "\r\n"), handler:read "*a") - handler:close() + assert.equal([[ +Usage: ./spec/script install [-f ] [-h] [] + +Install a rock. + +Arguments: + rock Name of the rock. + version Version of the rock. + +Options: + -f , --from + Fetch the rock from this server. + -h, --help Show this help message and exit. +]], get_output("foo install --help")) end) end) diff --git a/spec/tip_spec.lua b/spec/tip_spec.lua index cebdec3..c08ecdf 100644 --- a/spec/tip_spec.lua +++ b/spec/tip_spec.lua @@ -8,7 +8,7 @@ describe("tests related to tips", function() parser:option "-q" "--quiet" 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) it("for commands", function() @@ -16,7 +16,7 @@ describe("tests related to tips", function() parser:command "install" 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) @@ -26,7 +26,7 @@ describe("tests related to tips", function() parser:option "-q" "--quiet" 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) it("for commands", function() @@ -34,7 +34,7 @@ describe("tests related to tips", function() parser:command "install" 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) @@ -44,7 +44,7 @@ describe("tests related to tips", function() parser:option "-q" "--quiet" 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) it("for commands", function() @@ -52,7 +52,7 @@ describe("tests related to tips", function() parser:command "install" 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) @@ -62,7 +62,7 @@ describe("tests related to tips", function() parser:option "-q" "--quiet" 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) it("for commands", function() @@ -70,7 +70,7 @@ describe("tests related to tips", function() parser:command "install" 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) @@ -81,7 +81,7 @@ describe("tests related to tips", function() parser:option "--quick" 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) it("for commands", function() @@ -90,7 +90,7 @@ describe("tests related to tips", function() parser:command "instant" 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) diff --git a/spec/usage_spec.lua b/spec/usage_spec.lua index 60ebe62..076c5ec 100644 --- a/spec/usage_spec.lua +++ b/spec/usage_spec.lua @@ -19,10 +19,9 @@ describe("tests related to usage message generation", function() parser:argument "others" :args "*" - assert.equal(table.concat({ - "Usage: foo ", - " [] [] ..." - }, "\r\n"), parser:get_usage() + assert.equal([[ +Usage: foo + [] [] ...]], parser:get_usage() ) end) @@ -189,10 +188,9 @@ describe("tests related to usage message generation", function() ) parser:option "--yet-another-option" - assert.equal(table.concat({ - "Usage: foo ([-q] | [-v] | [-i]) ([-l] | [-f ])", - " [--yet-another-option ]" - }, "\r\n"), parser:get_usage() + assert.equal([=[ +Usage: foo ([-q] | [-v] | [-i]) ([-l] | [-f ]) + [--yet-another-option ]]=], parser:get_usage() ) end) end) diff --git a/src/argparse.lua b/src/argparse.lua index 1739a35..df31887 100644 --- a/src/argparse.lua +++ b/src/argparse.lua @@ -169,7 +169,7 @@ do local help = self:flag() :description "Show this help message and exit. " :action(function() - io.stdout:write(self:get_help() .. "\r\n") + io.stdout:write(self:get_help() .. "\n") os.exit(0) end)(param) @@ -548,7 +548,7 @@ function Parser:get_usage() add("...") end - return table.concat(lines, "\r\n") + return table.concat(lines, "\n") end local margin_len = 3 @@ -561,18 +561,12 @@ local function make_two_columns(s1, s2) return margin .. s1 end - s2 = s2:gsub("[\r\n][\r\n]?", function(sub) - if #sub == 1 or sub == "\r\n" then - return "\r\n" .. margin2 - else - return "\r\n\r\n" .. margin2 - end - end) + s2 = s2:gsub("\n", "\n" .. margin2) if #s1 < (margin_len2-margin_len) then return margin .. s1 .. (" "):rep(margin_len2-margin_len-#s1) .. s2 else - return margin .. s1 .. "\r\n" .. margin2 .. s2 + return margin .. s1 .. "\n" .. margin2 .. s2 end end @@ -597,7 +591,7 @@ function Parser:get_help() table.insert(buf, make_two_columns(element:_get_label(), element:_get_description())) end - table.insert(blocks, table.concat(buf, "\r\n")) + table.insert(blocks, table.concat(buf, "\n")) end end @@ -605,7 +599,7 @@ function Parser:get_help() table.insert(blocks, self._epilog) end - return table.concat(blocks, "\r\n\r\n") + return table.concat(blocks, "\n\n") end local function get_tip(context, wrong_name) @@ -647,9 +641,9 @@ local function get_tip(context, wrong_name) end 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 - return "\r\nDid you mean '" .. first .. "'?" + return "\nDid you mean '" .. first .. "'?" end else return "" @@ -1002,7 +996,7 @@ function Parser:_parse(args, errhandler) end 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) end