toolcontext: add the flag `accept_no_arguments`
authorJean Privat <jean@pryen.org>
Fri, 21 Mar 2014 00:47:29 +0000 (20:47 -0400)
committerJean Privat <jean@pryen.org>
Fri, 21 Mar 2014 13:22:42 +0000 (09:22 -0400)
Signed-off-by: Jean Privat <jean@pryen.org>

src/toolcontext.nit

index 6cc8889..4e26321 100644 (file)
@@ -180,6 +180,13 @@ class ToolContext
        # eg. `"Usage: tool [OPTION]... [FILE]...\nDo some things."`
        var tooldescription: String writable = "Usage: [OPTION]... [ARG]..."
 
+       # Does `process_options` should accept an empty sequence of arguments.
+       # ie. nothing except options.
+       # Is `false` by default.
+       #
+       # If required, if should be set by the client before calling `process_options`
+       var accept_no_arguments writable = false
+
        # print the full usage of the tool.
        # Is called by `process_option` on `--help`.
        # It also could be called by the client.
@@ -215,6 +222,12 @@ class ToolContext
                        exit 1
                end
 
+               if option_context.rest.is_empty and not accept_no_arguments then
+                       print tooldescription
+                       print "Use --help for help"
+                       exit 1
+               end
+
                # Set verbose level
                verbose_level = opt_verbose.value