opts: error when lack of a mandatory parameter
authorAlexandre Pennetier <alexandre.pennetier@me.com>
Mon, 11 Jun 2012 20:43:14 +0000 (16:43 -0400)
committerJean Privat <jean@pryen.org>
Fri, 21 Sep 2012 19:11:06 +0000 (15:11 -0400)
Signed-off-by: Alexandre Pennetier <alexandre.pennetier@me.com>

lib/opts.nit

index db931d6..44571c8 100644 (file)
@@ -123,23 +123,34 @@ class OptionCount
        end
 end
 
-# Option with one mandatory parameter
+# Option with one parameter (mandatory by default)
 abstract class OptionParameter
        super Option
        protected fun convert(str: String): VALUE is abstract
 
+       # Is the parameter mandatory?
+       readable writable var _parameter_mandatory: Bool
+
        redef fun read_param(it)
        do
                super
-               if it.is_ok then
+               if it.is_ok and it.item.first != '-' then
                        value = convert(it.item)
                        it.next
                else
-                       # TODO: What to do?
+                       if _parameter_mandatory then
+                               # FIXME exit(1) is not a good way of handling the error
+                               stderr.write("Error: parameter expected for option {names.first}\n")
+                               exit(1)
+                       end
                end
        end
 
-       init init_opt(h, d, n) do super
+       init init_opt(h, d, n)
+       do
+               super
+               _parameter_mandatory = true
+       end
 end
 
 class OptionString