[Lldb-commits] [lldb] r163773 - /lldb/trunk/examples/python/cmdtemplate.py

Greg Clayton gclayton at apple.com
Wed Sep 12 22:35:34 PDT 2012

Author: gclayton
Date: Thu Sep 13 00:35:34 2012
New Revision: 163773

URL: http://llvm.org/viewvc/llvm-project?rev=163773&view=rev
Modified the command template to include best practices.


Modified: lldb/trunk/examples/python/cmdtemplate.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/examples/python/cmdtemplate.py?rev=163773&r1=163772&r2=163773&view=diff
--- lldb/trunk/examples/python/cmdtemplate.py (original)
+++ lldb/trunk/examples/python/cmdtemplate.py Thu Sep 13 00:35:34 2012
@@ -7,12 +7,6 @@
 # import it with the full path using the "command script import" 
 # command
 #   (lldb) command script import /path/to/cmdtemplate.py
-# For the shells csh, tcsh:
-#   ( setenv PYTHONPATH /path/to/LLDB.framework/Resources/Python ; ./cmdtemplate.py )
-# For the shells sh, bash:
-#   PYTHONPATH=/path/to/LLDB.framework/Resources/Python ./cmdtemplate.py 
 import lldb
@@ -20,12 +14,27 @@
 import optparse
 import shlex
-def ls(debugger, command, result, dict):
-    command_args = shlex.split(command)
+def create_ls_options():
     usage = "usage: %prog [options] <PATH> [PATH ...]"
-    description='''This command lets you run the /bin/ls command from within lldb as a quick and easy example.'''
+    description='''This command lets you run the /bin/ls shell command from
+within lldb. This code is designed to demonstrate the best principles that 
+should be used when creating a new LLDB command through python.
+Creating the options in a separate function allows the parser to be
+created without running the command. The usage string is generated by the
+optparse module and can be used to populate the ls.__doc__ documentation
+string in the command interpreter function prior to registering the
+command with LLDB. The allows the output of "ls --help" to exactly match
+the output of "help ls" when both commands are run from within LLDB.
     parser = optparse.OptionParser(description=description, prog='ls',usage=usage)
     parser.add_option('-v', '--verbose', action='store_true', dest='verbose', help='display verbose debug info', default=False)
+    return parser
+def ls(debugger, command, result, dict):
+    # Use the Shell Lexer to properly parse up command options just like a 
+    # shell would
+    command_args = shlex.split(command)
+    parser = create_ls_options()
         (options, args) = parser.parse_args(command_args)
@@ -37,14 +46,12 @@
             result.PutCString(commands.getoutput('/bin/ls -lAF "%s"' % arg))
-if __name__ == '__main__':
-    # This script is being run from the command line, create a debugger in case we are
-    # going to use any debugger functions in our function.
-    lldb.debugger = lldb.SBDebugger.Create()
-    ls (sys.argv)
 def __lldb_init_module (debugger, dict):
-    # This initializer is being run from LLDB in the embedded command interpreter
+    # This initializer is being run from LLDB in the embedded command interpreter    
+    # Make the options so we can generate the help text for the new LLDB 
+    # command line command prior to registering it with LLDB below
+    parser = create_ls_options()
+    ls.__doc__ = parser.format_help()
     # Add any commands contained in this module to LLDB
     debugger.HandleCommand('command script add -f cmdtemplate.ls ls')
-    print '"ls" command installed, type "ls --help" for detailed help'
+    print 'The "ls" command has been installed, type "help ls" or "ls --help" for detailed help.'

More information about the lldb-commits mailing list