[Lldb-commits] [lldb] Add the ability to define a Python based command that uses CommandObjectParsed (PR #70734)
via lldb-commits
lldb-commits at lists.llvm.org
Tue Feb 6 13:58:14 PST 2024
================
@@ -0,0 +1,313 @@
+"""
+This module implements a couple of utility classes to make writing
+lldb parsed commands more Pythonic.
+The way to use it is to make a class for your command that inherits from ParsedCommandBase.
+That will make an LLDBOVParser which you will use for your
+option definition, and to fetch option values for the current invocation
+of your command. Access to the OV parser is through:
+
+ParsedCommandBase.get_parser()
+
+Next, implement setup_command_definition() in your new command class, and call:
+
+ self.get_parser().add_option()
+
+to add all your options. The order doesn't matter for options, lldb will sort them
+alphabetically for you when it prints help.
+
+Similarly you can define the arguments with:
+
+ self.get_parser().add_argument()
+
+At present, lldb doesn't do as much work as it should verifying arguments, it
+only checks that commands that take no arguments don't get passed arguments.
+
+Then implement the execute function for your command as:
+
+ def __call__(self, debugger, args_list, exe_ctx, result):
+
+The arguments will be a list of strings.
+
+You can access the option values using the 'dest' string you passed in when defining the option.
+
+If you need to know whether a given option was set by the user or not, you can
+use the was_set API.
+
+There are example commands in the lldb testsuite at:
+
+llvm-project/lldb/test/API/commands/command/script/add/test_commands.py
+"""
+import inspect
+import lldb
+import sys
+from abc import abstractmethod
+
+class LLDBOVParser:
+ def __init__(self):
+ # This is a dictionary of dictionaries. The key is the long option
+ # name, and the value is the rest of the definition.
+ self.options_dict = {}
+ self.args_array = []
+
+ # Some methods to translate common value types. Should return a
+ # tuple of the value and an error value (True => error) if the
+ # type can't be converted.
+ # FIXME: Need a way to push the conversion error string back to lldb.
+ @staticmethod
+ def to_bool(in_value):
+ error = True
+ value = False
+ if type(in_value) != str or len(in_value) == 0:
+ return (value, error)
+
+ low_in = in_value.lower()
+ if low_in == "y" or low_in == "yes" or low_in == "t" or low_in == "true" or low_in == "1":
+ value = True
+ error = False
+
+ if not value and low_in == "n" or low_in == "no" or low_in == "f" or low_in == "false" or low_in == "0":
----------------
jimingham wrote:
string in array of strings is definitely easier to read, thanks!
I don't think the second comment is right, however. Not all input values will make it into either of those if statements, for instance "low_in = definitely_not_a_boolean" will be rejected by both if's and so "error" will be left at True.
All of this conversion code should actually go away, because it's silly to have two versions of OptionArgParser::ToBool, one in C++ and one here in Python. This is just a shortcut for now because this patch was already getting somewhat unwieldily.
https://github.com/llvm/llvm-project/pull/70734
More information about the lldb-commits
mailing list