1 //===-- Options.cpp ---------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Interpreter/Options.h"
19 // Other libraries and framework includes
21 #include "lldb/Core/StreamString.h"
22 #include "lldb/Interpreter/CommandCompletions.h"
23 #include "lldb/Interpreter/CommandInterpreter.h"
24 #include "lldb/Interpreter/CommandObject.h"
25 #include "lldb/Interpreter/CommandReturnObject.h"
26 #include "lldb/Target/Target.h"
29 using namespace lldb_private;
31 //-------------------------------------------------------------------------
33 //-------------------------------------------------------------------------
34 Options::Options() : m_getopt_table() { BuildValidOptionSets(); }
36 Options::~Options() {}
38 void Options::NotifyOptionParsingStarting(ExecutionContext *execution_context) {
39 m_seen_options.clear();
40 // Let the subclass reset its option values
41 OptionParsingStarting(execution_context);
44 Error Options::NotifyOptionParsingFinished(
45 ExecutionContext *execution_context) {
46 return OptionParsingFinished(execution_context);
49 void Options::OptionSeen(int option_idx) { m_seen_options.insert(option_idx); }
51 // Returns true is set_a is a subset of set_b; Otherwise returns false.
53 bool Options::IsASubset(const OptionSet &set_a, const OptionSet &set_b) {
54 bool is_a_subset = true;
55 OptionSet::const_iterator pos_a;
56 OptionSet::const_iterator pos_b;
58 // set_a is a subset of set_b if every member of set_a is also a member of
61 for (pos_a = set_a.begin(); pos_a != set_a.end() && is_a_subset; ++pos_a) {
62 pos_b = set_b.find(*pos_a);
63 if (pos_b == set_b.end())
70 // Returns the set difference set_a - set_b, i.e. { x | ElementOf (x, set_a) &&
71 // !ElementOf (x, set_b) }
73 size_t Options::OptionsSetDiff(const OptionSet &set_a, const OptionSet &set_b,
76 OptionSet::const_iterator pos_a;
77 OptionSet::const_iterator pos_b;
79 for (pos_a = set_a.begin(); pos_a != set_a.end(); ++pos_a) {
80 pos_b = set_b.find(*pos_a);
81 if (pos_b == set_b.end()) {
90 // Returns the union of set_a and set_b. Does not put duplicate members into
93 void Options::OptionsSetUnion(const OptionSet &set_a, const OptionSet &set_b,
94 OptionSet &union_set) {
95 OptionSet::const_iterator pos;
96 OptionSet::iterator pos_union;
98 // Put all the elements of set_a into the union.
100 for (pos = set_a.begin(); pos != set_a.end(); ++pos)
101 union_set.insert(*pos);
103 // Put all the elements of set_b that are not already there into the union.
104 for (pos = set_b.begin(); pos != set_b.end(); ++pos) {
105 pos_union = union_set.find(*pos);
106 if (pos_union == union_set.end())
107 union_set.insert(*pos);
111 bool Options::VerifyOptions(CommandReturnObject &result) {
112 bool options_are_valid = false;
114 int num_levels = GetRequiredOptions().size();
116 for (int i = 0; i < num_levels && !options_are_valid; ++i) {
117 // This is the correct set of options if: 1). m_seen_options contains all
118 // of m_required_options[i]
119 // (i.e. all the required options at this level are a subset of
120 // m_seen_options); AND
121 // 2). { m_seen_options - m_required_options[i] is a subset of
122 // m_options_options[i] (i.e. all the rest of
123 // m_seen_options are in the set of optional options at this level.
125 // Check to see if all of m_required_options[i] are a subset of
127 if (IsASubset(GetRequiredOptions()[i], m_seen_options)) {
128 // Construct the set difference: remaining_options = {m_seen_options} -
129 // {m_required_options[i]}
130 OptionSet remaining_options;
131 OptionsSetDiff(m_seen_options, GetRequiredOptions()[i],
133 // Check to see if remaining_options is a subset of
134 // m_optional_options[i]
135 if (IsASubset(remaining_options, GetOptionalOptions()[i]))
136 options_are_valid = true;
140 options_are_valid = true;
143 if (options_are_valid) {
144 result.SetStatus(eReturnStatusSuccessFinishNoResult);
146 result.AppendError("invalid combination of options for the given command");
147 result.SetStatus(eReturnStatusFailed);
150 return options_are_valid;
153 // This is called in the Options constructor, though we could call it lazily if
154 // that ends up being
155 // a performance problem.
157 void Options::BuildValidOptionSets() {
158 // Check to see if we already did this.
159 if (m_required_options.size() != 0)
162 // Check to see if there are any options.
163 int num_options = NumCommandOptions();
164 if (num_options == 0)
167 auto opt_defs = GetDefinitions();
168 m_required_options.resize(1);
169 m_optional_options.resize(1);
171 // First count the number of option sets we've got. Ignore
172 // LLDB_ALL_OPTION_SETS...
174 uint32_t num_option_sets = 0;
176 for (const auto &def : opt_defs) {
177 uint32_t this_usage_mask = def.usage_mask;
178 if (this_usage_mask == LLDB_OPT_SET_ALL) {
179 if (num_option_sets == 0)
182 for (uint32_t j = 0; j < LLDB_MAX_NUM_OPTION_SETS; j++) {
183 if (this_usage_mask & (1 << j)) {
184 if (num_option_sets <= j)
185 num_option_sets = j + 1;
191 if (num_option_sets > 0) {
192 m_required_options.resize(num_option_sets);
193 m_optional_options.resize(num_option_sets);
195 for (const auto &def : opt_defs) {
196 for (uint32_t j = 0; j < num_option_sets; j++) {
197 if (def.usage_mask & 1 << j) {
199 m_required_options[j].insert(def.short_option);
201 m_optional_options[j].insert(def.short_option);
208 uint32_t Options::NumCommandOptions() { return GetDefinitions().size(); }
210 Option *Options::GetLongOptions() {
211 // Check to see if this has already been done.
212 if (m_getopt_table.empty()) {
213 auto defs = GetDefinitions();
217 std::map<int, uint32_t> option_seen;
219 m_getopt_table.resize(defs.size() + 1);
220 for (size_t i = 0; i < defs.size(); ++i) {
221 const int short_opt = defs[i].short_option;
223 m_getopt_table[i].definition = &defs[i];
224 m_getopt_table[i].flag = nullptr;
225 m_getopt_table[i].val = short_opt;
227 if (option_seen.find(short_opt) == option_seen.end()) {
228 option_seen[short_opt] = i;
229 } else if (short_opt) {
230 m_getopt_table[i].val = 0;
231 std::map<int, uint32_t>::const_iterator pos =
232 option_seen.find(short_opt);
234 if (isprint8(short_opt))
235 Host::SystemLog(Host::eSystemLogError,
236 "option[%u] --%s has a short option -%c that "
237 "conflicts with option[%u] --%s, short option won't "
238 "be used for --%s\n",
239 (int)i, defs[i].long_option, short_opt, pos->second,
240 m_getopt_table[pos->second].definition->long_option,
241 defs[i].long_option);
243 Host::SystemLog(Host::eSystemLogError,
244 "option[%u] --%s has a short option 0x%x that "
245 "conflicts with option[%u] --%s, short option won't "
246 "be used for --%s\n",
247 (int)i, defs[i].long_option, short_opt, pos->second,
248 m_getopt_table[pos->second].definition->long_option,
249 defs[i].long_option);
253 // getopt_long_only requires a NULL final entry in the table:
255 m_getopt_table.back().definition = nullptr;
256 m_getopt_table.back().flag = nullptr;
257 m_getopt_table.back().val = 0;
260 if (m_getopt_table.empty())
263 return &m_getopt_table.front();
266 // This function takes INDENT, which tells how many spaces to output at the
267 // front of each line; SPACES, which is
268 // a string containing 80 spaces; and TEXT, which is the text that is to be
269 // output. It outputs the text, on
270 // multiple lines if necessary, to RESULT, with INDENT spaces at the front of
271 // each line. It breaks lines on spaces,
272 // tabs or newlines, shortening the line if necessary to not break in the middle
273 // of a word. It assumes that each
274 // output line should contain a maximum of OUTPUT_MAX_COLUMNS characters.
276 void Options::OutputFormattedUsageText(Stream &strm,
277 const OptionDefinition &option_def,
278 uint32_t output_max_columns) {
279 std::string actual_text;
280 if (option_def.validator) {
281 const char *condition = option_def.validator->ShortConditionString();
284 actual_text.append(condition);
285 actual_text.append("] ");
288 actual_text.append(option_def.usage_text);
290 // Will it all fit on one line?
292 if (static_cast<uint32_t>(actual_text.length() + strm.GetIndentLevel()) <
293 output_max_columns) {
294 // Output it as a single line.
295 strm.Indent(actual_text.c_str());
298 // We need to break it up into multiple lines.
300 int text_width = output_max_columns - strm.GetIndentLevel() - 1;
303 int final_end = actual_text.length();
306 while (end < final_end) {
307 // Don't start the 'text' on a space, since we're already outputting the
309 while ((start < final_end) && (actual_text[start] == ' '))
312 end = start + text_width;
316 // If we're not at the end of the text, make sure we break the line on
318 while (end > start && actual_text[end] != ' ' &&
319 actual_text[end] != '\t' && actual_text[end] != '\n')
323 sub_len = end - start;
327 assert(start < final_end);
328 assert(start + sub_len <= final_end);
329 strm.Write(actual_text.c_str() + start, sub_len);
336 bool Options::SupportsLongOption(const char *long_option) {
337 if (!long_option || !long_option[0])
340 auto opt_defs = GetDefinitions();
341 if (opt_defs.empty())
344 const char *long_option_name = long_option;
345 if (long_option[0] == '-' && long_option[1] == '-')
346 long_option_name += 2;
348 for (auto &def : opt_defs) {
349 if (!def.long_option)
352 if (strcmp(def.long_option, long_option_name) == 0)
359 enum OptionDisplayType {
365 static bool PrintOption(const OptionDefinition &opt_def,
366 OptionDisplayType display_type, const char *header,
367 const char *footer, bool show_optional, Stream &strm) {
368 const bool has_short_option = isprint8(opt_def.short_option) != 0;
370 if (display_type == eDisplayShortOption && !has_short_option)
373 if (header && header[0])
374 strm.PutCString(header);
376 if (show_optional && !opt_def.required)
378 const bool show_short_option =
379 has_short_option && display_type != eDisplayLongOption;
380 if (show_short_option)
381 strm.Printf("-%c", opt_def.short_option);
383 strm.Printf("--%s", opt_def.long_option);
384 switch (opt_def.option_has_arg) {
385 case OptionParser::eNoArgument:
387 case OptionParser::eRequiredArgument:
388 strm.Printf(" <%s>", CommandObject::GetArgumentName(opt_def.argument_type));
391 case OptionParser::eOptionalArgument:
392 strm.Printf("%s[<%s>]", show_short_option ? "" : "=",
393 CommandObject::GetArgumentName(opt_def.argument_type));
396 if (show_optional && !opt_def.required)
398 if (footer && footer[0])
399 strm.PutCString(footer);
403 void Options::GenerateOptionUsage(Stream &strm, CommandObject *cmd,
404 uint32_t screen_width) {
405 const bool only_print_args = cmd->IsDashDashCommand();
407 auto opt_defs = GetDefinitions();
408 const uint32_t save_indent_level = strm.GetIndentLevel();
409 llvm::StringRef name;
411 StreamString arguments_str;
414 name = cmd->GetCommandName();
415 cmd->GetFormattedCommandArguments(arguments_str);
419 strm.PutCString("\nCommand Options Usage:\n");
423 // First, show each usage level set of options, e.g. <cmd>
424 // [options-for-level-0]
426 // [options-for-level-1]
429 const uint32_t num_options = NumCommandOptions();
430 if (num_options == 0)
433 uint32_t num_option_sets = GetRequiredOptions().size();
437 if (!only_print_args) {
438 for (uint32_t opt_set = 0; opt_set < num_option_sets; ++opt_set) {
439 uint32_t opt_set_mask;
441 opt_set_mask = 1 << opt_set;
446 // Different option sets may require different args.
447 StreamString args_str;
449 cmd->GetFormattedCommandArguments(args_str, opt_set_mask);
451 // First go through and print all options that take no arguments as
452 // a single string. If a command has "-a" "-b" and "-c", this will show
455 std::set<int> options;
456 std::set<int>::const_iterator options_pos, options_end;
457 for (auto &def : opt_defs) {
458 if (def.usage_mask & opt_set_mask && isprint8(def.short_option)) {
459 // Add current option to the end of out_stream.
461 if (def.required && def.option_has_arg == OptionParser::eNoArgument) {
462 options.insert(def.short_option);
467 if (options.empty() == false) {
468 // We have some required options with no arguments
469 strm.PutCString(" -");
470 for (i = 0; i < 2; ++i)
471 for (options_pos = options.begin(), options_end = options.end();
472 options_pos != options_end; ++options_pos) {
473 if (i == 0 && ::islower(*options_pos))
475 if (i == 1 && ::isupper(*options_pos))
477 strm << (char)*options_pos;
482 for (auto &def : opt_defs) {
483 if (def.usage_mask & opt_set_mask && isprint8(def.short_option)) {
484 // Add current option to the end of out_stream.
486 if (def.required == false &&
487 def.option_has_arg == OptionParser::eNoArgument) {
488 options.insert(def.short_option);
493 if (options.empty() == false) {
494 // We have some required options with no arguments
495 strm.PutCString(" [-");
496 for (i = 0; i < 2; ++i)
497 for (options_pos = options.begin(), options_end = options.end();
498 options_pos != options_end; ++options_pos) {
499 if (i == 0 && ::islower(*options_pos))
501 if (i == 1 && ::isupper(*options_pos))
503 strm << (char)*options_pos;
508 // First go through and print the required options (list them up front).
510 for (auto &def : opt_defs) {
511 if (def.usage_mask & opt_set_mask && isprint8(def.short_option)) {
512 if (def.required && def.option_has_arg != OptionParser::eNoArgument)
513 PrintOption(def, eDisplayBestOption, " ", nullptr, true, strm);
517 // Now go through again, and this time only print the optional options.
519 for (auto &def : opt_defs) {
520 if (def.usage_mask & opt_set_mask) {
521 // Add current option to the end of out_stream.
523 if (!def.required && def.option_has_arg != OptionParser::eNoArgument)
524 PrintOption(def, eDisplayBestOption, " ", nullptr, true, strm);
528 if (args_str.GetSize() > 0) {
529 if (cmd->WantsRawCommandString() && !only_print_args)
532 strm << " " << args_str.GetString();
539 if (cmd && (only_print_args || cmd->WantsRawCommandString()) &&
540 arguments_str.GetSize() > 0) {
541 if (!only_print_args)
544 strm << " " << arguments_str.GetString();
549 if (!only_print_args) {
550 // Now print out all the detailed information about the various options:
551 // long form, short form and help text:
552 // -short <argument> ( --long_name <argument> )
555 // This variable is used to keep track of which options' info we've printed
556 // out, because some options can be in
557 // more than one usage level, but we only want to print the long form of its
560 std::multimap<int, uint32_t> options_seen;
563 // Put the unique command options in a vector & sort it, so we can output
564 // them alphabetically (by short_option)
565 // when writing out detailed help for each option.
568 for (auto &def : opt_defs)
569 options_seen.insert(std::make_pair(def.short_option, i++));
571 // Go through the unique'd and alphabetically sorted vector of options, find
572 // the table entry for each option
573 // and write out the detailed help information for that option.
575 bool first_option_printed = false;
577 for (auto pos : options_seen) {
579 // Print out the help information for this option.
581 // Put a newline separation between arguments
582 if (first_option_printed)
585 first_option_printed = true;
587 CommandArgumentType arg_type = opt_defs[i].argument_type;
589 StreamString arg_name_str;
590 arg_name_str.Printf("<%s>", CommandObject::GetArgumentName(arg_type));
593 if (opt_defs[i].short_option && isprint8(opt_defs[i].short_option)) {
594 PrintOption(opt_defs[i], eDisplayShortOption, nullptr, nullptr, false,
596 PrintOption(opt_defs[i], eDisplayLongOption, " ( ", " )", false, strm);
598 // Short option is not printable, just print long option
599 PrintOption(opt_defs[i], eDisplayLongOption, nullptr, nullptr, false,
606 if (opt_defs[i].usage_text)
607 OutputFormattedUsageText(strm, opt_defs[i], screen_width);
608 if (opt_defs[i].enum_values != nullptr) {
610 strm.Printf("Values: ");
611 for (int k = 0; opt_defs[i].enum_values[k].string_value != nullptr;
614 strm.Printf("%s", opt_defs[i].enum_values[k].string_value);
616 strm.Printf(" | %s", opt_defs[i].enum_values[k].string_value);
624 // Restore the indent level
625 strm.SetIndentLevel(save_indent_level);
628 // This function is called when we have been given a potentially incomplete set
630 // options, such as when an alias has been defined (more options might be added
632 // at the time the alias is invoked). We need to verify that the options in the
634 // m_seen_options are all part of a set that may be used together, but
636 // may be missing some of the "required" options.
638 bool Options::VerifyPartialOptions(CommandReturnObject &result) {
639 bool options_are_valid = false;
641 int num_levels = GetRequiredOptions().size();
643 for (int i = 0; i < num_levels && !options_are_valid; ++i) {
644 // In this case we are treating all options as optional rather than
646 // Therefore a set of options is correct if m_seen_options is a subset of
648 // union of m_required_options and m_optional_options.
650 OptionsSetUnion(GetRequiredOptions()[i], GetOptionalOptions()[i],
652 if (IsASubset(m_seen_options, union_set))
653 options_are_valid = true;
657 return options_are_valid;
660 bool Options::HandleOptionCompletion(
661 Args &input, OptionElementVector &opt_element_vector, int cursor_index,
662 int char_pos, int match_start_point, int max_return_elements,
663 CommandInterpreter &interpreter, bool &word_complete,
664 lldb_private::StringList &matches) {
665 word_complete = true;
667 // For now we just scan the completions to see if the cursor position is in
668 // an option or its argument. Otherwise we'll call HandleArgumentCompletion.
669 // In the future we can use completion to validate options as well if we want.
671 auto opt_defs = GetDefinitions();
673 std::string cur_opt_std_str(input.GetArgumentAtIndex(cursor_index));
674 cur_opt_std_str.erase(char_pos);
675 const char *cur_opt_str = cur_opt_std_str.c_str();
677 for (size_t i = 0; i < opt_element_vector.size(); i++) {
678 int opt_pos = opt_element_vector[i].opt_pos;
679 int opt_arg_pos = opt_element_vector[i].opt_arg_pos;
680 int opt_defs_index = opt_element_vector[i].opt_defs_index;
681 if (opt_pos == cursor_index) {
682 // We're completing the option itself.
684 if (opt_defs_index == OptionArgElement::eBareDash) {
685 // We're completing a bare dash. That means all options are open.
686 // FIXME: We should scan the other options provided and only complete
688 // within the option group they belong to.
689 char opt_str[3] = {'-', 'a', '\0'};
691 for (auto &def : opt_defs) {
692 if (!def.short_option)
694 opt_str[1] = def.short_option;
695 matches.AppendString(opt_str);
699 } else if (opt_defs_index == OptionArgElement::eBareDoubleDash) {
700 std::string full_name("--");
701 for (auto &def : opt_defs) {
702 if (!def.short_option)
705 full_name.erase(full_name.begin() + 2, full_name.end());
706 full_name.append(def.long_option);
707 matches.AppendString(full_name.c_str());
710 } else if (opt_defs_index != OptionArgElement::eUnrecognizedArg) {
711 // We recognized it, if it an incomplete long option, complete it anyway
712 // (getopt_long_only is
713 // happy with shortest unique string, but it's still a nice thing to
714 // do.) Otherwise return
715 // The string so the upper level code will know this is a full match and
717 if (cur_opt_str && strlen(cur_opt_str) > 2 && cur_opt_str[0] == '-' &&
718 cur_opt_str[1] == '-' &&
719 strcmp(opt_defs[opt_defs_index].long_option, cur_opt_str) != 0) {
720 std::string full_name("--");
721 full_name.append(opt_defs[opt_defs_index].long_option);
722 matches.AppendString(full_name.c_str());
725 matches.AppendString(input.GetArgumentAtIndex(cursor_index));
729 // FIXME - not handling wrong options yet:
730 // Check to see if they are writing a long option & complete it.
731 // I think we will only get in here if the long option table has two
733 // that are not unique up to this point. getopt_long_only does shortest
735 // for long options already.
737 if (cur_opt_str && strlen(cur_opt_str) > 2 && cur_opt_str[0] == '-' &&
738 cur_opt_str[1] == '-') {
739 for (auto &def : opt_defs) {
740 if (!def.long_option)
743 if (strstr(def.long_option, cur_opt_str + 2) == def.long_option) {
744 std::string full_name("--");
745 full_name.append(def.long_option);
746 // The options definitions table has duplicates because of the
747 // way the grouping information is stored, so only add once.
748 bool duplicate = false;
749 for (size_t k = 0; k < matches.GetSize(); k++) {
750 if (matches.GetStringAtIndex(k) == full_name) {
756 matches.AppendString(full_name.c_str());
763 } else if (opt_arg_pos == cursor_index) {
764 // Okay the cursor is on the completion of an argument.
765 // See if it has a completion, otherwise return no matches.
767 if (opt_defs_index != -1) {
768 HandleOptionArgumentCompletion(
769 input, cursor_index, strlen(input.GetArgumentAtIndex(cursor_index)),
770 opt_element_vector, i, match_start_point, max_return_elements,
771 interpreter, word_complete, matches);
774 // No completion callback means no completions...
779 // Not the last element, keep going.
786 bool Options::HandleOptionArgumentCompletion(
787 Args &input, int cursor_index, int char_pos,
788 OptionElementVector &opt_element_vector, int opt_element_index,
789 int match_start_point, int max_return_elements,
790 CommandInterpreter &interpreter, bool &word_complete,
791 lldb_private::StringList &matches) {
792 auto opt_defs = GetDefinitions();
793 std::unique_ptr<SearchFilter> filter_ap;
795 int opt_arg_pos = opt_element_vector[opt_element_index].opt_arg_pos;
796 int opt_defs_index = opt_element_vector[opt_element_index].opt_defs_index;
798 // See if this is an enumeration type option, and if so complete it here:
800 OptionEnumValueElement *enum_values = opt_defs[opt_defs_index].enum_values;
801 if (enum_values != nullptr) {
802 bool return_value = false;
803 std::string match_string(input.GetArgumentAtIndex(opt_arg_pos),
804 input.GetArgumentAtIndex(opt_arg_pos) + char_pos);
805 for (int i = 0; enum_values[i].string_value != nullptr; i++) {
806 if (strstr(enum_values[i].string_value, match_string.c_str()) ==
807 enum_values[i].string_value) {
808 matches.AppendString(enum_values[i].string_value);
815 // If this is a source file or symbol type completion, and there is a
816 // -shlib option somewhere in the supplied arguments, then make a search
818 // for that shared library.
819 // FIXME: Do we want to also have an "OptionType" so we don't have to match
822 uint32_t completion_mask = opt_defs[opt_defs_index].completion_type;
824 if (completion_mask == 0) {
825 lldb::CommandArgumentType option_arg_type =
826 opt_defs[opt_defs_index].argument_type;
827 if (option_arg_type != eArgTypeNone) {
828 const CommandObject::ArgumentTableEntry *arg_entry =
829 CommandObject::FindArgumentDataByType(
830 opt_defs[opt_defs_index].argument_type);
832 completion_mask = arg_entry->completion_type;
836 if (completion_mask & CommandCompletions::eSourceFileCompletion ||
837 completion_mask & CommandCompletions::eSymbolCompletion) {
838 for (size_t i = 0; i < opt_element_vector.size(); i++) {
839 int cur_defs_index = opt_element_vector[i].opt_defs_index;
841 // trying to use <0 indices will definitely cause problems
842 if (cur_defs_index == OptionArgElement::eUnrecognizedArg ||
843 cur_defs_index == OptionArgElement::eBareDash ||
844 cur_defs_index == OptionArgElement::eBareDoubleDash)
847 int cur_arg_pos = opt_element_vector[i].opt_arg_pos;
848 const char *cur_opt_name = opt_defs[cur_defs_index].long_option;
850 // If this is the "shlib" option and there was an argument provided,
851 // restrict it to that shared library.
852 if (cur_opt_name && strcmp(cur_opt_name, "shlib") == 0 &&
854 const char *module_name = input.GetArgumentAtIndex(cur_arg_pos);
856 FileSpec module_spec(module_name, false);
857 lldb::TargetSP target_sp =
858 interpreter.GetDebugger().GetSelectedTarget();
859 // Search filters require a target...
861 filter_ap.reset(new SearchFilterByModule(target_sp, module_spec));
868 return CommandCompletions::InvokeCommonCompletionCallbacks(
869 interpreter, completion_mask, input.GetArgumentAtIndex(opt_arg_pos),
870 match_start_point, max_return_elements, filter_ap.get(), word_complete,
874 void OptionGroupOptions::Append(OptionGroup *group) {
875 auto group_option_defs = group->GetDefinitions();
876 for (uint32_t i = 0; i < group_option_defs.size(); ++i) {
877 m_option_infos.push_back(OptionInfo(group, i));
878 m_option_defs.push_back(group_option_defs[i]);
882 const OptionGroup *OptionGroupOptions::GetGroupWithOption(char short_opt) {
883 for (uint32_t i = 0; i < m_option_defs.size(); i++) {
884 OptionDefinition opt_def = m_option_defs[i];
885 if (opt_def.short_option == short_opt)
886 return m_option_infos[i].option_group;
891 void OptionGroupOptions::Append(OptionGroup *group, uint32_t src_mask,
893 auto group_option_defs = group->GetDefinitions();
894 for (uint32_t i = 0; i < group_option_defs.size(); ++i) {
895 if (group_option_defs[i].usage_mask & src_mask) {
896 m_option_infos.push_back(OptionInfo(group, i));
897 m_option_defs.push_back(group_option_defs[i]);
898 m_option_defs.back().usage_mask = dst_mask;
903 void OptionGroupOptions::Finalize() {
904 m_did_finalize = true;
907 Error OptionGroupOptions::SetOptionValue(uint32_t option_idx,
908 llvm::StringRef option_value,
909 ExecutionContext *execution_context) {
910 // After calling OptionGroupOptions::Append(...), you must finalize the groups
911 // by calling OptionGroupOptions::Finlize()
912 assert(m_did_finalize);
914 if (option_idx < m_option_infos.size()) {
915 error = m_option_infos[option_idx].option_group->SetOptionValue(
916 m_option_infos[option_idx].option_index, option_value,
920 error.SetErrorString("invalid option index"); // Shouldn't happen...
925 void OptionGroupOptions::OptionParsingStarting(
926 ExecutionContext *execution_context) {
927 std::set<OptionGroup *> group_set;
928 OptionInfos::iterator pos, end = m_option_infos.end();
929 for (pos = m_option_infos.begin(); pos != end; ++pos) {
930 OptionGroup *group = pos->option_group;
931 if (group_set.find(group) == group_set.end()) {
932 group->OptionParsingStarting(execution_context);
933 group_set.insert(group);
937 Error OptionGroupOptions::OptionParsingFinished(
938 ExecutionContext *execution_context) {
939 std::set<OptionGroup *> group_set;
941 OptionInfos::iterator pos, end = m_option_infos.end();
942 for (pos = m_option_infos.begin(); pos != end; ++pos) {
943 OptionGroup *group = pos->option_group;
944 if (group_set.find(group) == group_set.end()) {
945 error = group->OptionParsingFinished(execution_context);
946 group_set.insert(group);