From 9ae8df55ccdb83a678cab3ccc02bb0e24dc02860 Mon Sep 17 00:00:00 2001 From: mpeterv Date: Sun, 26 Jan 2014 17:38:22 +0400 Subject: [PATCH] added tests related to help, improved help generation --- spec/help_spec.lua | 104 +++++++++++++++++++++++++++++++++++++++++++++ src/argparse.lua | 10 ++--- 2 files changed, 109 insertions(+), 5 deletions(-) create mode 100644 spec/help_spec.lua diff --git a/spec/help_spec.lua b/spec/help_spec.lua new file mode 100644 index 0000000..4ba8e60 --- /dev/null +++ b/spec/help_spec.lua @@ -0,0 +1,104 @@ +local argparse = require "argparse" + +describe("tests related to help message generation", function() + it("creates correct help message for empty parser", function() + local parser = argparse.parser "foo" + assert.equal(table.concat({ + "Usage: foo [-h]", + "", + "Options: ", + " -h, --help Show this help message and exit. " + }, "\r\n"), parser:prepare():get_help()) + end) + + it("creates correct help message for arguments", function() + local parser = argparse.parser "foo" + parser:argument "first" + parser:argument "second-and-third" + :args "2" + parser:argument "maybe-fourth" + :args "?" + parser:argument "others" + :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:prepare():get_help()) + end) + + it("creates correct help message for options", function() + local parser = argparse.parser "foo" + parser:flag "-q" "--quiet" + parser:option "--from" + :count "1" + :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:prepare():get_help()) + end) + + it("creates correct help message for commands", function() + local parser = argparse.parser "foo" + parser:flag "-q" "--quiet" + local run = parser:command "run" + :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:prepare():get_help()) + end) + + it("creates correct help message for subcommands", function() + local parser = argparse.parser "foo" + parser:flag "-q" "--quiet" + local run = parser:command "run" + run:option "--where" + + parser:prepare() + + assert.equal(table.concat({ + "Usage: foo run [--where ] [-h]", + "", + "Options: ", + " --where ", + " -h, --help Show this help message and exit. ", + }, "\r\n"), run:prepare():get_help()) + end) + + it("uses message provided by user", function() + local parser = argparse.parser "foo" + :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:prepare():get_help() + ) + end) +end) diff --git a/src/argparse.lua b/src/argparse.lua index 87d31f1..60dd3e0 100644 --- a/src/argparse.lua +++ b/src/argparse.lua @@ -341,14 +341,14 @@ function Parser:get_usage() end local function make_two_columns(s1, s2) + if s2 == "" then + return " " .. s1 + end + if #s1 < 22 then return " " .. s1 .. (" "):rep(22 - #s1) .. s2 else - if s2 == "" then - return " " .. s1 - else - return " " .. s1 .. "\r\n" .. (" "):rep(25) .. s2 - end + return " " .. s1 .. "\r\n" .. (" "):rep(25) .. s2 end end