LLDB  mainline
OptionGroupPythonClassWithDict.cpp
Go to the documentation of this file.
1 //===-- OptionGroupPythonClassWithDict.cpp --------------------------------===//
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 
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, bool is_class, int class_option, int key_option,
18  int value_option, uint16_t required_options)
19  : m_is_class(is_class), m_required_options(required_options) {
20  m_key_usage_text.assign("The key for a key/value pair passed to the "
21  "implementation of a ");
22  m_key_usage_text.append(class_use);
23  m_key_usage_text.append(". Pairs can be specified more than once.");
24 
25  m_value_usage_text.assign("The value for the previous key in the pair passed "
26  "to the implementation of a ");
27  m_value_usage_text.append(class_use);
28  m_value_usage_text.append(". Pairs can be specified more than once.");
29 
30  m_class_usage_text.assign("The name of the ");
31  m_class_usage_text.append(m_is_class ? "class" : "function");
32  m_class_usage_text.append(" that will manage a ");
33  m_class_usage_text.append(class_use);
34  m_class_usage_text.append(".");
35 
38  m_option_definition[0].long_option = "script-class";
39  m_option_definition[0].short_option = class_option;
40  m_option_definition[0].validator = nullptr;
46 
49  m_option_definition[1].long_option = "structured-data-key";
50  m_option_definition[1].short_option = key_option;
51  m_option_definition[1].validator = nullptr;
57 
60  m_option_definition[2].long_option = "structured-data-value";
61  m_option_definition[2].short_option = value_option;
62  m_option_definition[2].validator = nullptr;
68 
71  m_option_definition[3].long_option = "python-function";
72  m_option_definition[3].short_option = class_option;
73  m_option_definition[3].validator = nullptr;
79 }
80 
82  uint32_t option_idx,
83  llvm::StringRef option_arg,
84  ExecutionContext *execution_context) {
85  Status error;
86  switch (option_idx) {
87  case 0:
88  case 3: {
89  m_name.assign(std::string(option_arg));
90  } break;
91  case 1: {
92  if (!m_dict_sp)
93  m_dict_sp = std::make_shared<StructuredData::Dictionary>();
94  if (m_current_key.empty())
95  m_current_key.assign(std::string(option_arg));
96  else
97  error.SetErrorStringWithFormat("Key: \"%s\" missing value.",
98  m_current_key.c_str());
99 
100  } break;
101  case 2: {
102  if (!m_dict_sp)
103  m_dict_sp = std::make_shared<StructuredData::Dictionary>();
104  if (!m_current_key.empty()) {
105  m_dict_sp->AddStringItem(m_current_key, option_arg);
106  m_current_key.clear();
107  }
108  else
109  error.SetErrorStringWithFormat("Value: \"%s\" missing matching key.",
110  option_arg.str().c_str());
111  } break;
112  default:
113  llvm_unreachable("Unimplemented option");
114  }
115  return error;
116 }
117 
119  ExecutionContext *execution_context) {
120  m_current_key.erase();
121  // Leave the dictionary shared pointer unset. That way you can tell that
122  // the user didn't pass any -k -v pairs. We want to be able to warn if these
123  // were passed when the function they passed won't use them.
124  m_dict_sp.reset();
125  m_name.clear();
126 }
127 
129  ExecutionContext *execution_context) {
130  Status error;
131  // If we get here and there's contents in the m_current_key, somebody must
132  // have provided a key but no value.
133  if (!m_current_key.empty())
134  error.SetErrorStringWithFormat("Key: \"%s\" missing value.",
135  m_current_key.c_str());
136  return error;
137 }
138 
lldb_private::ExecutionContext
Definition: ExecutionContext.h:291
lldb_private::OptionGroupPythonClassWithDict::ePythonFunction
@ ePythonFunction
Definition: OptionGroupPythonClassWithDict.h:31
lldb_private::OptionGroupPythonClassWithDict::m_key_usage_text
std::string m_key_usage_text
Definition: OptionGroupPythonClassWithDict.h:64
lldb_private::Flags::Test
bool Test(ValueType bit) const
Test a single flag bit.
Definition: Flags.h:96
lldb_private::OptionDefinition::required
bool required
This option is required (in the current usage level).
Definition: OptionDefinition.h:25
lldb::eArgTypePythonFunction
@ eArgTypePythonFunction
Definition: lldb-enumerations.h:565
lldb_private::OptionGroupPythonClassWithDict::m_dict_sp
StructuredData::DictionarySP m_dict_sp
Definition: OptionGroupPythonClassWithDict.h:63
lldb_private::OptionGroupPythonClassWithDict::m_name
std::string m_name
Definition: OptionGroupPythonClassWithDict.h:61
lldb_private::OptionGroupPythonClassWithDict::OptionParsingFinished
Status OptionParsingFinished(ExecutionContext *execution_context) override
Definition: OptionGroupPythonClassWithDict.cpp:128
lldb_private::OptionDefinition::validator
OptionValidator * validator
If non-NULL, option is valid iff |validator->IsValid()|, otherwise always valid.
Definition: OptionDefinition.h:37
lldb_private::OptionGroupPythonClassWithDict::SetOptionValue
Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_value, ExecutionContext *execution_context) override
Definition: OptionGroupPythonClassWithDict.cpp:81
lldb_private::OptionGroupPythonClassWithDict::eDictValue
@ eDictValue
Definition: OptionGroupPythonClassWithDict.h:30
error
static llvm::raw_ostream & error(Stream &strm)
Definition: CommandReturnObject.cpp:17
lldb_private::OptionDefinition::completion_type
uint32_t completion_type
The kind of completion for this option.
Definition: OptionDefinition.h:42
lldb_private::OptionDefinition::usage_text
const char * usage_text
Full text explaining what this options does and what (if any) argument to pass it.
Definition: OptionDefinition.h:47
lldb_private::OptionDefinition::long_option
const char * long_option
Full name for this option.
Definition: OptionDefinition.h:27
lldb_private::OptionDefinition::option_has_arg
int option_has_arg
no_argument, required_argument or optional_argument
Definition: OptionDefinition.h:34
OptionGroupPythonClassWithDict.h
lldb::eArgTypePythonClass
@ eArgTypePythonClass
Definition: lldb-enumerations.h:564
LLDB_OPT_SET_1
#define LLDB_OPT_SET_1
Definition: lldb-defines.h:112
lldb_private::OptionGroupPythonClassWithDict::m_option_definition
OptionDefinition m_option_definition[4]
Definition: OptionGroupPythonClassWithDict.h:66
LLDB_OPT_SET_3
#define LLDB_OPT_SET_3
Definition: lldb-defines.h:114
lldb_private::OptionGroupPythonClassWithDict::m_required_options
Flags m_required_options
Definition: OptionGroupPythonClassWithDict.h:67
lldb_private::OptionGroupPythonClassWithDict::eDictKey
@ eDictKey
Definition: OptionGroupPythonClassWithDict.h:29
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:38
lldb_private::OptionGroupPythonClassWithDict::eScriptClass
@ eScriptClass
Definition: OptionGroupPythonClassWithDict.h:28
lldb_private::OptionGroupPythonClassWithDict::m_class_usage_text
std::string m_class_usage_text
Definition: OptionGroupPythonClassWithDict.h:64
lldb_private::Status
Definition: Status.h:44
uint32_t
lldb::eArgTypeNone
@ eArgTypeNone
Definition: lldb-enumerations.h:595
lldb_private::OptionDefinition::usage_mask
uint32_t usage_mask
Used to mark options that can be used together.
Definition: OptionDefinition.h:23
uint16_t
lldb_private::OptionGroupPythonClassWithDict::m_value_usage_text
std::string m_value_usage_text
Definition: OptionGroupPythonClassWithDict.h:64
OptionParser.h
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::OptionGroupPythonClassWithDict::m_is_class
bool m_is_class
Definition: OptionGroupPythonClassWithDict.h:65
lldb_private::OptionGroupPythonClassWithDict::m_current_key
std::string m_current_key
Definition: OptionGroupPythonClassWithDict.h:62
lldb_private::OptionGroupPythonClassWithDict::OptionParsingStarting
void OptionParsingStarting(ExecutionContext *execution_context) override
Definition: OptionGroupPythonClassWithDict.cpp:118
lldb_private::OptionDefinition::enum_values
OptionEnumValues enum_values
If not empty, an array of enum values.
Definition: OptionDefinition.h:39
lldb_private::OptionDefinition::argument_type
lldb::CommandArgumentType argument_type
Type of argument this option takes.
Definition: OptionDefinition.h:44
lldb_private::OptionDefinition::short_option
int short_option
Single character for this option.
Definition: OptionDefinition.h:32
LLDB_OPT_SET_2
#define LLDB_OPT_SET_2
Definition: lldb-defines.h:113
lldb
Definition: SBAddress.h:15
lldb_private::OptionParser::eRequiredArgument
@ eRequiredArgument
Definition: OptionParser.h:35