-
Notifications
You must be signed in to change notification settings - Fork 15.2k
Make flag-only options work in the ParsedCommand mode of adding commands #157756
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
commands. 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
@llvm/pr-subscribers-lldb Author: None (jimingham) ChangesI 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 Full diff: https://github.com/llvm/llvm-project/pull/157756.diff 4 Files Affected:
diff --git a/lldb/examples/python/cmdtemplate.py b/lldb/examples/python/cmdtemplate.py
index a9fbe0b40e195..e84690575053c 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..e770e5e78771f 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,9 +324,9 @@ def dest_for_option(self, opt_name):
value = self.__dict__[elem["dest"]]
return value
- def add_option(self, short_option, long_option, help, default,
+ def add_option(self, short_option, long_option, help, default = None,
dest = None, required=False, groups = None,
- value_type=lldb.eArgTypeNone, completion_type=None,
+ value_type=None, completion_type=None,
enum_values=None):
"""
short_option: one character, must be unique, not required
@@ -344,13 +356,22 @@ def add_option(self, short_option, long_option, help, default,
dict = {"short_option" : short_option,
"required" : required,
"help" : help,
- "value_type" : value_type,
- "completion_type" : completion_type,
- "dest" : dest,
- "default" : default}
+ }
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..5938a180e10ab 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,8 +216,21 @@ 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):",
+ "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..d1bd0bbadf94d 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,12 @@ 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",
|
✅ With the latest revision this PR passed the Python code formatter. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
…nds (llvm#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 (cherry picked from commit e2d9420)
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