blob: 9936d0e68e26b27830041f313e685a263e4fce50 [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:
Enrico Granata6308f582013-02-25 23:01:08 +000041 # if you don't handle exceptions, passing an incorrect argument to the OptionParser will cause LLDB to exit
42 # (courtesy of OptParse dealing with argument errors by throwing SystemExit)
Jim Ingham6c00a012013-02-21 18:38:46 +000043 result.SetStatus (lldb.eReturnStatusFailed)
Greg Claytond879a632012-01-22 02:55:08 +000044 return
45
46 for arg in args:
47 if options.verbose:
Jim Ingham87a59362012-01-24 02:40:42 +000048 result.PutCString(commands.getoutput('/bin/ls "%s"' % arg))
Greg Claytond879a632012-01-22 02:55:08 +000049 else:
Jim Ingham87a59362012-01-24 02:40:42 +000050 result.PutCString(commands.getoutput('/bin/ls -lAF "%s"' % arg))
Greg Claytond879a632012-01-22 02:55:08 +000051
Jim Ingham87a59362012-01-24 02:40:42 +000052def __lldb_init_module (debugger, dict):
Greg Claytonf4539072012-09-13 05:35:34 +000053 # This initializer is being run from LLDB in the embedded command interpreter
54 # Make the options so we can generate the help text for the new LLDB
55 # command line command prior to registering it with LLDB below
56 parser = create_ls_options()
57 ls.__doc__ = parser.format_help()
Greg Claytond879a632012-01-22 02:55:08 +000058 # Add any commands contained in this module to LLDB
Jim Ingham87a59362012-01-24 02:40:42 +000059 debugger.HandleCommand('command script add -f cmdtemplate.ls ls')
Greg Claytonf4539072012-09-13 05:35:34 +000060 print 'The "ls" command has been installed, type "help ls" or "ls --help" for detailed help.'