Commits

Doug Hellmann committed a9aa253

Add introspection to produce argument description for main app, not just options.

Comments (0)

Files changed (3)

CommandLineApp.py

                 options_text_parts.append(option.arg_name)
             option_texts.append( ''.join(options_text_parts) )
         return ', '.join(option_texts)
-    
+
+    def getArgumentsSyntaxString(self):
+        """Look at the arguments to main to see what the program accepts,
+        and build a syntax string explaining how to pass those arguments.
+        """
+        syntax_parts = []
+        argspec = inspect.getargspec(self.main)
+        args = argspec[0]
+        if len(args) > 1:
+            for arg in args[1:]:
+                syntax_parts.append(arg)
+        if argspec[1]:
+            syntax_parts.append(argspec[1])
+            syntax_parts.append('[' + argspec[1] + '...]')
+        syntax = ' '.join(syntax_parts)
+        return syntax
+            
     def getSimpleSyntaxHelpString(self):    
         """Return syntax statement.
         
         syntax of the command.
         """
         buffer = StringIO()
-
+        
+        
+        
         # Show the name of the command and basic syntax.
-        buffer.write('%s [<options>] %s\n\n' % (sys.argv[0],
-                                                self.SHORT_ARGUMENTS_DESCRIPTION))
+        buffer.write('%s [<options>] %s\n\n' % \
+                         (sys.argv[0], self.getArgumentsSyntaxString())
+                     )
 
         grouped_options = self._groupOptionAliases()
 
 
 To do:
 
-- more unit tests
 - switch to gnu_getopt?
 - check command line arguments

test_CommandLineApp.py

         CLALongOptionTest( [ '--test-args=foo' ] ).run()
         return
 
+    def testHelpForMainArgs(self):
+        class CLAOneMainArg(CommandLineApp):
+            def main(self, argname):
+                return
+        
+        app = CLAOneMainArg()
+        self.failUnlessEqual(app.getArgumentsSyntaxString(), 'argname')
+
+        class CLAListMainArg(CommandLineApp):
+            def main(self, *argname):
+                return
+        
+        app = CLAListMainArg()
+        self.failUnlessEqual(app.getArgumentsSyntaxString(), 'argname [argname...]')
+
+        class CLAComboMainArg(CommandLineApp):
+            def main(self, onearg, *listarg):
+                return
+        
+        app = CLAComboMainArg()
+        self.failUnlessEqual(app.getArgumentsSyntaxString(), 
+                             'onearg listarg [listarg...]')
+
+        class CLATwoSinglesMainArg(CommandLineApp):
+            def main(self, onearg, twoarg, *listarg):
+                return
+        
+        app = CLATwoSinglesMainArg()
+        self.failUnlessEqual(app.getArgumentsSyntaxString(), 
+                             'onearg twoarg listarg [listarg...]')
+        return
+
     def testArgsToMain(self):
         class CLALongOptionTest(CommandLineApp):
             force_exit = 0
 
         app = CLAHelpTest()
         s = app.getSimpleSyntaxHelpString()
-        self.failUnlessEqual(s, '''test_CommandLineApp.py [<options>] 
+        self.failUnlessEqual(s, '''test_CommandLineApp.py [<options>] args [args...]
 
     --debug
     -h
         self.failUnlessEqual(s, '''
 SYNTAX:
 
-  test_CommandLineApp.py [<options>] 
+  test_CommandLineApp.py [<options>] args [args...]
 
     --debug
     -h