diff options
Diffstat (limited to 'lldb/source/Interpreter/Options.cpp')
-rw-r--r-- | lldb/source/Interpreter/Options.cpp | 128 |
1 files changed, 41 insertions, 87 deletions
diff --git a/lldb/source/Interpreter/Options.cpp b/lldb/source/Interpreter/Options.cpp index feebe33..7d1f766 100644 --- a/lldb/source/Interpreter/Options.cpp +++ b/lldb/source/Interpreter/Options.cpp @@ -20,6 +20,7 @@ #include "lldb/Interpreter/CommandReturnObject.h" #include "lldb/Target/Target.h" #include "lldb/Utility/StreamString.h" +#include "llvm/ADT/STLExtras.h" using namespace lldb; using namespace lldb_private; @@ -420,8 +421,6 @@ void Options::GenerateOptionUsage(Stream &strm, CommandObject *cmd, uint32_t num_option_sets = GetRequiredOptions().size(); - uint32_t i; - if (!only_print_args) { for (uint32_t opt_set = 0; opt_set < num_option_sets; ++opt_set) { uint32_t opt_set_mask; @@ -440,77 +439,46 @@ void Options::GenerateOptionUsage(Stream &strm, CommandObject *cmd, // single string. If a command has "-a" "-b" and "-c", this will show up // as [-abc] - std::set<int> options; - std::set<int>::const_iterator options_pos, options_end; - for (auto &def : opt_defs) { - if (def.usage_mask & opt_set_mask && def.HasShortOption()) { - // Add current option to the end of out_stream. + // We use a set here so that they will be sorted. + std::set<int> required_options; + std::set<int> optional_options; - if (def.required && def.option_has_arg == OptionParser::eNoArgument) { - options.insert(def.short_option); + for (auto &def : opt_defs) { + if (def.usage_mask & opt_set_mask && def.HasShortOption() && + def.option_has_arg == OptionParser::eNoArgument) { + if (def.required) { + required_options.insert(def.short_option); + } else { + optional_options.insert(def.short_option); } } } - if (!options.empty()) { - // We have some required options with no arguments + if (!required_options.empty()) { strm.PutCString(" -"); - for (i = 0; i < 2; ++i) - for (options_pos = options.begin(), options_end = options.end(); - options_pos != options_end; ++options_pos) { - if (i == 0 && ::islower(*options_pos)) - continue; - if (i == 1 && ::isupper(*options_pos)) - continue; - strm << (char)*options_pos; - } + for (int short_option : required_options) + strm.PutChar(short_option); } - options.clear(); - for (auto &def : opt_defs) { - if (def.usage_mask & opt_set_mask && def.HasShortOption()) { - // Add current option to the end of out_stream. - - if (!def.required && - def.option_has_arg == OptionParser::eNoArgument) { - options.insert(def.short_option); - } - } - } - - if (!options.empty()) { - // We have some required options with no arguments + if (!optional_options.empty()) { strm.PutCString(" [-"); - for (i = 0; i < 2; ++i) - for (options_pos = options.begin(), options_end = options.end(); - options_pos != options_end; ++options_pos) { - if (i == 0 && ::islower(*options_pos)) - continue; - if (i == 1 && ::isupper(*options_pos)) - continue; - strm << (char)*options_pos; - } + for (int short_option : optional_options) + strm.PutChar(short_option); strm.PutChar(']'); } // First go through and print the required options (list them up front). - for (auto &def : opt_defs) { - if (def.usage_mask & opt_set_mask && def.HasShortOption()) { - if (def.required && def.option_has_arg != OptionParser::eNoArgument) - PrintOption(def, eDisplayBestOption, " ", nullptr, true, strm); - } + if (def.usage_mask & opt_set_mask && def.HasShortOption() && + def.required && def.option_has_arg != OptionParser::eNoArgument) + PrintOption(def, eDisplayBestOption, " ", nullptr, true, strm); } // Now go through again, and this time only print the optional options. - for (auto &def : opt_defs) { - if (def.usage_mask & opt_set_mask) { - // Add current option to the end of out_stream. - - if (!def.required && def.option_has_arg != OptionParser::eNoArgument) - PrintOption(def, eDisplayBestOption, " ", nullptr, true, strm); - } + if (def.usage_mask & opt_set_mask && !def.required && + def.option_has_arg != OptionParser::eNoArgument) + PrintOption(def, eDisplayBestOption, " ", nullptr, true, strm); } if (args_str.GetSize() > 0) { @@ -540,63 +508,49 @@ void Options::GenerateOptionUsage(Stream &strm, CommandObject *cmd, // -short <argument> ( --long_name <argument> ) // help text - // This variable is used to keep track of which options' info we've printed - // out, because some options can be in more than one usage level, but we - // only want to print the long form of its information once. - - std::multimap<int, uint32_t> options_seen; strm.IndentMore(5); - // Put the unique command options in a vector & sort it, so we can output - // them alphabetically (by short_option) when writing out detailed help for - // each option. - - i = 0; - for (auto &def : opt_defs) - options_seen.insert(std::make_pair(def.short_option, i++)); + // Put the command options in a sorted container, so we can output + // them alphabetically by short_option. + std::multimap<int, uint32_t> options_ordered; + for (auto def : llvm::enumerate(opt_defs)) + options_ordered.insert( + std::make_pair(def.value().short_option, def.index())); - // Go through the unique'd and alphabetically sorted vector of options, - // find the table entry for each option and write out the detailed help - // information for that option. + // Go through each option, find the table entry and write out the detailed + // help information for that option. bool first_option_printed = false; - for (auto pos : options_seen) { - i = pos.second; - // Print out the help information for this option. - + for (auto pos : options_ordered) { // Put a newline separation between arguments if (first_option_printed) strm.EOL(); else first_option_printed = true; - CommandArgumentType arg_type = opt_defs[i].argument_type; - - StreamString arg_name_str; - arg_name_str.Printf("<%s>", CommandObject::GetArgumentName(arg_type)); + OptionDefinition opt_def = opt_defs[pos.second]; strm.Indent(); - if (opt_defs[i].short_option && opt_defs[i].HasShortOption()) { - PrintOption(opt_defs[i], eDisplayShortOption, nullptr, nullptr, false, + if (opt_def.short_option && opt_def.HasShortOption()) { + PrintOption(opt_def, eDisplayShortOption, nullptr, nullptr, false, strm); - PrintOption(opt_defs[i], eDisplayLongOption, " ( ", " )", false, strm); + PrintOption(opt_def, eDisplayLongOption, " ( ", " )", false, strm); } else { // Short option is not printable, just print long option - PrintOption(opt_defs[i], eDisplayLongOption, nullptr, nullptr, false, - strm); + PrintOption(opt_def, eDisplayLongOption, nullptr, nullptr, false, strm); } strm.EOL(); strm.IndentMore(5); - if (opt_defs[i].usage_text) - OutputFormattedUsageText(strm, opt_defs[i], screen_width); - if (!opt_defs[i].enum_values.empty()) { + if (opt_def.usage_text) + OutputFormattedUsageText(strm, opt_def, screen_width); + if (!opt_def.enum_values.empty()) { strm.Indent(); strm.Printf("Values: "); bool is_first = true; - for (const auto &enum_value : opt_defs[i].enum_values) { + for (const auto &enum_value : opt_def.enum_values) { if (is_first) { strm.Printf("%s", enum_value.string_value); is_first = false; |