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/lldb-python.h"
12 #include "lldb/Interpreter/Options.h"
20 // Other libraries and framework includes
22 #include "lldb/Interpreter/CommandObject.h"
23 #include "lldb/Interpreter/CommandReturnObject.h"
24 #include "lldb/Interpreter/CommandCompletions.h"
25 #include "lldb/Interpreter/CommandInterpreter.h"
26 #include "lldb/Core/StreamString.h"
27 #include "lldb/Target/Target.h"
30 using namespace lldb_private;
32 //-------------------------------------------------------------------------
34 //-------------------------------------------------------------------------
35 Options::Options (CommandInterpreter &interpreter) :
36 m_interpreter (interpreter),
39 BuildValidOptionSets();
47 Options::NotifyOptionParsingStarting ()
49 m_seen_options.clear();
50 // Let the subclass reset its option values
51 OptionParsingStarting ();
55 Options::NotifyOptionParsingFinished ()
57 return OptionParsingFinished ();
61 Options::OptionSeen (int option_idx)
63 m_seen_options.insert (option_idx);
66 // Returns true is set_a is a subset of set_b; Otherwise returns false.
69 Options::IsASubset (const OptionSet& set_a, const OptionSet& set_b)
71 bool is_a_subset = true;
72 OptionSet::const_iterator pos_a;
73 OptionSet::const_iterator pos_b;
75 // set_a is a subset of set_b if every member of set_a is also a member of set_b
77 for (pos_a = set_a.begin(); pos_a != set_a.end() && is_a_subset; ++pos_a)
79 pos_b = set_b.find(*pos_a);
80 if (pos_b == set_b.end())
87 // Returns the set difference set_a - set_b, i.e. { x | ElementOf (x, set_a) && !ElementOf (x, set_b) }
90 Options::OptionsSetDiff (const OptionSet& set_a, const OptionSet& set_b, OptionSet& diffs)
93 OptionSet::const_iterator pos_a;
94 OptionSet::const_iterator pos_b;
96 for (pos_a = set_a.begin(); pos_a != set_a.end(); ++pos_a)
98 pos_b = set_b.find(*pos_a);
99 if (pos_b == set_b.end())
102 diffs.insert(*pos_a);
109 // Returns the union of set_a and set_b. Does not put duplicate members into the union.
112 Options::OptionsSetUnion (const OptionSet &set_a, const OptionSet &set_b, OptionSet &union_set)
114 OptionSet::const_iterator pos;
115 OptionSet::iterator pos_union;
117 // Put all the elements of set_a into the union.
119 for (pos = set_a.begin(); pos != set_a.end(); ++pos)
120 union_set.insert(*pos);
122 // Put all the elements of set_b that are not already there into the union.
123 for (pos = set_b.begin(); pos != set_b.end(); ++pos)
125 pos_union = union_set.find(*pos);
126 if (pos_union == union_set.end())
127 union_set.insert(*pos);
132 Options::VerifyOptions (CommandReturnObject &result)
134 bool options_are_valid = false;
136 int num_levels = GetRequiredOptions().size();
139 for (int i = 0; i < num_levels && !options_are_valid; ++i)
141 // This is the correct set of options if: 1). m_seen_options contains all of m_required_options[i]
142 // (i.e. all the required options at this level are a subset of m_seen_options); AND
143 // 2). { m_seen_options - m_required_options[i] is a subset of m_options_options[i] (i.e. all the rest of
144 // m_seen_options are in the set of optional options at this level.
146 // Check to see if all of m_required_options[i] are a subset of m_seen_options
147 if (IsASubset (GetRequiredOptions()[i], m_seen_options))
149 // Construct the set difference: remaining_options = {m_seen_options} - {m_required_options[i]}
150 OptionSet remaining_options;
151 OptionsSetDiff (m_seen_options, GetRequiredOptions()[i], remaining_options);
152 // Check to see if remaining_options is a subset of m_optional_options[i]
153 if (IsASubset (remaining_options, GetOptionalOptions()[i]))
154 options_are_valid = true;
160 options_are_valid = true;
163 if (options_are_valid)
165 result.SetStatus (eReturnStatusSuccessFinishNoResult);
169 result.AppendError ("invalid combination of options for the given command");
170 result.SetStatus (eReturnStatusFailed);
173 return options_are_valid;
176 // This is called in the Options constructor, though we could call it lazily if that ends up being
177 // a performance problem.
180 Options::BuildValidOptionSets ()
182 // Check to see if we already did this.
183 if (m_required_options.size() != 0)
186 // Check to see if there are any options.
187 int num_options = NumCommandOptions ();
188 if (num_options == 0)
191 const OptionDefinition *opt_defs = GetDefinitions();
192 m_required_options.resize(1);
193 m_optional_options.resize(1);
195 // First count the number of option sets we've got. Ignore LLDB_ALL_OPTION_SETS...
197 uint32_t num_option_sets = 0;
199 for (int i = 0; i < num_options; i++)
201 uint32_t this_usage_mask = opt_defs[i].usage_mask;
202 if (this_usage_mask == LLDB_OPT_SET_ALL)
204 if (num_option_sets == 0)
209 for (uint32_t j = 0; j < LLDB_MAX_NUM_OPTION_SETS; j++)
211 if (this_usage_mask & (1 << j))
213 if (num_option_sets <= j)
214 num_option_sets = j + 1;
220 if (num_option_sets > 0)
222 m_required_options.resize(num_option_sets);
223 m_optional_options.resize(num_option_sets);
225 for (int i = 0; i < num_options; ++i)
227 for (uint32_t j = 0; j < num_option_sets; j++)
229 if (opt_defs[i].usage_mask & 1 << j)
231 if (opt_defs[i].required)
232 m_required_options[j].insert(opt_defs[i].short_option);
234 m_optional_options[j].insert(opt_defs[i].short_option);
242 Options::NumCommandOptions ()
244 const OptionDefinition *opt_defs = GetDefinitions ();
245 if (opt_defs == nullptr)
250 if (opt_defs != nullptr)
252 while (opt_defs[i].long_option != nullptr)
260 Options::GetLongOptions ()
262 // Check to see if this has already been done.
263 if (m_getopt_table.empty())
265 // Check to see if there are any options.
266 const uint32_t num_options = NumCommandOptions();
267 if (num_options == 0)
271 const OptionDefinition *opt_defs = GetDefinitions();
273 std::map<int, uint32_t> option_seen;
275 m_getopt_table.resize(num_options + 1);
276 for (i = 0; i < num_options; ++i)
278 const int short_opt = opt_defs[i].short_option;
280 m_getopt_table[i].definition = &opt_defs[i];
281 m_getopt_table[i].flag = nullptr;
282 m_getopt_table[i].val = short_opt;
284 if (option_seen.find(short_opt) == option_seen.end())
286 option_seen[short_opt] = i;
290 m_getopt_table[i].val = 0;
291 std::map<int, uint32_t>::const_iterator pos = option_seen.find(short_opt);
293 if (isprint8(short_opt))
294 Host::SystemLog (Host::eSystemLogError, "option[%u] --%s has a short option -%c that conflicts with option[%u] --%s, short option won't be used for --%s\n",
296 opt_defs[i].long_option,
299 m_getopt_table[pos->second].definition->long_option,
300 opt_defs[i].long_option);
302 Host::SystemLog (Host::eSystemLogError, "option[%u] --%s has a short option 0x%x that conflicts with option[%u] --%s, short option won't be used for --%s\n",
304 opt_defs[i].long_option,
307 m_getopt_table[pos->second].definition->long_option,
308 opt_defs[i].long_option);
312 //getopt_long_only requires a NULL final entry in the table:
314 m_getopt_table[i].definition = nullptr;
315 m_getopt_table[i].flag = nullptr;
316 m_getopt_table[i].val = 0;
319 if (m_getopt_table.empty())
322 return &m_getopt_table.front();
326 // This function takes INDENT, which tells how many spaces to output at the front of each line; SPACES, which is
327 // a string containing 80 spaces; and TEXT, which is the text that is to be output. It outputs the text, on
328 // multiple lines if necessary, to RESULT, with INDENT spaces at the front of each line. It breaks lines on spaces,
329 // tabs or newlines, shortening the line if necessary to not break in the middle of a word. It assumes that each
330 // output line should contain a maximum of OUTPUT_MAX_COLUMNS characters.
334 Options::OutputFormattedUsageText
337 const OptionDefinition &option_def,
338 uint32_t output_max_columns
341 std::string actual_text;
342 if (option_def.validator)
344 const char *condition = option_def.validator->ShortConditionString();
348 actual_text.append(condition);
349 actual_text.append("] ");
352 actual_text.append(option_def.usage_text);
354 // Will it all fit on one line?
356 if (static_cast<uint32_t>(actual_text.length() + strm.GetIndentLevel()) < output_max_columns)
358 // Output it as a single line.
359 strm.Indent (actual_text.c_str());
364 // We need to break it up into multiple lines.
366 int text_width = output_max_columns - strm.GetIndentLevel() - 1;
369 int final_end = actual_text.length();
372 while (end < final_end)
374 // Don't start the 'text' on a space, since we're already outputting the indentation.
375 while ((start < final_end) && (actual_text[start] == ' '))
378 end = start + text_width;
383 // If we're not at the end of the text, make sure we break the line on white space.
385 && actual_text[end] != ' ' && actual_text[end] != '\t' && actual_text[end] != '\n')
389 sub_len = end - start;
393 assert (start < final_end);
394 assert (start + sub_len <= final_end);
395 strm.Write(actual_text.c_str() + start, sub_len);
403 Options::SupportsLongOption (const char *long_option)
405 if (long_option && long_option[0])
407 const OptionDefinition *opt_defs = GetDefinitions ();
410 const char *long_option_name = long_option;
411 if (long_option[0] == '-' && long_option[1] == '-')
412 long_option_name += 2;
414 for (uint32_t i = 0; opt_defs[i].long_option; ++i)
416 if (strcmp(opt_defs[i].long_option, long_option_name) == 0)
424 enum OptionDisplayType
432 PrintOption (const OptionDefinition &opt_def,
433 OptionDisplayType display_type,
439 const bool has_short_option = isprint8(opt_def.short_option) != 0;
441 if (display_type == eDisplayShortOption && !has_short_option)
444 if (header && header[0])
445 strm.PutCString(header);
447 if (show_optional && !opt_def.required)
449 const bool show_short_option = has_short_option && display_type != eDisplayLongOption;
450 if (show_short_option)
451 strm.Printf ("-%c", opt_def.short_option);
453 strm.Printf ("--%s", opt_def.long_option);
454 switch (opt_def.option_has_arg)
456 case OptionParser::eNoArgument:
458 case OptionParser::eRequiredArgument:
459 strm.Printf (" <%s>", CommandObject::GetArgumentName (opt_def.argument_type));
462 case OptionParser::eOptionalArgument:
463 strm.Printf ("%s[<%s>]",
464 show_short_option ? "" : "=",
465 CommandObject::GetArgumentName (opt_def.argument_type));
468 if (show_optional && !opt_def.required)
470 if (footer && footer[0])
471 strm.PutCString(footer);
476 Options::GenerateOptionUsage
482 const uint32_t screen_width = m_interpreter.GetDebugger().GetTerminalWidth();
484 const OptionDefinition *opt_defs = GetDefinitions();
485 const uint32_t save_indent_level = strm.GetIndentLevel();
488 StreamString arguments_str;
492 name = cmd->GetCommandName();
493 cmd->GetFormattedCommandArguments (arguments_str);
498 strm.PutCString ("\nCommand Options Usage:\n");
502 // First, show each usage level set of options, e.g. <cmd> [options-for-level-0]
503 // <cmd> [options-for-level-1]
506 const uint32_t num_options = NumCommandOptions();
507 if (num_options == 0)
510 uint32_t num_option_sets = GetRequiredOptions().size();
514 for (uint32_t opt_set = 0; opt_set < num_option_sets; ++opt_set)
516 uint32_t opt_set_mask;
518 opt_set_mask = 1 << opt_set;
523 // Different option sets may require different args.
524 StreamString args_str;
526 cmd->GetFormattedCommandArguments(args_str, opt_set_mask);
528 // First go through and print all options that take no arguments as
529 // a single string. If a command has "-a" "-b" and "-c", this will show
532 std::set<int> options;
533 std::set<int>::const_iterator options_pos, options_end;
534 for (i = 0; i < num_options; ++i)
536 if (opt_defs[i].usage_mask & opt_set_mask && isprint8(opt_defs[i].short_option))
538 // Add current option to the end of out_stream.
540 if (opt_defs[i].required == true &&
541 opt_defs[i].option_has_arg == OptionParser::eNoArgument)
543 options.insert (opt_defs[i].short_option);
548 if (options.empty() == false)
550 // We have some required options with no arguments
551 strm.PutCString(" -");
553 for (options_pos = options.begin(), options_end = options.end();
554 options_pos != options_end;
557 if (i==0 && ::islower (*options_pos))
559 if (i==1 && ::isupper (*options_pos))
561 strm << (char)*options_pos;
565 for (i = 0, options.clear(); i < num_options; ++i)
567 if (opt_defs[i].usage_mask & opt_set_mask && isprint8(opt_defs[i].short_option))
569 // Add current option to the end of out_stream.
571 if (opt_defs[i].required == false &&
572 opt_defs[i].option_has_arg == OptionParser::eNoArgument)
574 options.insert (opt_defs[i].short_option);
579 if (options.empty() == false)
581 // We have some required options with no arguments
582 strm.PutCString(" [-");
584 for (options_pos = options.begin(), options_end = options.end();
585 options_pos != options_end;
588 if (i==0 && ::islower (*options_pos))
590 if (i==1 && ::isupper (*options_pos))
592 strm << (char)*options_pos;
597 // First go through and print the required options (list them up front).
599 for (i = 0; i < num_options; ++i)
601 if (opt_defs[i].usage_mask & opt_set_mask && isprint8(opt_defs[i].short_option))
603 if (opt_defs[i].required && opt_defs[i].option_has_arg != OptionParser::eNoArgument)
604 PrintOption (opt_defs[i], eDisplayBestOption, " ", nullptr, true, strm);
608 // Now go through again, and this time only print the optional options.
610 for (i = 0; i < num_options; ++i)
612 if (opt_defs[i].usage_mask & opt_set_mask)
614 // Add current option to the end of out_stream.
616 if (!opt_defs[i].required && opt_defs[i].option_has_arg != OptionParser::eNoArgument)
617 PrintOption (opt_defs[i], eDisplayBestOption, " ", nullptr, true, strm);
621 if (args_str.GetSize() > 0)
623 if (cmd->WantsRawCommandString())
626 strm.Printf (" %s", args_str.GetData());
631 cmd->WantsRawCommandString() &&
632 arguments_str.GetSize() > 0)
636 strm.Printf(" %s", arguments_str.GetData());
639 strm.Printf ("\n\n");
641 // Now print out all the detailed information about the various options: long form, short form and help text:
642 // -short <argument> ( --long_name <argument> )
645 // This variable is used to keep track of which options' info we've printed out, because some options can be in
646 // more than one usage level, but we only want to print the long form of its information once.
648 std::multimap<int, uint32_t> options_seen;
651 // Put the unique command options in a vector & sort it, so we can output them alphabetically (by short_option)
652 // when writing out detailed help for each option.
654 for (i = 0; i < num_options; ++i)
655 options_seen.insert(std::make_pair(opt_defs[i].short_option, i));
657 // Go through the unique'd and alphabetically sorted vector of options, find the table entry for each option
658 // and write out the detailed help information for that option.
660 bool first_option_printed = false;;
662 for (auto pos : options_seen)
665 //Print out the help information for this option.
667 // Put a newline separation between arguments
668 if (first_option_printed)
671 first_option_printed = true;
673 CommandArgumentType arg_type = opt_defs[i].argument_type;
675 StreamString arg_name_str;
676 arg_name_str.Printf ("<%s>", CommandObject::GetArgumentName (arg_type));
679 if (opt_defs[i].short_option && isprint8(opt_defs[i].short_option))
681 PrintOption (opt_defs[i], eDisplayShortOption, nullptr, nullptr, false, strm);
682 PrintOption (opt_defs[i], eDisplayLongOption, " ( ", " )", false, strm);
686 // Short option is not printable, just print long option
687 PrintOption (opt_defs[i], eDisplayLongOption, nullptr, nullptr, false, strm);
693 if (opt_defs[i].usage_text)
694 OutputFormattedUsageText (strm,
697 if (opt_defs[i].enum_values != nullptr)
700 strm.Printf("Values: ");
701 for (int k = 0; opt_defs[i].enum_values[k].string_value != nullptr; k++)
704 strm.Printf("%s", opt_defs[i].enum_values[k].string_value);
706 strm.Printf(" | %s", opt_defs[i].enum_values[k].string_value);
713 // Restore the indent level
714 strm.SetIndentLevel (save_indent_level);
717 // This function is called when we have been given a potentially incomplete set of
718 // options, such as when an alias has been defined (more options might be added at
719 // at the time the alias is invoked). We need to verify that the options in the set
720 // m_seen_options are all part of a set that may be used together, but m_seen_options
721 // may be missing some of the "required" options.
724 Options::VerifyPartialOptions (CommandReturnObject &result)
726 bool options_are_valid = false;
728 int num_levels = GetRequiredOptions().size();
731 for (int i = 0; i < num_levels && !options_are_valid; ++i)
733 // In this case we are treating all options as optional rather than required.
734 // Therefore a set of options is correct if m_seen_options is a subset of the
735 // union of m_required_options and m_optional_options.
737 OptionsSetUnion (GetRequiredOptions()[i], GetOptionalOptions()[i], union_set);
738 if (IsASubset (m_seen_options, union_set))
739 options_are_valid = true;
743 return options_are_valid;
747 Options::HandleOptionCompletion
750 OptionElementVector &opt_element_vector,
753 int match_start_point,
754 int max_return_elements,
756 lldb_private::StringList &matches
759 word_complete = true;
761 // For now we just scan the completions to see if the cursor position is in
762 // an option or its argument. Otherwise we'll call HandleArgumentCompletion.
763 // In the future we can use completion to validate options as well if we want.
765 const OptionDefinition *opt_defs = GetDefinitions();
767 std::string cur_opt_std_str (input.GetArgumentAtIndex(cursor_index));
768 cur_opt_std_str.erase(char_pos);
769 const char *cur_opt_str = cur_opt_std_str.c_str();
771 for (size_t i = 0; i < opt_element_vector.size(); i++)
773 int opt_pos = opt_element_vector[i].opt_pos;
774 int opt_arg_pos = opt_element_vector[i].opt_arg_pos;
775 int opt_defs_index = opt_element_vector[i].opt_defs_index;
776 if (opt_pos == cursor_index)
778 // We're completing the option itself.
780 if (opt_defs_index == OptionArgElement::eBareDash)
782 // We're completing a bare dash. That means all options are open.
783 // FIXME: We should scan the other options provided and only complete options
784 // within the option group they belong to.
785 char opt_str[3] = {'-', 'a', '\0'};
787 for (int j = 0 ; opt_defs[j].short_option != 0 ; j++)
789 opt_str[1] = opt_defs[j].short_option;
790 matches.AppendString (opt_str);
794 else if (opt_defs_index == OptionArgElement::eBareDoubleDash)
796 std::string full_name ("--");
797 for (int j = 0 ; opt_defs[j].short_option != 0 ; j++)
799 full_name.erase(full_name.begin() + 2, full_name.end());
800 full_name.append (opt_defs[j].long_option);
801 matches.AppendString (full_name.c_str());
805 else if (opt_defs_index != OptionArgElement::eUnrecognizedArg)
807 // We recognized it, if it an incomplete long option, complete it anyway (getopt_long_only is
808 // happy with shortest unique string, but it's still a nice thing to do.) Otherwise return
809 // The string so the upper level code will know this is a full match and add the " ".
810 if (cur_opt_str && strlen (cur_opt_str) > 2
811 && cur_opt_str[0] == '-' && cur_opt_str[1] == '-'
812 && strcmp (opt_defs[opt_defs_index].long_option, cur_opt_str) != 0)
814 std::string full_name ("--");
815 full_name.append (opt_defs[opt_defs_index].long_option);
816 matches.AppendString(full_name.c_str());
821 matches.AppendString(input.GetArgumentAtIndex(cursor_index));
827 // FIXME - not handling wrong options yet:
828 // Check to see if they are writing a long option & complete it.
829 // I think we will only get in here if the long option table has two elements
830 // that are not unique up to this point. getopt_long_only does shortest unique match
831 // for long options already.
833 if (cur_opt_str && strlen (cur_opt_str) > 2
834 && cur_opt_str[0] == '-' && cur_opt_str[1] == '-')
836 for (int j = 0 ; opt_defs[j].short_option != 0 ; j++)
838 if (strstr(opt_defs[j].long_option, cur_opt_str + 2) == opt_defs[j].long_option)
840 std::string full_name ("--");
841 full_name.append (opt_defs[j].long_option);
842 // The options definitions table has duplicates because of the
843 // way the grouping information is stored, so only add once.
844 bool duplicate = false;
845 for (size_t k = 0; k < matches.GetSize(); k++)
847 if (matches.GetStringAtIndex(k) == full_name)
854 matches.AppendString(full_name.c_str());
863 else if (opt_arg_pos == cursor_index)
865 // Okay the cursor is on the completion of an argument.
866 // See if it has a completion, otherwise return no matches.
868 if (opt_defs_index != -1)
870 HandleOptionArgumentCompletion (input,
872 strlen (input.GetArgumentAtIndex(cursor_index)),
883 // No completion callback means no completions...
890 // Not the last element, keep going.
898 Options::HandleOptionArgumentCompletion
903 OptionElementVector &opt_element_vector,
904 int opt_element_index,
905 int match_start_point,
906 int max_return_elements,
908 lldb_private::StringList &matches
911 const OptionDefinition *opt_defs = GetDefinitions();
912 std::unique_ptr<SearchFilter> filter_ap;
914 int opt_arg_pos = opt_element_vector[opt_element_index].opt_arg_pos;
915 int opt_defs_index = opt_element_vector[opt_element_index].opt_defs_index;
917 // See if this is an enumeration type option, and if so complete it here:
919 OptionEnumValueElement *enum_values = opt_defs[opt_defs_index].enum_values;
920 if (enum_values != nullptr)
922 bool return_value = false;
923 std::string match_string(input.GetArgumentAtIndex (opt_arg_pos), input.GetArgumentAtIndex (opt_arg_pos) + char_pos);
924 for (int i = 0; enum_values[i].string_value != nullptr; i++)
926 if (strstr(enum_values[i].string_value, match_string.c_str()) == enum_values[i].string_value)
928 matches.AppendString (enum_values[i].string_value);
935 // If this is a source file or symbol type completion, and there is a
936 // -shlib option somewhere in the supplied arguments, then make a search filter
937 // for that shared library.
938 // FIXME: Do we want to also have an "OptionType" so we don't have to match string names?
940 uint32_t completion_mask = opt_defs[opt_defs_index].completion_type;
942 if (completion_mask == 0)
944 lldb::CommandArgumentType option_arg_type = opt_defs[opt_defs_index].argument_type;
945 if (option_arg_type != eArgTypeNone)
947 CommandObject::ArgumentTableEntry *arg_entry = CommandObject::FindArgumentDataByType (opt_defs[opt_defs_index].argument_type);
949 completion_mask = arg_entry->completion_type;
953 if (completion_mask & CommandCompletions::eSourceFileCompletion
954 || completion_mask & CommandCompletions::eSymbolCompletion)
956 for (size_t i = 0; i < opt_element_vector.size(); i++)
958 int cur_defs_index = opt_element_vector[i].opt_defs_index;
959 int cur_arg_pos = opt_element_vector[i].opt_arg_pos;
960 const char *cur_opt_name = opt_defs[cur_defs_index].long_option;
962 // If this is the "shlib" option and there was an argument provided,
963 // restrict it to that shared library.
964 if (cur_opt_name && strcmp(cur_opt_name, "shlib") == 0 && cur_arg_pos != -1)
966 const char *module_name = input.GetArgumentAtIndex(cur_arg_pos);
969 FileSpec module_spec(module_name, false);
970 lldb::TargetSP target_sp = m_interpreter.GetDebugger().GetSelectedTarget();
971 // Search filters require a target...
973 filter_ap.reset (new SearchFilterByModule (target_sp, module_spec));
980 return CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
982 input.GetArgumentAtIndex (opt_arg_pos),
993 OptionGroupOptions::Append (OptionGroup* group)
995 const OptionDefinition* group_option_defs = group->GetDefinitions ();
996 const uint32_t group_option_count = group->GetNumDefinitions();
997 for (uint32_t i=0; i<group_option_count; ++i)
999 m_option_infos.push_back (OptionInfo (group, i));
1000 m_option_defs.push_back (group_option_defs[i]);
1005 OptionGroupOptions::GetGroupWithOption (char short_opt)
1007 for (uint32_t i = 0; i < m_option_defs.size(); i++)
1009 OptionDefinition opt_def = m_option_defs[i];
1010 if (opt_def.short_option == short_opt)
1011 return m_option_infos[i].option_group;
1017 OptionGroupOptions::Append (OptionGroup* group,
1021 const OptionDefinition* group_option_defs = group->GetDefinitions ();
1022 const uint32_t group_option_count = group->GetNumDefinitions();
1023 for (uint32_t i=0; i<group_option_count; ++i)
1025 if (group_option_defs[i].usage_mask & src_mask)
1027 m_option_infos.push_back (OptionInfo (group, i));
1028 m_option_defs.push_back (group_option_defs[i]);
1029 m_option_defs.back().usage_mask = dst_mask;
1035 OptionGroupOptions::Finalize ()
1037 m_did_finalize = true;
1038 OptionDefinition empty_option_def = { 0, false, nullptr, 0, 0, nullptr, nullptr, 0, eArgTypeNone, nullptr };
1039 m_option_defs.push_back (empty_option_def);
1043 OptionGroupOptions::SetOptionValue (uint32_t option_idx,
1044 const char *option_value)
1046 // After calling OptionGroupOptions::Append(...), you must finalize the groups
1047 // by calling OptionGroupOptions::Finlize()
1048 assert (m_did_finalize);
1049 assert (m_option_infos.size() + 1 == m_option_defs.size());
1051 if (option_idx < m_option_infos.size())
1053 error = m_option_infos[option_idx].option_group->SetOptionValue (m_interpreter,
1054 m_option_infos[option_idx].option_index,
1060 error.SetErrorString ("invalid option index"); // Shouldn't happen...
1066 OptionGroupOptions::OptionParsingStarting ()
1068 std::set<OptionGroup*> group_set;
1069 OptionInfos::iterator pos, end = m_option_infos.end();
1070 for (pos = m_option_infos.begin(); pos != end; ++pos)
1072 OptionGroup* group = pos->option_group;
1073 if (group_set.find(group) == group_set.end())
1075 group->OptionParsingStarting (m_interpreter);
1076 group_set.insert(group);
1081 OptionGroupOptions::OptionParsingFinished ()
1083 std::set<OptionGroup*> group_set;
1085 OptionInfos::iterator pos, end = m_option_infos.end();
1086 for (pos = m_option_infos.begin(); pos != end; ++pos)
1088 OptionGroup* group = pos->option_group;
1089 if (group_set.find(group) == group_set.end())
1091 error = group->OptionParsingFinished (m_interpreter);
1092 group_set.insert(group);