src: update tools to the new toolcontext helpers on option processing
authorJean Privat <jean@pryen.org>
Fri, 21 Mar 2014 01:56:40 +0000 (21:56 -0400)
committerJean Privat <jean@pryen.org>
Fri, 21 Mar 2014 13:22:43 +0000 (09:22 -0400)
Specify a tooldescription
Get rid of manual management of most arguments error

Signed-off-by: Jean Privat <jean@pryen.org>

src/debugger_commons.nit
src/nitdbg_client.nit
src/nitdoc.nit
src/nitg.nit
src/nitlight.nit
src/nitls.nit
src/nitmetrics.nit
src/nitunit.nit
src/nitx.nit
src/test_markdown.nit
src/test_phase.nit

index e2e9dc3..ad03be6 100644 (file)
@@ -36,6 +36,7 @@ class InterpretCommons
        do
                # Create a tool context to handle options and paths
                toolcontext = new ToolContext
+               toolcontext.tooldescription = "Usage: nit [OPTION]... <file.nit>...\nInterprets and debbugs Nit programs."
                # Add an option "-o" to enable compatibilit with the tests.sh script
                var opt = new OptionString("compatibility (does noting)", "-o")
                toolcontext.option_context.add_option(opt)
@@ -51,10 +52,6 @@ class InterpretCommons
                modelbuilder = new ModelBuilder(model, toolcontext.as(not null))
                
                arguments = toolcontext.option_context.rest
-               if arguments.is_empty or toolcontext.opt_help.value then
-                       toolcontext.option_context.usage
-                       exit(0)
-               end
                var progname = arguments.first
                
                # Here we load an process all modules passed on the command line
index 3257c72..489e1d7 100644 (file)
@@ -103,15 +103,12 @@ end
 
 # Create a tool context to handle options and paths
 var toolcontext = new ToolContext
+toolcontext.tooldescription = "Usage: nitdbg_client [OPTION]...\nConnects to a nitdbg_server and controls it."
+toolcontext.accept_no_arguments = true
 toolcontext.process_options
 
 var debug: DebugClient
 
-if toolcontext.opt_help.value then
-       toolcontext.option_context.usage
-       return
-end
-
 # If the port value is not an Int between 0 and 65535 (Mandatory according to the norm)
 # Print the usage
 if toolcontext.opt_debug_port.value < 0 or toolcontext.opt_debug_port.value > 65535 then
index 344aac6..3ec48a9 100644 (file)
@@ -73,14 +73,10 @@ class NitdocContext
                toolcontext.option_context.add_option(opt_github_gitdir)
                toolcontext.option_context.add_option(opt_piwik_tracker)
                toolcontext.option_context.add_option(opt_piwik_site_id)
+               toolcontext.tooldescription = "Usage: nitdoc [OPTION]... <file.nit>...\nGenerates HTML pages of API documentation from Nit source files."
                toolcontext.process_options
                self.arguments = toolcontext.option_context.rest
 
-               if arguments.length < 1 then
-                       print "usage: nitdoc [options] file..."
-                       toolcontext.option_context.usage
-                       exit(0)
-               end
                self.process_options
 
                model = new Model
index e4e965f..9a6e69d 100644 (file)
@@ -36,6 +36,8 @@ toolcontext.option_context.add_option(opt_global)
 var opt_mixins = new OptionArray("Additionals module to min-in", "-m")
 toolcontext.option_context.add_option(opt_mixins)
 
+toolcontext.tooldescription = "Usage: nitg [OPTION]... file.nit\nCompiles Nit programs."
+
 # We do not add other options, so process them now!
 toolcontext.process_options
 
@@ -45,14 +47,10 @@ var model = new Model
 var modelbuilder = new ModelBuilder(model, toolcontext)
 
 var arguments = toolcontext.option_context.rest
-if arguments.is_empty then
-       toolcontext.option_context.usage
-       return
-end
 if arguments.length > 1 then
        print "Too much arguments: {arguments.join(" ")}"
-       toolcontext.option_context.usage
-       return
+       print toolcontext.tooldescription
+       exit 1
 end
 var progname = arguments.first
 
