[tarantool-patches] [PATCH 2/2] app: fix error messages for not specified parameters in argparse

Vladislav Shpilevoy v.shpilevoy at tarantool.org
Thu Sep 19 02:00:31 MSK 2019


Argparse module stores unspecified parameter values as boolean
true. It led to a problem, that a command line '--value' with
'value' defined as a number or a string, showed a strange error
message:

    Expected number/string, got "true"

Even though a user didn't pass any value. Now it shows 'nothing'
instead of '"true"'. That is clearer.

Follow up #4076
---
 src/lua/argparse.lua       | 27 ++++++++++++++-------------
 test/app/argparse.result   | 30 +++++++++++++++++++++++++++---
 test/app/argparse.test.lua | 12 ++++++++++++
 3 files changed, 53 insertions(+), 16 deletions(-)

diff --git a/src/lua/argparse.lua b/src/lua/argparse.lua
index 1471d5846..8dae388b1 100644
--- a/src/lua/argparse.lua
+++ b/src/lua/argparse.lua
@@ -20,14 +20,21 @@ local function result_set_add(t_out, key, val)
     end
 end
 
+local function err_bad_parameter_value(name, got, expected)
+    if type(got) ~= 'string' then
+        got = 'nothing'
+    else
+        got = string.format('"%s"', got)
+    end
+    error(string.format('Bad value for parameter "%s". Expected %s, got %s',
+                        name, expected, got))
+end
+
 local function convert_parameter_simple(name, convert_from, convert_to)
     if convert_to == 'number' then
         local converted = tonumber(convert_from)
         if converted == nil then
-            error(
-                ('Bad value for parameter %s. expected type %s, got "%s"')
-                :format(name, convert_to, convert_from)
-            )
+            return err_bad_parameter_value(name, convert_from, convert_to)
         end
         return converted
     elseif convert_to == 'boolean' then
@@ -41,20 +48,14 @@ local function convert_parameter_simple(name, convert_from, convert_to)
         if convert_from == '1' or convert_from == 'true' then
             return true
         end
-        error(
-            ('Bad input for parameter "%s". Expected boolean, got "%s"')
-            :format(name, convert_from)
-        )
+        return err_bad_parameter_value(name, convert_from, convert_to)
     elseif convert_to == 'string' then
         if type(convert_from) ~= 'string' then
-            error(
-                ('Bad input for parameter "%s". Expected string, got "%s"')
-                :format(name, convert_from)
-            )
+            return err_bad_parameter_value(name, convert_from, convert_to)
         end
     else
         error(
-            ('Bad convertion format "%s" provided for %s')
+            ('Bad conversion format "%s" provided for %s')
             :format(convert_to, name)
         )
     end
diff --git a/test/app/argparse.result b/test/app/argparse.result
index d5be7fe2d..ef104fce3 100644
--- a/test/app/argparse.result
+++ b/test/app/argparse.result
@@ -137,12 +137,12 @@ argparse({'--verh=42'}, {'verh'})
 ...
 argparse({'--verh=42'}, {{'verh', 'boolean'}})
 ---
-- error: 'builtin/internal.argparse.lua:<line>"]: Bad input for parameter "verh". Expected
+- error: 'builtin/internal.argparse.lua:<line>"]: Bad value for parameter "verh". Expected
     boolean, got "42"'
 ...
 argparse({'--verh=42'}, {{'verh', 'boolean+'}})
 ---
-- error: 'builtin/internal.argparse.lua:<line>"]: Bad input for parameter "verh". Expected
+- error: 'builtin/internal.argparse.lua:<line>"]: Bad value for parameter "verh". Expected
     boolean, got "42"'
 ...
 argparse({'--verh=42'}, {'niz'})
@@ -201,9 +201,33 @@ args = {'--flag1', 'abc'}
 ...
 argparse(args, params)
 ---
-- error: 'builtin/internal.argparse.lua:<line>"]: Bad input for parameter "flag1". Expected
+- error: 'builtin/internal.argparse.lua:<line>"]: Bad value for parameter "flag1". Expected
     boolean, got "abc"'
 ...
+--
+-- When parameter value was omitted, it was replaced internally
+-- with boolean true, and sometimes was showed in error messages.
+-- Now it is 'nothing'.
+--
+params = {}
+---
+...
+params[1] = {'value', 'number'}
+---
+...
+argparse({'--value'}, params)
+---
+- error: 'builtin/internal.argparse.lua:<line>"]: Bad value for parameter "value". Expected
+    number, got nothing'
+...
+params[1][2] = 'string'
+---
+...
+argparse({'--value'}, params)
+---
+- error: 'builtin/internal.argparse.lua:<line>"]: Bad value for parameter "value". Expected
+    string, got nothing'
+...
 test_run:cmd("clear filter")
 ---
 - true
diff --git a/test/app/argparse.test.lua b/test/app/argparse.test.lua
index 9315451d3..f09a337ef 100644
--- a/test/app/argparse.test.lua
+++ b/test/app/argparse.test.lua
@@ -47,4 +47,16 @@ argparse(args, params)
 args = {'--flag1', 'abc'}
 argparse(args, params)
 
+--
+-- When parameter value was omitted, it was replaced internally
+-- with boolean true, and sometimes was showed in error messages.
+-- Now it is 'nothing'.
+--
+params = {}
+params[1] = {'value', 'number'}
+argparse({'--value'}, params)
+
+params[1][2] = 'string'
+argparse({'--value'}, params)
+
 test_run:cmd("clear filter")
-- 
2.20.1 (Apple Git-117)





More information about the Tarantool-patches mailing list