From aeacbeb5899c57394f2e707fbb88c5d5efbc1e50 Mon Sep 17 00:00:00 2001 From: mpeterv Date: Sun, 23 Feb 2014 16:58:27 +0400 Subject: [PATCH] Improved error message Report '1 times' as '1 time' --- spec/options_spec.lua | 2 +- src/argparse.lua | 12 ++++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/spec/options_spec.lua b/spec/options_spec.lua index 81ea0e3..2a624af 100644 --- a/spec/options_spec.lua +++ b/spec/options_spec.lua @@ -243,7 +243,7 @@ describe("tests related to options", function() count = 1, overwrite = false }) - assert.has_error(function() parser:parse{"-qq"} end, "option '-q' must be used at most 1 times") + assert.has_error(function() parser:parse{"-qq"} end, "option '-q' must be used at most 1 time") end) it("handles too few invocations correctly", function() diff --git a/src/argparse.lua b/src/argparse.lua index f6da4d7..58d3dca 100644 --- a/src/argparse.lua +++ b/src/argparse.lua @@ -493,6 +493,14 @@ local function get_tip(context, wrong_name) end end +local function plural(x) + if x == 1 then + return "" + end + + return "s" +end + function Parser:_parse(args, errhandler) args = args or arg self._name = self._name or args[0] @@ -546,7 +554,7 @@ function Parser:_parse(args, errhandler) if element._overwrite then overwrite = true else - error_("option '%s' must be used at most %d times", element._name, element._maxcount) + error_("option '%s' must be used at most %d time%s", element._name, element._maxcount, plural(element._maxcount)) end else invocations[element] = invocations[element]+1 @@ -772,7 +780,7 @@ function Parser:_parse(args, errhandler) close(option) end else - error_("option '%s' must be used at least %d times", option._name, option._mincount) + error_("option '%s' must be used at least %d time%s", option._name, option._mincount, plural(option._mincount)) end end end