index 64bea58..7ead6b9 100644 (file)
@@ -26,6 +26,7 @@ var opt_dir = new OptionString("Output html files in a specific directory (requi
 var opt_full = new OptionBool("Process also imported modules", "--full")
 var opt_ast = new OptionBool("Generate specific HTML elements for each Node of the AST", "--ast")
 toolcontext.option_context.add_option(opt_fragment, opt_first_line, opt_last_line, opt_dir, opt_full)
+toolcontext.tooldescription = "Usage: nitlight [OPTION]... <file.nit>...\nGenerates HTML of highlited code from Nit source files."
 
 var model = new Model
 var modelbuilder = new ModelBuilder(model, toolcontext)
@@ -33,12 +34,6 @@ var modelbuilder = new ModelBuilder(model, toolcontext)
 toolcontext.process_options
 var args = toolcontext.option_context.rest
 
-if args.is_empty then
-       print "usage: nitlight [options] files..."
-       toolcontext.option_context.usage
-       return
-end
-
 var mmodules = modelbuilder.parse(args)
 modelbuilder.run_phases
 
index 6e266d9..c058219 100644 (file)
@@ -32,15 +32,14 @@ var opt_project = new OptionBool("List projects paths (default)", "-p", "--proje
 var opt_depends = new OptionBool("List dependencies of given modules", "-M", "--depends")
 
 tc.option_context.add_option(opt_keep, opt_recursive, opt_tree, opt_source, opt_project, opt_depends)
-
+tc.tooldescription = "Usage: nitls [OPTION]... <file.nit|directory>...\nLists the projects and/or paths of Nit sources files."
 tc.process_options
 
 var sum = opt_tree.value.to_i + opt_source.value.to_i + opt_project.value.to_i + opt_depends.value.to_i
-if sum > 1 or tc.option_context.rest.is_empty or tc.opt_help.value then
-       print "Usage: nitls [OPTION].. [FILES]..."
-       print "List Nit source files"
-       tc.option_context.usage
-       exit 0
+if sum > 1 then
+       print "Error: options --tree, --source, and --project are exclusives."
+       print tc.tooldescription
+       exit 1
 end
 
 if opt_depends.value then
index b7cdeb7..01b3654 100644 (file)
@@ -23,15 +23,13 @@ import metrics
 
 # Create a tool context to handle options and paths
 var toolcontext = new ToolContext
+toolcontext.tooldescription = "Usage: nitmetrics [OPTION]... <file.nit>...\mComputes various metrics on Nit programs."
+
 # We do not add other options, so process them now!
 toolcontext.process_options
 
 # Get arguments
 var arguments = toolcontext.option_context.rest
-if arguments.is_empty or toolcontext.opt_help.value then
-       toolcontext.option_context.usage
-       return
-end
 
 # We need a model to collect stufs
 var model = new Model
index 5a96471..46c8254 100644 (file)
@@ -221,15 +221,10 @@ end
 var toolcontext = new ToolContext
 
 toolcontext.option_context.add_option(toolcontext.opt_full, toolcontext.opt_output, toolcontext.opt_dir)
-
+toolcontext.tooldescription = "Usage: nitunit [OPTION]... <file.nit>...\nExecutes the unit tests from Nit source files."
 
 toolcontext.process_options
 var args = toolcontext.option_context.rest
-if args.is_empty or toolcontext.opt_help.value then
-       print "usage: nitunit [options] file.nit..."
-       toolcontext.option_context.usage
-       return
-end
 
 var model = new Model
 var modelbuilder = new ModelBuilder(model, toolcontext)
index a57af8b..7225571 100644 (file)
@@ -45,12 +45,10 @@ class NitIndex
        init(toolcontext: ToolContext) do
                # We need a model to collect stufs
                self.toolcontext = toolcontext
-               self.toolcontext.option_context.options.clear
                self.arguments = toolcontext.option_context.rest
 
-               if arguments.is_empty or arguments.length > 2 then
-                       print "usage: ni path/to/module.nit [expression]"
-                       toolcontext.option_context.usage
+               if arguments.length > 2 then
+                       print toolcontext.tooldescription
                        exit(1)
                end
 
@@ -869,6 +867,7 @@ end
 
 # Create a tool context to handle options and paths
 var toolcontext = new ToolContext
+toolcontext.tooldescription = "Usage: nitx [OPTION]... <file.nit> [query]\nDisplays specific pieces of API information from Nit source files."
 toolcontext.process_options
 
 # Here we launch the nit index
index b89d802..24b48f7 100644 (file)
@@ -56,14 +56,10 @@ var toolcontext = new ToolContext
 
 var opt_full = new OptionBool("Process also imported modules", "--full")
 toolcontext.option_context.add_option(opt_full)
+toolcontext.tooldescription = "Usage: test_markdown [OPTION]... <file.nit>...\nGenerates HTML of comments of documentation from Nit source files."
 
 toolcontext.process_options
 var args = toolcontext.option_context.rest
-if args.is_empty then
-       print "usage: test_markdown [options] file.nit..."
-       toolcontext.option_context.usage
-       return
-end
 
 var model = new Model
 var modelbuilder = new ModelBuilder(model, toolcontext)
index 1b90355..8594a5b 100644 (file)
@@ -22,15 +22,13 @@ import modelbuilder
 
 # Create a tool context to handle options and paths
 var toolcontext = new ToolContext
+toolcontext.tooldescription = "Usage: [OPTION]... <file.nit>..."
+
 # We do not add other options, so process them now!
 toolcontext.process_options
 
 # Get arguments
 var arguments = toolcontext.option_context.rest
-if arguments.is_empty or toolcontext.opt_help.value then
-       toolcontext.option_context.usage
-       return
-end
 
 # We need a model to collect stufs
 var model = new Model