blob: eec1d3712778cbb4f183f3f58afdbada7bdbed1c [file] [log] [blame]
Greg Claytond879a632012-01-22 02:55:08 +00001#!/usr/bin/python
2
3#----------------------------------------------------------------------
4# Be sure to add the python path that points to the LLDB shared library.
5#
Greg Claytone2841632012-01-26 02:56:24 +00006# # To use this in the embedded python interpreter using "lldb" just
7# import it with the full path using the "command script import"
8# command
9# (lldb) command script import /path/to/cmdtemplate.py
Greg Claytond879a632012-01-22 02:55:08 +000010#----------------------------------------------------------------------
11
12import lldb
13import commands
14import optparse
15import shlex
16
Greg Claytonf4539072012-09-13 05:35:34 +000017def create_ls_options():
Greg Claytond879a632012-01-22 02:55:08 +000018 usage = "usage: %prog [options] <PATH> [PATH ...]"
Greg Claytonf4539072012-09-13 05:35:34 +000019 description='''This command lets you run the /bin/ls shell command from
20within lldb. This code is designed to demonstrate the best principles that
21should be used when creating a new LLDB command through python.
22Creating the options in a separate function allows the parser to be
23created without running the command. The usage string is generated by the
24optparse module and can be used to populate the ls.__doc__ documentation
25string in the command interpreter function prior to registering the
26command with LLDB. The allows the output of "ls --help" to exactly match
27the output of "help ls" when both commands are run from within LLDB.
28'''
Greg Claytond879a632012-01-22 02:55:08 +000029 parser = optparse.OptionParser(description=description, prog='ls',usage=usage)
30 parser.add_option('-v', '--verbose', action='store_true', dest='verbose', help='display verbose debug info', default=False)
Greg Claytonf4539072012-09-13 05:35:34 +000031 return parser
32
33def ls(debugger, command, result, dict):
34 # Use the Shell Lexer to properly parse up command options just like a
35 # shell would
36 command_args = shlex.split(command)
37 parser = create_ls_options()
Greg Claytond879a632012-01-22 02:55:08 +000038 try:
39 (options, args) = parser.parse_args(command_args)
40 except:
41 return
42
43 for arg in args:
44 if options.verbose:
Jim Ingham87a59362012-01-24 02:40:42 +000045 result.PutCString(commands.getoutput('/bin/ls "%s"' % arg))
Greg Claytond879a632012-01-22 02:55:08 +000046 else:
Jim Ingham87a59362012-01-24 02:40:42 +000047 result.PutCString(commands.getoutput('/bin/ls -lAF "%s"' % arg))
Greg Claytond879a632012-01-22 02:55:08 +000048
Jim Ingham87a59362012-01-24 02:40:42 +000049def __lldb_init_module (debugger, dict):
Greg Claytonf4539072012-09-13 05:35:34 +000050 # This initializer is being run from LLDB in the embedded command interpreter
51 # Make the options so we can generate the help text for the new LLDB
52 # command line command prior to registering it with LLDB below
53 parser = create_ls_options()
54 ls.__doc__ = parser.format_help()
Greg Claytond879a632012-01-22 02:55:08 +000055 # Add any commands contained in this module to LLDB
Jim Ingham87a59362012-01-24 02:40:42 +000056 debugger.HandleCommand('command script add -f cmdtemplate.ls ls')
Greg Claytonf4539072012-09-13 05:35:34 +000057 print 'The "ls" command has been installed, type "help ls" or "ls --help" for detailed help.'