From 4ae0a464477b8b76cdb6c718d44b7718401ec6bc Mon Sep 17 00:00:00 2001 From: =?utf8?q?Alexis=20Laferri=C3=A8re?= Date: Wed, 20 Apr 2016 07:34:29 -0400 Subject: [PATCH] lib/opts: report invalid integer passed to an OptionInt MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Alexis Laferrière --- lib/opts.nit | 8 +++++++- tests/sav/test_opts_args5.res | 2 ++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/opts.nit b/lib/opts.nit index da78780..30ea286 100644 --- a/lib/opts.nit +++ b/lib/opts.nit @@ -236,7 +236,13 @@ class OptionInt super(help, default, names) end - redef fun convert(str) do return str.to_i + redef fun convert(str) + do + if str.is_int then return str.to_i + + errors.add "Expected an integer for option {names.join(", ")}." + return 0 + end end # An option with a Float as parameter diff --git a/tests/sav/test_opts_args5.res b/tests/sav/test_opts_args5.res index 76da1d5..cd209be 100644 --- a/tests/sav/test_opts_args5.res +++ b/tests/sav/test_opts_args5.res @@ -2,6 +2,8 @@ Arguments: 2 -i one Rest: 0 +Errors: 1 +Expected an integer for option -i, --int. OptionBool: false OptionCount: 0 OptionString: -- 1.7.9.5