1 //===-- OptionGroupPythonClassWithDict.cpp ----------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "lldb/Interpreter/OptionGroupPythonClassWithDict.h" 10 11 #include "lldb/Host/OptionParser.h" 12 13 using namespace lldb; 14 using namespace lldb_private; 15 16 OptionGroupPythonClassWithDict::OptionGroupPythonClassWithDict 17 (const char *class_use, 18 int class_option, 19 int key_option, 20 int value_option, 21 const char *class_long_option, 22 const char *key_long_option, 23 const char *value_long_option, 24 bool required) { 25 m_key_usage_text.assign("The key for a key/value pair passed to the class" 26 " that implements a "); 27 m_key_usage_text.append(class_use); 28 m_key_usage_text.append(". Pairs can be specified more than once."); 29 30 m_value_usage_text.assign("The value for a previous key in the pair passed to" 31 " the class that implements a "); 32 m_value_usage_text.append(class_use); 33 m_value_usage_text.append(". Pairs can be specified more than once."); 34 35 m_class_usage_text.assign("The name of the class that will manage a "); 36 m_class_usage_text.append(class_use); 37 m_class_usage_text.append("."); 38 39 m_option_definition[0].usage_mask = LLDB_OPT_SET_1; 40 m_option_definition[0].required = required; 41 m_option_definition[0].long_option = class_long_option; 42 m_option_definition[0].short_option = class_option; 43 m_option_definition[0].validator = nullptr; 44 m_option_definition[0].option_has_arg = OptionParser::eRequiredArgument; 45 m_option_definition[0].enum_values = {}; 46 m_option_definition[0].completion_type = 0; 47 m_option_definition[0].argument_type = eArgTypePythonClass; 48 m_option_definition[0].usage_text = m_class_usage_text.data(); 49 50 m_option_definition[1].usage_mask = LLDB_OPT_SET_1; 51 m_option_definition[1].required = required; 52 m_option_definition[1].long_option = key_long_option; 53 m_option_definition[1].short_option = key_option; 54 m_option_definition[1].validator = nullptr; 55 m_option_definition[1].option_has_arg = OptionParser::eRequiredArgument; 56 m_option_definition[1].enum_values = {}; 57 m_option_definition[1].completion_type = 0; 58 m_option_definition[1].argument_type = eArgTypeNone; 59 m_option_definition[1].usage_text = m_key_usage_text.data(); 60 61 m_option_definition[2].usage_mask = LLDB_OPT_SET_1; 62 m_option_definition[2].required = required; 63 m_option_definition[2].long_option = value_long_option; 64 m_option_definition[2].short_option = value_option; 65 m_option_definition[2].validator = nullptr; 66 m_option_definition[2].option_has_arg = OptionParser::eRequiredArgument; 67 m_option_definition[2].enum_values = {}; 68 m_option_definition[2].completion_type = 0; 69 m_option_definition[2].argument_type = eArgTypeNone; 70 m_option_definition[2].usage_text = m_value_usage_text.data(); 71 } 72 73 OptionGroupPythonClassWithDict::~OptionGroupPythonClassWithDict() {} 74 75 Status OptionGroupPythonClassWithDict::SetOptionValue( 76 uint32_t option_idx, 77 llvm::StringRef option_arg, 78 ExecutionContext *execution_context) { 79 Status error; 80 switch (option_idx) { 81 case 0: { 82 m_class_name.assign(option_arg); 83 } break; 84 case 1: { 85 if (m_current_key.empty()) 86 m_current_key.assign(option_arg); 87 else 88 error.SetErrorStringWithFormat("Key: \"%s\" missing value.", 89 m_current_key.c_str()); 90 91 } break; 92 case 2: { 93 if (!m_current_key.empty()) { 94 m_dict_sp->AddStringItem(m_current_key, option_arg); 95 m_current_key.clear(); 96 } 97 else 98 error.SetErrorStringWithFormat("Value: \"%s\" missing matching key.", 99 option_arg.str().c_str()); 100 } break; 101 default: 102 llvm_unreachable("Unimplemented option"); 103 } 104 return error; 105 } 106 107 void OptionGroupPythonClassWithDict::OptionParsingStarting( 108 ExecutionContext *execution_context) { 109 m_current_key.erase(); 110 m_dict_sp = std::make_shared<StructuredData::Dictionary>(); 111 } 112 113 Status OptionGroupPythonClassWithDict::OptionParsingFinished( 114 ExecutionContext *execution_context) { 115 Status error; 116 // If we get here and there's contents in the m_current_key, somebody must 117 // have provided a key but no value. 118 if (!m_current_key.empty()) 119 error.SetErrorStringWithFormat("Key: \"%s\" missing value.", 120 m_current_key.c_str()); 121 return error; 122 } 123 124