[Lldb-commits] [lldb] e2d9420 - Make flag-only options work in the ParsedCommand mode of adding commands (#157756)
via lldb-commits
lldb-commits at lists.llvm.org
Wed Sep 10 09:27:32 PDT 2025
Author: jimingham
Date: 2025-09-10T09:27:29-07:00
New Revision: e2d9420272e2f479dc714706be5a4aa0a4b2c90d
URL: https://github.com/llvm/llvm-project/commit/e2d9420272e2f479dc714706be5a4aa0a4b2c90d
DIFF: https://github.com/llvm/llvm-project/commit/e2d9420272e2f479dc714706be5a4aa0a4b2c90d.diff
LOG: Make flag-only options work in the ParsedCommand mode of adding commands (#157756)
I neglected to add a test when I was writing tests for this, so of
course it broke. This makes it work again and adds a test.
rdar://159459160
Added:
Modified:
lldb/examples/python/cmdtemplate.py
lldb/examples/python/templates/parsed_cmd.py
lldb/test/API/commands/command/script/add/TestAddParsedCommand.py
lldb/test/API/commands/command/script/add/test_commands.py
Removed:
################################################################################
diff --git a/lldb/examples/python/cmdtemplate.py b/lldb/examples/python/cmdtemplate.py
index a9fbe0b40e195..be4c2da53ee06 100644
--- a/lldb/examples/python/cmdtemplate.py
+++ b/lldb/examples/python/cmdtemplate.py
@@ -74,6 +74,11 @@ def setup_command_definition(self):
dest = "statics",
default = True,
)
+ ov_parser.add_option(
+ "t",
+ "test-flag",
+ help="test a flag value.",
+ )
def get_repeat_command(self, args):
"""As an example, make the command not auto-repeat:"""
@@ -123,6 +128,11 @@ def __call__(self, debugger, command, exe_ctx, result):
% (variables_count, total_size, average_size),
file=result,
)
+ if ov_parser.was_set("test-flag"):
+ print("Got the test flag")
+ else:
+ print("Got no test flag")
+
# not returning anything is akin to returning success
diff --git a/lldb/examples/python/templates/parsed_cmd.py b/lldb/examples/python/templates/parsed_cmd.py
index 13d6eae405c08..c1baad3ffd247 100644
--- a/lldb/examples/python/templates/parsed_cmd.py
+++ b/lldb/examples/python/templates/parsed_cmd.py
@@ -241,12 +241,18 @@ def option_parsing_started(self):
starts, you can override this to handle your special option. """
for key, elem in self.options_dict.items():
elem['_value_set'] = False
+ # If there's no value_type, then there can't be a dest.
+ if not "value_type" in elem:
+ continue
+
try:
object.__setattr__(self, elem["dest"], elem["default"])
except AttributeError:
# It isn't an error not to have a "dest" variable name, you'll
# just have to manage this option's value on your own.
continue
+ except KeyError:
+ continue
def set_enum_value(self, enum_values, input):
""" This sets the value for an enum option, you should not have to call this
@@ -271,7 +277,13 @@ def set_option_value(self, exe_ctx, opt_name, opt_value):
elem = self.get_option_element(opt_name)
if not elem:
return False
-
+
+ # If there's no value_type in element, then it has no value, so just mark
+ # it set and return:
+ if not "value_type" in elem:
+ elem["_value_set"] = True
+ return True
+
if "enum_values" in elem:
(value, error) = self.set_enum_value(elem["enum_values"], opt_value)
else:
@@ -312,10 +324,19 @@ def dest_for_option(self, opt_name):
value = self.__dict__[elem["dest"]]
return value
- def add_option(self, short_option, long_option, help, default,
- dest = None, required=False, groups = None,
- value_type=lldb.eArgTypeNone, completion_type=None,
- enum_values=None):
+ def add_option(
+ self,
+ short_option,
+ long_option,
+ help,
+ default=None,
+ dest=None,
+ required=False,
+ groups=None,
+ value_type=None,
+ completion_type=None,
+ enum_values=None,
+ ):
"""
short_option: one character, must be unique, not required
long_option: no spaces, must be unique, required
@@ -340,17 +361,27 @@ def add_option(self, short_option, long_option, help, default,
if not completion_type:
completion_type = self.determine_completion(value_type)
-
- dict = {"short_option" : short_option,
- "required" : required,
- "help" : help,
- "value_type" : value_type,
- "completion_type" : completion_type,
- "dest" : dest,
- "default" : default}
+
+ dict = {
+ "short_option": short_option,
+ "required": required,
+ "help": help,
+ }
if enum_values:
+ if not value_type:
+ print("I am setting value type for an enum value")
+ value_type = lldb.eArgTypeNone
+ else:
+ print(f"An enum value had a type: {value_type}")
dict["enum_values"] = enum_values
+
+ if value_type:
+ dict["value_type"] = value_type
+ dict["completion_type"] = completion_type
+ dict["dest"] = dest
+ dict["default"] = default
+
if groups:
dict["groups"] = groups
diff --git a/lldb/test/API/commands/command/script/add/TestAddParsedCommand.py b/lldb/test/API/commands/command/script/add/TestAddParsedCommand.py
index 2faf2f45046d7..9deebe29eaae4 100644
--- a/lldb/test/API/commands/command/script/add/TestAddParsedCommand.py
+++ b/lldb/test/API/commands/command/script/add/TestAddParsedCommand.py
@@ -34,6 +34,7 @@ def check_help_options(self, cmd_name, opt_list, substrs=[]):
else:
(short_opt, type, long_opt) = elem
substrs.append(f"-{short_opt} <{type}> ( --{long_opt} <{type}> )")
+
self.expect("help " + cmd_name, substrs=substrs)
def run_one_repeat(self, commands, expected_num_errors):
@@ -215,6 +216,19 @@ def cleanup():
"bool-arg (set: True): False",
"shlib-name (set: True): Something",
"disk-file-name (set: False):",
+ "flag-value (set: False):",
+ "line-num (set: False):",
+ "enum-option (set: False):",
+ ],
+ )
+ # Make sure flag values work:
+ self.expect(
+ "no-args -b false -s Something -f",
+ substrs=[
+ "bool-arg (set: True): False",
+ "shlib-name (set: True): Something",
+ "disk-file-name (set: False):",
+ "flag-value (set: True):",
"line-num (set: False):",
"enum-option (set: False):",
],
diff --git a/lldb/test/API/commands/command/script/add/test_commands.py b/lldb/test/API/commands/command/script/add/test_commands.py
index b15ea935c0586..db302796819ad 100644
--- a/lldb/test/API/commands/command/script/add/test_commands.py
+++ b/lldb/test/API/commands/command/script/add/test_commands.py
@@ -16,10 +16,16 @@ def __call__(self, debugger, args_array, exe_ctx, result):
if len(opt_def):
result.AppendMessage("Options:\n")
for long_option, elem in opt_def.items():
- dest = elem["dest"]
- result.AppendMessage(
- f"{long_option} (set: {elem['_value_set']}): {object.__getattribute__(self.get_parser(), dest)}\n"
- )
+ if "value_type" in elem:
+ print(f"Looking at {long_option} - {elem}")
+ dest = elem["dest"]
+ result.AppendMessage(
+ f"{long_option} (set: {elem['_value_set']}): {object.__getattribute__(self.get_parser(), dest)}\n"
+ )
+ else:
+ result.AppendMessage(
+ f"{long_option} (set: {elem['_value_set']}): flag value\n"
+ )
else:
result.AppendMessage("No options\n")
@@ -78,6 +84,8 @@ def setup_command_definition(self):
default=None,
)
+ ov_parser.add_option("f", "flag-value", "This is a flag value")
+
ov_parser.add_option(
"l",
"line-num",
More information about the lldb-commits
mailing list