Greg Clayton | d879a63 | 2012-01-22 02:55:08 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | #---------------------------------------------------------------------- |
| 4 | # Be sure to add the python path that points to the LLDB shared library. |
| 5 | # |
Greg Clayton | e284163 | 2012-01-26 02:56:24 +0000 | [diff] [blame] | 6 | # # 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 Clayton | d879a63 | 2012-01-22 02:55:08 +0000 | [diff] [blame] | 10 | #---------------------------------------------------------------------- |
| 11 | |
| 12 | import lldb |
| 13 | import commands |
| 14 | import optparse |
| 15 | import shlex |
| 16 | |
Greg Clayton | f453907 | 2012-09-13 05:35:34 +0000 | [diff] [blame] | 17 | def create_ls_options(): |
Greg Clayton | d879a63 | 2012-01-22 02:55:08 +0000 | [diff] [blame] | 18 | usage = "usage: %prog [options] <PATH> [PATH ...]" |
Greg Clayton | f453907 | 2012-09-13 05:35:34 +0000 | [diff] [blame] | 19 | description='''This command lets you run the /bin/ls shell command from |
| 20 | within lldb. This code is designed to demonstrate the best principles that |
| 21 | should be used when creating a new LLDB command through python. |
| 22 | Creating the options in a separate function allows the parser to be |
| 23 | created without running the command. The usage string is generated by the |
| 24 | optparse module and can be used to populate the ls.__doc__ documentation |
| 25 | string in the command interpreter function prior to registering the |
| 26 | command with LLDB. The allows the output of "ls --help" to exactly match |
| 27 | the output of "help ls" when both commands are run from within LLDB. |
| 28 | ''' |
Greg Clayton | d879a63 | 2012-01-22 02:55:08 +0000 | [diff] [blame] | 29 | 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 Clayton | f453907 | 2012-09-13 05:35:34 +0000 | [diff] [blame] | 31 | return parser |
| 32 | |
| 33 | def 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 Clayton | d879a63 | 2012-01-22 02:55:08 +0000 | [diff] [blame] | 38 | try: |
| 39 | (options, args) = parser.parse_args(command_args) |
| 40 | except: |
| 41 | return |
| 42 | |
| 43 | for arg in args: |
| 44 | if options.verbose: |
Jim Ingham | 87a5936 | 2012-01-24 02:40:42 +0000 | [diff] [blame] | 45 | result.PutCString(commands.getoutput('/bin/ls "%s"' % arg)) |
Greg Clayton | d879a63 | 2012-01-22 02:55:08 +0000 | [diff] [blame] | 46 | else: |
Jim Ingham | 87a5936 | 2012-01-24 02:40:42 +0000 | [diff] [blame] | 47 | result.PutCString(commands.getoutput('/bin/ls -lAF "%s"' % arg)) |
Greg Clayton | d879a63 | 2012-01-22 02:55:08 +0000 | [diff] [blame] | 48 | |
Jim Ingham | 87a5936 | 2012-01-24 02:40:42 +0000 | [diff] [blame] | 49 | def __lldb_init_module (debugger, dict): |
Greg Clayton | f453907 | 2012-09-13 05:35:34 +0000 | [diff] [blame] | 50 | # 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 Clayton | d879a63 | 2012-01-22 02:55:08 +0000 | [diff] [blame] | 55 | # Add any commands contained in this module to LLDB |
Jim Ingham | 87a5936 | 2012-01-24 02:40:42 +0000 | [diff] [blame] | 56 | debugger.HandleCommand('command script add -f cmdtemplate.ls ls') |
Greg Clayton | f453907 | 2012-09-13 05:35:34 +0000 | [diff] [blame] | 57 | print 'The "ls" command has been installed, type "help ls" or "ls --help" for detailed help.' |