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      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   const int short_option = m_option_definition[option_idx].short_option;
81   switch (option_idx) {
82   case 0: {
83     m_class_name.assign(option_arg);
84   } break;
85   case 1: {
86       if (m_current_key.empty())
87         m_current_key.assign(option_arg);
88       else
89         error.SetErrorStringWithFormat("Key: \"%s\" missing value.",
90                                         m_current_key.c_str());
91 
92   } break;
93   case 2: {
94       if (!m_current_key.empty()) {
95           m_dict_sp->AddStringItem(m_current_key, option_arg);
96           m_current_key.clear();
97       }
98       else
99         error.SetErrorStringWithFormat("Value: \"%s\" missing matching key.",
100                                        option_arg.str().c_str());
101   } break;
102   default:
103     llvm_unreachable("Unimplemented option");
104   }
105   return error;
106 }
107 
108 void OptionGroupPythonClassWithDict::OptionParsingStarting(
109   ExecutionContext *execution_context) {
110   m_current_key.erase();
111   m_dict_sp = std::make_shared<StructuredData::Dictionary>();
112 }
113 
114 Status OptionGroupPythonClassWithDict::OptionParsingFinished(
115   ExecutionContext *execution_context) {
116   Status error;
117   // If we get here and there's contents in the m_current_key, somebody must
118   // have provided a key but no value.
119   if (!m_current_key.empty())
120       error.SetErrorStringWithFormat("Key: \"%s\" missing value.",
121                                      m_current_key.c_str());
122   return error;
123 }
124 
125