Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- Options.cpp ---------------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Jim Ingham | 40af72e | 2010-06-15 19:49:27 +0000 | [diff] [blame] | 10 | #include "lldb/Interpreter/Options.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 11 | |
| 12 | // C Includes |
| 13 | // C++ Includes |
Caroline Tice | f362c45 | 2010-09-09 16:44:14 +0000 | [diff] [blame] | 14 | #include <algorithm> |
Greg Clayton | ab65b34 | 2011-04-13 22:47:15 +0000 | [diff] [blame] | 15 | #include <bitset> |
Greg Clayton | 3bcdfc0 | 2012-12-04 00:32:51 +0000 | [diff] [blame] | 16 | #include <map> |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 17 | |
| 18 | // Other libraries and framework includes |
| 19 | // Project includes |
| 20 | #include "lldb/Interpreter/CommandObject.h" |
| 21 | #include "lldb/Interpreter/CommandReturnObject.h" |
| 22 | #include "lldb/Interpreter/CommandCompletions.h" |
| 23 | #include "lldb/Interpreter/CommandInterpreter.h" |
| 24 | #include "lldb/Core/StreamString.h" |
| 25 | #include "lldb/Target/Target.h" |
| 26 | |
| 27 | using namespace lldb; |
| 28 | using namespace lldb_private; |
| 29 | |
| 30 | //------------------------------------------------------------------------- |
| 31 | // Options |
| 32 | //------------------------------------------------------------------------- |
Greg Clayton | eb0103f | 2011-04-07 22:46:35 +0000 | [diff] [blame] | 33 | Options::Options (CommandInterpreter &interpreter) : |
| 34 | m_interpreter (interpreter), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 35 | m_getopt_table () |
| 36 | { |
Jim Ingham | 8651121 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 37 | BuildValidOptionSets(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | Options::~Options () |
| 41 | { |
| 42 | } |
| 43 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 44 | void |
Greg Clayton | f6b8b58 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 45 | Options::NotifyOptionParsingStarting () |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 46 | { |
| 47 | m_seen_options.clear(); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 48 | // Let the subclass reset its option values |
Greg Clayton | f6b8b58 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 49 | OptionParsingStarting (); |
| 50 | } |
| 51 | |
| 52 | Error |
| 53 | Options::NotifyOptionParsingFinished () |
| 54 | { |
| 55 | return OptionParsingFinished (); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | void |
| 59 | Options::OptionSeen (int option_idx) |
| 60 | { |
Greg Clayton | 3bcdfc0 | 2012-12-04 00:32:51 +0000 | [diff] [blame] | 61 | m_seen_options.insert (option_idx); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | // Returns true is set_a is a subset of set_b; Otherwise returns false. |
| 65 | |
| 66 | bool |
| 67 | Options::IsASubset (const OptionSet& set_a, const OptionSet& set_b) |
| 68 | { |
| 69 | bool is_a_subset = true; |
| 70 | OptionSet::const_iterator pos_a; |
| 71 | OptionSet::const_iterator pos_b; |
| 72 | |
| 73 | // set_a is a subset of set_b if every member of set_a is also a member of set_b |
| 74 | |
| 75 | for (pos_a = set_a.begin(); pos_a != set_a.end() && is_a_subset; ++pos_a) |
| 76 | { |
| 77 | pos_b = set_b.find(*pos_a); |
| 78 | if (pos_b == set_b.end()) |
| 79 | is_a_subset = false; |
| 80 | } |
| 81 | |
| 82 | return is_a_subset; |
| 83 | } |
| 84 | |
| 85 | // Returns the set difference set_a - set_b, i.e. { x | ElementOf (x, set_a) && !ElementOf (x, set_b) } |
| 86 | |
| 87 | size_t |
| 88 | Options::OptionsSetDiff (const OptionSet& set_a, const OptionSet& set_b, OptionSet& diffs) |
| 89 | { |
| 90 | size_t num_diffs = 0; |
| 91 | OptionSet::const_iterator pos_a; |
| 92 | OptionSet::const_iterator pos_b; |
| 93 | |
| 94 | for (pos_a = set_a.begin(); pos_a != set_a.end(); ++pos_a) |
| 95 | { |
| 96 | pos_b = set_b.find(*pos_a); |
| 97 | if (pos_b == set_b.end()) |
| 98 | { |
| 99 | ++num_diffs; |
| 100 | diffs.insert(*pos_a); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | return num_diffs; |
| 105 | } |
| 106 | |
| 107 | // Returns the union of set_a and set_b. Does not put duplicate members into the union. |
| 108 | |
| 109 | void |
| 110 | Options::OptionsSetUnion (const OptionSet &set_a, const OptionSet &set_b, OptionSet &union_set) |
| 111 | { |
| 112 | OptionSet::const_iterator pos; |
| 113 | OptionSet::iterator pos_union; |
| 114 | |
| 115 | // Put all the elements of set_a into the union. |
| 116 | |
| 117 | for (pos = set_a.begin(); pos != set_a.end(); ++pos) |
| 118 | union_set.insert(*pos); |
| 119 | |
| 120 | // Put all the elements of set_b that are not already there into the union. |
| 121 | for (pos = set_b.begin(); pos != set_b.end(); ++pos) |
| 122 | { |
| 123 | pos_union = union_set.find(*pos); |
| 124 | if (pos_union == union_set.end()) |
| 125 | union_set.insert(*pos); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | bool |
| 130 | Options::VerifyOptions (CommandReturnObject &result) |
| 131 | { |
| 132 | bool options_are_valid = false; |
| 133 | |
Jim Ingham | d6ccc60 | 2010-06-24 20:30:15 +0000 | [diff] [blame] | 134 | int num_levels = GetRequiredOptions().size(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 135 | if (num_levels) |
| 136 | { |
| 137 | for (int i = 0; i < num_levels && !options_are_valid; ++i) |
| 138 | { |
| 139 | // This is the correct set of options if: 1). m_seen_options contains all of m_required_options[i] |
| 140 | // (i.e. all the required options at this level are a subset of m_seen_options); AND |
| 141 | // 2). { m_seen_options - m_required_options[i] is a subset of m_options_options[i] (i.e. all the rest of |
| 142 | // m_seen_options are in the set of optional options at this level. |
| 143 | |
| 144 | // Check to see if all of m_required_options[i] are a subset of m_seen_options |
Jim Ingham | d6ccc60 | 2010-06-24 20:30:15 +0000 | [diff] [blame] | 145 | if (IsASubset (GetRequiredOptions()[i], m_seen_options)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 146 | { |
| 147 | // Construct the set difference: remaining_options = {m_seen_options} - {m_required_options[i]} |
| 148 | OptionSet remaining_options; |
Jim Ingham | d6ccc60 | 2010-06-24 20:30:15 +0000 | [diff] [blame] | 149 | OptionsSetDiff (m_seen_options, GetRequiredOptions()[i], remaining_options); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 150 | // Check to see if remaining_options is a subset of m_optional_options[i] |
Jim Ingham | d6ccc60 | 2010-06-24 20:30:15 +0000 | [diff] [blame] | 151 | if (IsASubset (remaining_options, GetOptionalOptions()[i])) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 152 | options_are_valid = true; |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | else |
| 157 | { |
| 158 | options_are_valid = true; |
| 159 | } |
| 160 | |
| 161 | if (options_are_valid) |
| 162 | { |
| 163 | result.SetStatus (eReturnStatusSuccessFinishNoResult); |
| 164 | } |
| 165 | else |
| 166 | { |
| 167 | result.AppendError ("invalid combination of options for the given command"); |
| 168 | result.SetStatus (eReturnStatusFailed); |
| 169 | } |
| 170 | |
| 171 | return options_are_valid; |
| 172 | } |
| 173 | |
Jim Ingham | 8651121 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 174 | // This is called in the Options constructor, though we could call it lazily if that ends up being |
| 175 | // a performance problem. |
| 176 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 177 | void |
| 178 | Options::BuildValidOptionSets () |
| 179 | { |
| 180 | // Check to see if we already did this. |
| 181 | if (m_required_options.size() != 0) |
| 182 | return; |
| 183 | |
| 184 | // Check to see if there are any options. |
| 185 | int num_options = NumCommandOptions (); |
| 186 | if (num_options == 0) |
| 187 | return; |
| 188 | |
Greg Clayton | 52ec56c | 2011-10-29 00:57:28 +0000 | [diff] [blame] | 189 | const OptionDefinition *opt_defs = GetDefinitions(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 190 | m_required_options.resize(1); |
| 191 | m_optional_options.resize(1); |
Jim Ingham | 8651121 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 192 | |
| 193 | // First count the number of option sets we've got. Ignore LLDB_ALL_OPTION_SETS... |
| 194 | |
| 195 | uint32_t num_option_sets = 0; |
| 196 | |
| 197 | for (int i = 0; i < num_options; i++) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 198 | { |
Greg Clayton | 52ec56c | 2011-10-29 00:57:28 +0000 | [diff] [blame] | 199 | uint32_t this_usage_mask = opt_defs[i].usage_mask; |
Jim Ingham | 8651121 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 200 | if (this_usage_mask == LLDB_OPT_SET_ALL) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 201 | { |
Jim Ingham | 8651121 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 202 | if (num_option_sets == 0) |
| 203 | num_option_sets = 1; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 204 | } |
| 205 | else |
| 206 | { |
Andy Gibbs | a297a97 | 2013-06-19 19:04:53 +0000 | [diff] [blame] | 207 | for (uint32_t j = 0; j < LLDB_MAX_NUM_OPTION_SETS; j++) |
Jim Ingham | 8651121 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 208 | { |
Jim Ingham | d6ccc60 | 2010-06-24 20:30:15 +0000 | [diff] [blame] | 209 | if (this_usage_mask & (1 << j)) |
Jim Ingham | 8651121 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 210 | { |
| 211 | if (num_option_sets <= j) |
| 212 | num_option_sets = j + 1; |
| 213 | } |
| 214 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 215 | } |
Jim Ingham | 8651121 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 216 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 217 | |
Jim Ingham | 8651121 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 218 | if (num_option_sets > 0) |
| 219 | { |
| 220 | m_required_options.resize(num_option_sets); |
| 221 | m_optional_options.resize(num_option_sets); |
| 222 | |
| 223 | for (int i = 0; i < num_options; ++i) |
| 224 | { |
Andy Gibbs | a297a97 | 2013-06-19 19:04:53 +0000 | [diff] [blame] | 225 | for (uint32_t j = 0; j < num_option_sets; j++) |
Jim Ingham | 8651121 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 226 | { |
Greg Clayton | 52ec56c | 2011-10-29 00:57:28 +0000 | [diff] [blame] | 227 | if (opt_defs[i].usage_mask & 1 << j) |
Jim Ingham | 8651121 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 228 | { |
Greg Clayton | 52ec56c | 2011-10-29 00:57:28 +0000 | [diff] [blame] | 229 | if (opt_defs[i].required) |
| 230 | m_required_options[j].insert(opt_defs[i].short_option); |
Jim Ingham | 8651121 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 231 | else |
Greg Clayton | 52ec56c | 2011-10-29 00:57:28 +0000 | [diff] [blame] | 232 | m_optional_options[j].insert(opt_defs[i].short_option); |
Jim Ingham | 8651121 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 233 | } |
| 234 | } |
| 235 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | |
| 239 | uint32_t |
| 240 | Options::NumCommandOptions () |
| 241 | { |
Greg Clayton | 52ec56c | 2011-10-29 00:57:28 +0000 | [diff] [blame] | 242 | const OptionDefinition *opt_defs = GetDefinitions (); |
Ed Maste | d78c957 | 2014-04-20 00:31:37 +0000 | [diff] [blame] | 243 | if (opt_defs == nullptr) |
Jim Ingham | 8651121 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 244 | return 0; |
| 245 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 246 | int i = 0; |
| 247 | |
Ed Maste | d78c957 | 2014-04-20 00:31:37 +0000 | [diff] [blame] | 248 | if (opt_defs != nullptr) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 249 | { |
Ed Maste | d78c957 | 2014-04-20 00:31:37 +0000 | [diff] [blame] | 250 | while (opt_defs[i].long_option != nullptr) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 251 | ++i; |
| 252 | } |
| 253 | |
| 254 | return i; |
| 255 | } |
| 256 | |
Virgile Bello | e2607b5 | 2013-09-05 16:42:23 +0000 | [diff] [blame] | 257 | Option * |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 258 | Options::GetLongOptions () |
| 259 | { |
| 260 | // Check to see if this has already been done. |
| 261 | if (m_getopt_table.empty()) |
| 262 | { |
| 263 | // Check to see if there are any options. |
| 264 | const uint32_t num_options = NumCommandOptions(); |
| 265 | if (num_options == 0) |
Ed Maste | d78c957 | 2014-04-20 00:31:37 +0000 | [diff] [blame] | 266 | return nullptr; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 267 | |
| 268 | uint32_t i; |
Greg Clayton | 52ec56c | 2011-10-29 00:57:28 +0000 | [diff] [blame] | 269 | const OptionDefinition *opt_defs = GetDefinitions(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 270 | |
Greg Clayton | 3bcdfc0 | 2012-12-04 00:32:51 +0000 | [diff] [blame] | 271 | std::map<int, uint32_t> option_seen; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 272 | |
| 273 | m_getopt_table.resize(num_options + 1); |
Greg Clayton | 3bcdfc0 | 2012-12-04 00:32:51 +0000 | [diff] [blame] | 274 | for (i = 0; i < num_options; ++i) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 275 | { |
Greg Clayton | 3bcdfc0 | 2012-12-04 00:32:51 +0000 | [diff] [blame] | 276 | const int short_opt = opt_defs[i].short_option; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 277 | |
Zachary Turner | d37221d | 2014-07-09 16:31:49 +0000 | [diff] [blame] | 278 | m_getopt_table[i].definition = &opt_defs[i]; |
Ed Maste | d78c957 | 2014-04-20 00:31:37 +0000 | [diff] [blame] | 279 | m_getopt_table[i].flag = nullptr; |
Greg Clayton | 3bcdfc0 | 2012-12-04 00:32:51 +0000 | [diff] [blame] | 280 | m_getopt_table[i].val = short_opt; |
| 281 | |
| 282 | if (option_seen.find(short_opt) == option_seen.end()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 283 | { |
Greg Clayton | 3bcdfc0 | 2012-12-04 00:32:51 +0000 | [diff] [blame] | 284 | option_seen[short_opt] = i; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 285 | } |
Greg Clayton | 3bcdfc0 | 2012-12-04 00:32:51 +0000 | [diff] [blame] | 286 | else if (short_opt) |
Greg Clayton | 1c5f186 | 2012-11-30 19:05:35 +0000 | [diff] [blame] | 287 | { |
Greg Clayton | 3bcdfc0 | 2012-12-04 00:32:51 +0000 | [diff] [blame] | 288 | m_getopt_table[i].val = 0; |
| 289 | std::map<int, uint32_t>::const_iterator pos = option_seen.find(short_opt); |
| 290 | StreamString strm; |
Daniel Malea | 90b0c84 | 2012-12-05 20:24:57 +0000 | [diff] [blame] | 291 | if (isprint8(short_opt)) |
Greg Clayton | 3bcdfc0 | 2012-12-04 00:32:51 +0000 | [diff] [blame] | 292 | 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", |
| 293 | i, |
| 294 | opt_defs[i].long_option, |
| 295 | short_opt, |
| 296 | pos->second, |
Zachary Turner | d37221d | 2014-07-09 16:31:49 +0000 | [diff] [blame] | 297 | m_getopt_table[pos->second].definition->long_option, |
Greg Clayton | 3bcdfc0 | 2012-12-04 00:32:51 +0000 | [diff] [blame] | 298 | opt_defs[i].long_option); |
| 299 | else |
| 300 | 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", |
| 301 | i, |
| 302 | opt_defs[i].long_option, |
| 303 | short_opt, |
| 304 | pos->second, |
Zachary Turner | d37221d | 2014-07-09 16:31:49 +0000 | [diff] [blame] | 305 | m_getopt_table[pos->second].definition->long_option, |
Greg Clayton | 3bcdfc0 | 2012-12-04 00:32:51 +0000 | [diff] [blame] | 306 | opt_defs[i].long_option); |
Greg Clayton | 1c5f186 | 2012-11-30 19:05:35 +0000 | [diff] [blame] | 307 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 308 | } |
| 309 | |
Greg Clayton | b7ad58a | 2013-04-04 20:35:24 +0000 | [diff] [blame] | 310 | //getopt_long_only requires a NULL final entry in the table: |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 311 | |
Zachary Turner | d37221d | 2014-07-09 16:31:49 +0000 | [diff] [blame] | 312 | m_getopt_table[i].definition = nullptr; |
| 313 | m_getopt_table[i].flag = nullptr; |
| 314 | m_getopt_table[i].val = 0; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 315 | } |
| 316 | |
Greg Clayton | 471b31c | 2010-07-20 22:52:08 +0000 | [diff] [blame] | 317 | if (m_getopt_table.empty()) |
Ed Maste | d78c957 | 2014-04-20 00:31:37 +0000 | [diff] [blame] | 318 | return nullptr; |
Greg Clayton | 471b31c | 2010-07-20 22:52:08 +0000 | [diff] [blame] | 319 | |
| 320 | return &m_getopt_table.front(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | |
| 324 | // This function takes INDENT, which tells how many spaces to output at the front of each line; SPACES, which is |
| 325 | // a string containing 80 spaces; and TEXT, which is the text that is to be output. It outputs the text, on |
| 326 | // multiple lines if necessary, to RESULT, with INDENT spaces at the front of each line. It breaks lines on spaces, |
| 327 | // tabs or newlines, shortening the line if necessary to not break in the middle of a word. It assumes that each |
| 328 | // output line should contain a maximum of OUTPUT_MAX_COLUMNS characters. |
| 329 | |
| 330 | |
| 331 | void |
| 332 | Options::OutputFormattedUsageText |
| 333 | ( |
| 334 | Stream &strm, |
Zachary Turner | d37221d | 2014-07-09 16:31:49 +0000 | [diff] [blame] | 335 | const OptionDefinition &option_def, |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 336 | uint32_t output_max_columns |
| 337 | ) |
| 338 | { |
Zachary Turner | d37221d | 2014-07-09 16:31:49 +0000 | [diff] [blame] | 339 | std::string actual_text; |
| 340 | if (option_def.validator) |
| 341 | { |
| 342 | const char *condition = option_def.validator->ShortConditionString(); |
| 343 | if (condition) |
| 344 | { |
| 345 | actual_text = "["; |
| 346 | actual_text.append(condition); |
| 347 | actual_text.append("] "); |
| 348 | } |
| 349 | } |
| 350 | actual_text.append(option_def.usage_text); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 351 | |
| 352 | // Will it all fit on one line? |
| 353 | |
Zachary Turner | d37221d | 2014-07-09 16:31:49 +0000 | [diff] [blame] | 354 | if (static_cast<uint32_t>(actual_text.length() + strm.GetIndentLevel()) < output_max_columns) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 355 | { |
| 356 | // Output it as a single line. |
Zachary Turner | d37221d | 2014-07-09 16:31:49 +0000 | [diff] [blame] | 357 | strm.Indent (actual_text.c_str()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 358 | strm.EOL(); |
| 359 | } |
| 360 | else |
| 361 | { |
| 362 | // We need to break it up into multiple lines. |
| 363 | |
| 364 | int text_width = output_max_columns - strm.GetIndentLevel() - 1; |
| 365 | int start = 0; |
| 366 | int end = start; |
Zachary Turner | d37221d | 2014-07-09 16:31:49 +0000 | [diff] [blame] | 367 | int final_end = actual_text.length(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 368 | int sub_len; |
| 369 | |
| 370 | while (end < final_end) |
| 371 | { |
| 372 | // Don't start the 'text' on a space, since we're already outputting the indentation. |
Zachary Turner | d37221d | 2014-07-09 16:31:49 +0000 | [diff] [blame] | 373 | while ((start < final_end) && (actual_text[start] == ' ')) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 374 | start++; |
| 375 | |
| 376 | end = start + text_width; |
| 377 | if (end > final_end) |
| 378 | end = final_end; |
| 379 | else |
| 380 | { |
| 381 | // If we're not at the end of the text, make sure we break the line on white space. |
| 382 | while (end > start |
Zachary Turner | d37221d | 2014-07-09 16:31:49 +0000 | [diff] [blame] | 383 | && actual_text[end] != ' ' && actual_text[end] != '\t' && actual_text[end] != '\n') |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 384 | end--; |
| 385 | } |
| 386 | |
| 387 | sub_len = end - start; |
| 388 | if (start != 0) |
| 389 | strm.EOL(); |
| 390 | strm.Indent(); |
| 391 | assert (start < final_end); |
| 392 | assert (start + sub_len <= final_end); |
Zachary Turner | d37221d | 2014-07-09 16:31:49 +0000 | [diff] [blame] | 393 | strm.Write(actual_text.c_str() + start, sub_len); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 394 | start = end + 1; |
| 395 | } |
| 396 | strm.EOL(); |
| 397 | } |
| 398 | } |
| 399 | |
Greg Clayton | 52ec56c | 2011-10-29 00:57:28 +0000 | [diff] [blame] | 400 | bool |
| 401 | Options::SupportsLongOption (const char *long_option) |
| 402 | { |
| 403 | if (long_option && long_option[0]) |
| 404 | { |
| 405 | const OptionDefinition *opt_defs = GetDefinitions (); |
| 406 | if (opt_defs) |
| 407 | { |
Greg Clayton | 9d3d688 | 2011-10-31 23:51:19 +0000 | [diff] [blame] | 408 | const char *long_option_name = long_option; |
Greg Clayton | 52ec56c | 2011-10-29 00:57:28 +0000 | [diff] [blame] | 409 | if (long_option[0] == '-' && long_option[1] == '-') |
| 410 | long_option_name += 2; |
Greg Clayton | 52ec56c | 2011-10-29 00:57:28 +0000 | [diff] [blame] | 411 | |
| 412 | for (uint32_t i = 0; opt_defs[i].long_option; ++i) |
| 413 | { |
| 414 | if (strcmp(opt_defs[i].long_option, long_option_name) == 0) |
| 415 | return true; |
| 416 | } |
| 417 | } |
| 418 | } |
| 419 | return false; |
| 420 | } |
| 421 | |
Greg Clayton | 3bcdfc0 | 2012-12-04 00:32:51 +0000 | [diff] [blame] | 422 | enum OptionDisplayType |
| 423 | { |
| 424 | eDisplayBestOption, |
| 425 | eDisplayShortOption, |
| 426 | eDisplayLongOption |
| 427 | }; |
| 428 | |
| 429 | static bool |
| 430 | PrintOption (const OptionDefinition &opt_def, |
| 431 | OptionDisplayType display_type, |
| 432 | const char *header, |
| 433 | const char *footer, |
| 434 | bool show_optional, |
| 435 | Stream &strm) |
| 436 | { |
Daniel Malea | 90b0c84 | 2012-12-05 20:24:57 +0000 | [diff] [blame] | 437 | const bool has_short_option = isprint8(opt_def.short_option) != 0; |
Greg Clayton | 3bcdfc0 | 2012-12-04 00:32:51 +0000 | [diff] [blame] | 438 | |
| 439 | if (display_type == eDisplayShortOption && !has_short_option) |
| 440 | return false; |
| 441 | |
| 442 | if (header && header[0]) |
| 443 | strm.PutCString(header); |
| 444 | |
| 445 | if (show_optional && !opt_def.required) |
| 446 | strm.PutChar('['); |
| 447 | const bool show_short_option = has_short_option && display_type != eDisplayLongOption; |
| 448 | if (show_short_option) |
| 449 | strm.Printf ("-%c", opt_def.short_option); |
| 450 | else |
| 451 | strm.Printf ("--%s", opt_def.long_option); |
| 452 | switch (opt_def.option_has_arg) |
| 453 | { |
Virgile Bello | e2607b5 | 2013-09-05 16:42:23 +0000 | [diff] [blame] | 454 | case OptionParser::eNoArgument: |
Greg Clayton | 3bcdfc0 | 2012-12-04 00:32:51 +0000 | [diff] [blame] | 455 | break; |
Virgile Bello | e2607b5 | 2013-09-05 16:42:23 +0000 | [diff] [blame] | 456 | case OptionParser::eRequiredArgument: |
Greg Clayton | 3bcdfc0 | 2012-12-04 00:32:51 +0000 | [diff] [blame] | 457 | strm.Printf (" <%s>", CommandObject::GetArgumentName (opt_def.argument_type)); |
| 458 | break; |
| 459 | |
Virgile Bello | e2607b5 | 2013-09-05 16:42:23 +0000 | [diff] [blame] | 460 | case OptionParser::eOptionalArgument: |
Greg Clayton | 3bcdfc0 | 2012-12-04 00:32:51 +0000 | [diff] [blame] | 461 | strm.Printf ("%s[<%s>]", |
| 462 | show_short_option ? "" : "=", |
| 463 | CommandObject::GetArgumentName (opt_def.argument_type)); |
| 464 | break; |
| 465 | } |
| 466 | if (show_optional && !opt_def.required) |
| 467 | strm.PutChar(']'); |
| 468 | if (footer && footer[0]) |
| 469 | strm.PutCString(footer); |
| 470 | return true; |
| 471 | } |
| 472 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 473 | void |
| 474 | Options::GenerateOptionUsage |
| 475 | ( |
| 476 | Stream &strm, |
Greg Clayton | a701509 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 477 | CommandObject *cmd |
| 478 | ) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 479 | { |
Greg Clayton | eb0103f | 2011-04-07 22:46:35 +0000 | [diff] [blame] | 480 | const uint32_t screen_width = m_interpreter.GetDebugger().GetTerminalWidth(); |
Caroline Tice | 3df9a8d | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 481 | |
Greg Clayton | 52ec56c | 2011-10-29 00:57:28 +0000 | [diff] [blame] | 482 | const OptionDefinition *opt_defs = GetDefinitions(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 483 | const uint32_t save_indent_level = strm.GetIndentLevel(); |
| 484 | const char *name; |
| 485 | |
Caroline Tice | e139cf2 | 2010-10-01 17:46:38 +0000 | [diff] [blame] | 486 | StreamString arguments_str; |
| 487 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 488 | if (cmd) |
Caroline Tice | e139cf2 | 2010-10-01 17:46:38 +0000 | [diff] [blame] | 489 | { |
Greg Clayton | a701509 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 490 | name = cmd->GetCommandName(); |
Caroline Tice | e139cf2 | 2010-10-01 17:46:38 +0000 | [diff] [blame] | 491 | cmd->GetFormattedCommandArguments (arguments_str); |
| 492 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 493 | else |
Greg Clayton | a701509 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 494 | name = ""; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 495 | |
| 496 | strm.PutCString ("\nCommand Options Usage:\n"); |
| 497 | |
| 498 | strm.IndentMore(2); |
| 499 | |
| 500 | // First, show each usage level set of options, e.g. <cmd> [options-for-level-0] |
| 501 | // <cmd> [options-for-level-1] |
| 502 | // etc. |
| 503 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 504 | const uint32_t num_options = NumCommandOptions(); |
Jim Ingham | 8651121 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 505 | if (num_options == 0) |
| 506 | return; |
| 507 | |
Andy Gibbs | 70f94f9 | 2013-06-24 14:04:57 +0000 | [diff] [blame] | 508 | uint32_t num_option_sets = GetRequiredOptions().size(); |
Jim Ingham | 8651121 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 509 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 510 | uint32_t i; |
Jim Ingham | 8651121 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 511 | |
| 512 | for (uint32_t opt_set = 0; opt_set < num_option_sets; ++opt_set) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 513 | { |
Jim Ingham | 8651121 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 514 | uint32_t opt_set_mask; |
| 515 | |
| 516 | opt_set_mask = 1 << opt_set; |
| 517 | if (opt_set > 0) |
| 518 | strm.Printf ("\n"); |
| 519 | strm.Indent (name); |
Caroline Tice | f362c45 | 2010-09-09 16:44:14 +0000 | [diff] [blame] | 520 | |
Johnny Chen | 34ddc8d | 2012-02-08 01:13:31 +0000 | [diff] [blame] | 521 | // Different option sets may require different args. |
| 522 | StreamString args_str; |
Jim Ingham | 28eb571 | 2012-10-12 17:34:26 +0000 | [diff] [blame] | 523 | if (cmd) |
| 524 | cmd->GetFormattedCommandArguments(args_str, opt_set_mask); |
Johnny Chen | 34ddc8d | 2012-02-08 01:13:31 +0000 | [diff] [blame] | 525 | |
Greg Clayton | ed8a705 | 2010-09-18 03:37:20 +0000 | [diff] [blame] | 526 | // First go through and print all options that take no arguments as |
| 527 | // a single string. If a command has "-a" "-b" and "-c", this will show |
| 528 | // up as [-abc] |
| 529 | |
Greg Clayton | 3bcdfc0 | 2012-12-04 00:32:51 +0000 | [diff] [blame] | 530 | std::set<int> options; |
| 531 | std::set<int>::const_iterator options_pos, options_end; |
Greg Clayton | 03da4cc | 2013-04-19 21:31:16 +0000 | [diff] [blame] | 532 | for (i = 0; i < num_options; ++i) |
Greg Clayton | ed8a705 | 2010-09-18 03:37:20 +0000 | [diff] [blame] | 533 | { |
Daniel Malea | 90b0c84 | 2012-12-05 20:24:57 +0000 | [diff] [blame] | 534 | if (opt_defs[i].usage_mask & opt_set_mask && isprint8(opt_defs[i].short_option)) |
Greg Clayton | ed8a705 | 2010-09-18 03:37:20 +0000 | [diff] [blame] | 535 | { |
| 536 | // Add current option to the end of out_stream. |
| 537 | |
Greg Clayton | 52ec56c | 2011-10-29 00:57:28 +0000 | [diff] [blame] | 538 | if (opt_defs[i].required == true && |
Virgile Bello | e2607b5 | 2013-09-05 16:42:23 +0000 | [diff] [blame] | 539 | opt_defs[i].option_has_arg == OptionParser::eNoArgument) |
Greg Clayton | ed8a705 | 2010-09-18 03:37:20 +0000 | [diff] [blame] | 540 | { |
Greg Clayton | 52ec56c | 2011-10-29 00:57:28 +0000 | [diff] [blame] | 541 | options.insert (opt_defs[i].short_option); |
Greg Clayton | ed8a705 | 2010-09-18 03:37:20 +0000 | [diff] [blame] | 542 | } |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | if (options.empty() == false) |
| 547 | { |
| 548 | // We have some required options with no arguments |
| 549 | strm.PutCString(" -"); |
| 550 | for (i=0; i<2; ++i) |
| 551 | for (options_pos = options.begin(), options_end = options.end(); |
| 552 | options_pos != options_end; |
| 553 | ++options_pos) |
| 554 | { |
Greg Clayton | 3bcdfc0 | 2012-12-04 00:32:51 +0000 | [diff] [blame] | 555 | if (i==0 && ::islower (*options_pos)) |
Greg Clayton | ed8a705 | 2010-09-18 03:37:20 +0000 | [diff] [blame] | 556 | continue; |
Greg Clayton | 3bcdfc0 | 2012-12-04 00:32:51 +0000 | [diff] [blame] | 557 | if (i==1 && ::isupper (*options_pos)) |
Greg Clayton | ed8a705 | 2010-09-18 03:37:20 +0000 | [diff] [blame] | 558 | continue; |
Greg Clayton | 3bcdfc0 | 2012-12-04 00:32:51 +0000 | [diff] [blame] | 559 | strm << (char)*options_pos; |
Greg Clayton | ed8a705 | 2010-09-18 03:37:20 +0000 | [diff] [blame] | 560 | } |
| 561 | } |
| 562 | |
| 563 | for (i = 0, options.clear(); i < num_options; ++i) |
| 564 | { |
Daniel Malea | 90b0c84 | 2012-12-05 20:24:57 +0000 | [diff] [blame] | 565 | if (opt_defs[i].usage_mask & opt_set_mask && isprint8(opt_defs[i].short_option)) |
Greg Clayton | ed8a705 | 2010-09-18 03:37:20 +0000 | [diff] [blame] | 566 | { |
| 567 | // Add current option to the end of out_stream. |
| 568 | |
Greg Clayton | 52ec56c | 2011-10-29 00:57:28 +0000 | [diff] [blame] | 569 | if (opt_defs[i].required == false && |
Virgile Bello | e2607b5 | 2013-09-05 16:42:23 +0000 | [diff] [blame] | 570 | opt_defs[i].option_has_arg == OptionParser::eNoArgument) |
Greg Clayton | ed8a705 | 2010-09-18 03:37:20 +0000 | [diff] [blame] | 571 | { |
Greg Clayton | 52ec56c | 2011-10-29 00:57:28 +0000 | [diff] [blame] | 572 | options.insert (opt_defs[i].short_option); |
Greg Clayton | ed8a705 | 2010-09-18 03:37:20 +0000 | [diff] [blame] | 573 | } |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | if (options.empty() == false) |
| 578 | { |
| 579 | // We have some required options with no arguments |
| 580 | strm.PutCString(" [-"); |
| 581 | for (i=0; i<2; ++i) |
| 582 | for (options_pos = options.begin(), options_end = options.end(); |
| 583 | options_pos != options_end; |
| 584 | ++options_pos) |
| 585 | { |
Greg Clayton | 3bcdfc0 | 2012-12-04 00:32:51 +0000 | [diff] [blame] | 586 | if (i==0 && ::islower (*options_pos)) |
Greg Clayton | ed8a705 | 2010-09-18 03:37:20 +0000 | [diff] [blame] | 587 | continue; |
Greg Clayton | 3bcdfc0 | 2012-12-04 00:32:51 +0000 | [diff] [blame] | 588 | if (i==1 && ::isupper (*options_pos)) |
Greg Clayton | ed8a705 | 2010-09-18 03:37:20 +0000 | [diff] [blame] | 589 | continue; |
Greg Clayton | 3bcdfc0 | 2012-12-04 00:32:51 +0000 | [diff] [blame] | 590 | strm << (char)*options_pos; |
Greg Clayton | ed8a705 | 2010-09-18 03:37:20 +0000 | [diff] [blame] | 591 | } |
| 592 | strm.PutChar(']'); |
| 593 | } |
| 594 | |
Caroline Tice | f362c45 | 2010-09-09 16:44:14 +0000 | [diff] [blame] | 595 | // First go through and print the required options (list them up front). |
Jim Ingham | 8651121 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 596 | |
| 597 | for (i = 0; i < num_options; ++i) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 598 | { |
Daniel Malea | 90b0c84 | 2012-12-05 20:24:57 +0000 | [diff] [blame] | 599 | if (opt_defs[i].usage_mask & opt_set_mask && isprint8(opt_defs[i].short_option)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 600 | { |
Virgile Bello | e2607b5 | 2013-09-05 16:42:23 +0000 | [diff] [blame] | 601 | if (opt_defs[i].required && opt_defs[i].option_has_arg != OptionParser::eNoArgument) |
Ed Maste | d78c957 | 2014-04-20 00:31:37 +0000 | [diff] [blame] | 602 | PrintOption (opt_defs[i], eDisplayBestOption, " ", nullptr, true, strm); |
Caroline Tice | f362c45 | 2010-09-09 16:44:14 +0000 | [diff] [blame] | 603 | } |
| 604 | } |
| 605 | |
| 606 | // Now go through again, and this time only print the optional options. |
| 607 | |
| 608 | for (i = 0; i < num_options; ++i) |
| 609 | { |
Greg Clayton | 52ec56c | 2011-10-29 00:57:28 +0000 | [diff] [blame] | 610 | if (opt_defs[i].usage_mask & opt_set_mask) |
Caroline Tice | f362c45 | 2010-09-09 16:44:14 +0000 | [diff] [blame] | 611 | { |
| 612 | // Add current option to the end of out_stream. |
| 613 | |
Virgile Bello | e2607b5 | 2013-09-05 16:42:23 +0000 | [diff] [blame] | 614 | if (!opt_defs[i].required && opt_defs[i].option_has_arg != OptionParser::eNoArgument) |
Ed Maste | d78c957 | 2014-04-20 00:31:37 +0000 | [diff] [blame] | 615 | PrintOption (opt_defs[i], eDisplayBestOption, " ", nullptr, true, strm); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 616 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 617 | } |
Sean Callanan | a4c6ad1 | 2012-01-04 19:11:25 +0000 | [diff] [blame] | 618 | |
Johnny Chen | 34ddc8d | 2012-02-08 01:13:31 +0000 | [diff] [blame] | 619 | if (args_str.GetSize() > 0) |
Sean Callanan | a4c6ad1 | 2012-01-04 19:11:25 +0000 | [diff] [blame] | 620 | { |
| 621 | if (cmd->WantsRawCommandString()) |
| 622 | strm.Printf(" --"); |
| 623 | |
Johnny Chen | 34ddc8d | 2012-02-08 01:13:31 +0000 | [diff] [blame] | 624 | strm.Printf (" %s", args_str.GetData()); |
Sean Callanan | a4c6ad1 | 2012-01-04 19:11:25 +0000 | [diff] [blame] | 625 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 626 | } |
Sean Callanan | a4c6ad1 | 2012-01-04 19:11:25 +0000 | [diff] [blame] | 627 | |
Jim Ingham | 28eb571 | 2012-10-12 17:34:26 +0000 | [diff] [blame] | 628 | if (cmd && |
| 629 | cmd->WantsRawCommandString() && |
Sean Callanan | a4c6ad1 | 2012-01-04 19:11:25 +0000 | [diff] [blame] | 630 | arguments_str.GetSize() > 0) |
| 631 | { |
| 632 | strm.PutChar('\n'); |
| 633 | strm.Indent(name); |
| 634 | strm.Printf(" %s", arguments_str.GetData()); |
| 635 | } |
| 636 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 637 | strm.Printf ("\n\n"); |
| 638 | |
| 639 | // Now print out all the detailed information about the various options: long form, short form and help text: |
Zachary Turner | d37221d | 2014-07-09 16:31:49 +0000 | [diff] [blame] | 640 | // -short <argument> ( --long_name <argument> ) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 641 | // help text |
| 642 | |
| 643 | // This variable is used to keep track of which options' info we've printed out, because some options can be in |
| 644 | // more than one usage level, but we only want to print the long form of its information once. |
| 645 | |
Greg Clayton | 3bcdfc0 | 2012-12-04 00:32:51 +0000 | [diff] [blame] | 646 | std::multimap<int, uint32_t> options_seen; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 647 | strm.IndentMore (5); |
| 648 | |
Caroline Tice | f362c45 | 2010-09-09 16:44:14 +0000 | [diff] [blame] | 649 | // Put the unique command options in a vector & sort it, so we can output them alphabetically (by short_option) |
| 650 | // when writing out detailed help for each option. |
| 651 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 652 | for (i = 0; i < num_options; ++i) |
Greg Clayton | 3bcdfc0 | 2012-12-04 00:32:51 +0000 | [diff] [blame] | 653 | options_seen.insert(std::make_pair(opt_defs[i].short_option, i)); |
Caroline Tice | f362c45 | 2010-09-09 16:44:14 +0000 | [diff] [blame] | 654 | |
| 655 | // Go through the unique'd and alphabetically sorted vector of options, find the table entry for each option |
| 656 | // and write out the detailed help information for that option. |
| 657 | |
Greg Clayton | 3bcdfc0 | 2012-12-04 00:32:51 +0000 | [diff] [blame] | 658 | bool first_option_printed = false;; |
| 659 | |
| 660 | for (auto pos : options_seen) |
Caroline Tice | f362c45 | 2010-09-09 16:44:14 +0000 | [diff] [blame] | 661 | { |
Greg Clayton | 3bcdfc0 | 2012-12-04 00:32:51 +0000 | [diff] [blame] | 662 | i = pos.second; |
| 663 | //Print out the help information for this option. |
| 664 | |
| 665 | // Put a newline separation between arguments |
| 666 | if (first_option_printed) |
| 667 | strm.EOL(); |
| 668 | else |
| 669 | first_option_printed = true; |
| 670 | |
| 671 | CommandArgumentType arg_type = opt_defs[i].argument_type; |
| 672 | |
| 673 | StreamString arg_name_str; |
| 674 | arg_name_str.Printf ("<%s>", CommandObject::GetArgumentName (arg_type)); |
| 675 | |
| 676 | strm.Indent (); |
Daniel Malea | 90b0c84 | 2012-12-05 20:24:57 +0000 | [diff] [blame] | 677 | if (opt_defs[i].short_option && isprint8(opt_defs[i].short_option)) |
Caroline Tice | f362c45 | 2010-09-09 16:44:14 +0000 | [diff] [blame] | 678 | { |
Ed Maste | d78c957 | 2014-04-20 00:31:37 +0000 | [diff] [blame] | 679 | PrintOption (opt_defs[i], eDisplayShortOption, nullptr, nullptr, false, strm); |
Greg Clayton | 3bcdfc0 | 2012-12-04 00:32:51 +0000 | [diff] [blame] | 680 | PrintOption (opt_defs[i], eDisplayLongOption, " ( ", " )", false, strm); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 681 | } |
Greg Clayton | 3bcdfc0 | 2012-12-04 00:32:51 +0000 | [diff] [blame] | 682 | else |
| 683 | { |
| 684 | // Short option is not printable, just print long option |
Ed Maste | d78c957 | 2014-04-20 00:31:37 +0000 | [diff] [blame] | 685 | PrintOption (opt_defs[i], eDisplayLongOption, nullptr, nullptr, false, strm); |
Greg Clayton | 3bcdfc0 | 2012-12-04 00:32:51 +0000 | [diff] [blame] | 686 | } |
| 687 | strm.EOL(); |
| 688 | |
| 689 | strm.IndentMore (5); |
| 690 | |
| 691 | if (opt_defs[i].usage_text) |
| 692 | OutputFormattedUsageText (strm, |
Zachary Turner | d37221d | 2014-07-09 16:31:49 +0000 | [diff] [blame] | 693 | opt_defs[i], |
Greg Clayton | 3bcdfc0 | 2012-12-04 00:32:51 +0000 | [diff] [blame] | 694 | screen_width); |
Ed Maste | d78c957 | 2014-04-20 00:31:37 +0000 | [diff] [blame] | 695 | if (opt_defs[i].enum_values != nullptr) |
Greg Clayton | 3bcdfc0 | 2012-12-04 00:32:51 +0000 | [diff] [blame] | 696 | { |
| 697 | strm.Indent (); |
| 698 | strm.Printf("Values: "); |
Ed Maste | d78c957 | 2014-04-20 00:31:37 +0000 | [diff] [blame] | 699 | for (int k = 0; opt_defs[i].enum_values[k].string_value != nullptr; k++) |
Greg Clayton | 3bcdfc0 | 2012-12-04 00:32:51 +0000 | [diff] [blame] | 700 | { |
| 701 | if (k == 0) |
| 702 | strm.Printf("%s", opt_defs[i].enum_values[k].string_value); |
| 703 | else |
| 704 | strm.Printf(" | %s", opt_defs[i].enum_values[k].string_value); |
| 705 | } |
| 706 | strm.EOL(); |
| 707 | } |
| 708 | strm.IndentLess (5); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | // Restore the indent level |
| 712 | strm.SetIndentLevel (save_indent_level); |
| 713 | } |
| 714 | |
| 715 | // This function is called when we have been given a potentially incomplete set of |
| 716 | // options, such as when an alias has been defined (more options might be added at |
| 717 | // at the time the alias is invoked). We need to verify that the options in the set |
| 718 | // m_seen_options are all part of a set that may be used together, but m_seen_options |
| 719 | // may be missing some of the "required" options. |
| 720 | |
| 721 | bool |
| 722 | Options::VerifyPartialOptions (CommandReturnObject &result) |
| 723 | { |
| 724 | bool options_are_valid = false; |
| 725 | |
Jim Ingham | d6ccc60 | 2010-06-24 20:30:15 +0000 | [diff] [blame] | 726 | int num_levels = GetRequiredOptions().size(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 727 | if (num_levels) |
| 728 | { |
| 729 | for (int i = 0; i < num_levels && !options_are_valid; ++i) |
| 730 | { |
| 731 | // In this case we are treating all options as optional rather than required. |
| 732 | // Therefore a set of options is correct if m_seen_options is a subset of the |
| 733 | // union of m_required_options and m_optional_options. |
| 734 | OptionSet union_set; |
Jim Ingham | d6ccc60 | 2010-06-24 20:30:15 +0000 | [diff] [blame] | 735 | OptionsSetUnion (GetRequiredOptions()[i], GetOptionalOptions()[i], union_set); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 736 | if (IsASubset (m_seen_options, union_set)) |
| 737 | options_are_valid = true; |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | return options_are_valid; |
| 742 | } |
| 743 | |
| 744 | bool |
| 745 | Options::HandleOptionCompletion |
| 746 | ( |
| 747 | Args &input, |
| 748 | OptionElementVector &opt_element_vector, |
| 749 | int cursor_index, |
| 750 | int char_pos, |
| 751 | int match_start_point, |
| 752 | int max_return_elements, |
Jim Ingham | 558ce12 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 753 | bool &word_complete, |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 754 | lldb_private::StringList &matches |
| 755 | ) |
| 756 | { |
Jim Ingham | 558ce12 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 757 | word_complete = true; |
| 758 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 759 | // For now we just scan the completions to see if the cursor position is in |
| 760 | // an option or its argument. Otherwise we'll call HandleArgumentCompletion. |
| 761 | // In the future we can use completion to validate options as well if we want. |
| 762 | |
| 763 | const OptionDefinition *opt_defs = GetDefinitions(); |
| 764 | |
| 765 | std::string cur_opt_std_str (input.GetArgumentAtIndex(cursor_index)); |
| 766 | cur_opt_std_str.erase(char_pos); |
| 767 | const char *cur_opt_str = cur_opt_std_str.c_str(); |
| 768 | |
Andy Gibbs | a297a97 | 2013-06-19 19:04:53 +0000 | [diff] [blame] | 769 | for (size_t i = 0; i < opt_element_vector.size(); i++) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 770 | { |
| 771 | int opt_pos = opt_element_vector[i].opt_pos; |
| 772 | int opt_arg_pos = opt_element_vector[i].opt_arg_pos; |
| 773 | int opt_defs_index = opt_element_vector[i].opt_defs_index; |
| 774 | if (opt_pos == cursor_index) |
| 775 | { |
| 776 | // We're completing the option itself. |
Jim Ingham | d6ccc60 | 2010-06-24 20:30:15 +0000 | [diff] [blame] | 777 | |
| 778 | if (opt_defs_index == OptionArgElement::eBareDash) |
| 779 | { |
| 780 | // We're completing a bare dash. That means all options are open. |
| 781 | // FIXME: We should scan the other options provided and only complete options |
| 782 | // within the option group they belong to. |
| 783 | char opt_str[3] = {'-', 'a', '\0'}; |
| 784 | |
Greg Clayton | b132097 | 2010-07-14 00:18:15 +0000 | [diff] [blame] | 785 | for (int j = 0 ; opt_defs[j].short_option != 0 ; j++) |
Jim Ingham | d6ccc60 | 2010-06-24 20:30:15 +0000 | [diff] [blame] | 786 | { |
Greg Clayton | b132097 | 2010-07-14 00:18:15 +0000 | [diff] [blame] | 787 | opt_str[1] = opt_defs[j].short_option; |
Jim Ingham | d6ccc60 | 2010-06-24 20:30:15 +0000 | [diff] [blame] | 788 | matches.AppendString (opt_str); |
| 789 | } |
| 790 | return true; |
| 791 | } |
| 792 | else if (opt_defs_index == OptionArgElement::eBareDoubleDash) |
| 793 | { |
| 794 | std::string full_name ("--"); |
Greg Clayton | b132097 | 2010-07-14 00:18:15 +0000 | [diff] [blame] | 795 | for (int j = 0 ; opt_defs[j].short_option != 0 ; j++) |
Jim Ingham | d6ccc60 | 2010-06-24 20:30:15 +0000 | [diff] [blame] | 796 | { |
| 797 | full_name.erase(full_name.begin() + 2, full_name.end()); |
Greg Clayton | b132097 | 2010-07-14 00:18:15 +0000 | [diff] [blame] | 798 | full_name.append (opt_defs[j].long_option); |
Jim Ingham | d6ccc60 | 2010-06-24 20:30:15 +0000 | [diff] [blame] | 799 | matches.AppendString (full_name.c_str()); |
| 800 | } |
| 801 | return true; |
| 802 | } |
| 803 | else if (opt_defs_index != OptionArgElement::eUnrecognizedArg) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 804 | { |
Greg Clayton | b7ad58a | 2013-04-04 20:35:24 +0000 | [diff] [blame] | 805 | // We recognized it, if it an incomplete long option, complete it anyway (getopt_long_only is |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 806 | // happy with shortest unique string, but it's still a nice thing to do.) Otherwise return |
| 807 | // The string so the upper level code will know this is a full match and add the " ". |
| 808 | if (cur_opt_str && strlen (cur_opt_str) > 2 |
| 809 | && cur_opt_str[0] == '-' && cur_opt_str[1] == '-' |
| 810 | && strcmp (opt_defs[opt_defs_index].long_option, cur_opt_str) != 0) |
| 811 | { |
| 812 | std::string full_name ("--"); |
| 813 | full_name.append (opt_defs[opt_defs_index].long_option); |
| 814 | matches.AppendString(full_name.c_str()); |
| 815 | return true; |
| 816 | } |
| 817 | else |
| 818 | { |
| 819 | matches.AppendString(input.GetArgumentAtIndex(cursor_index)); |
| 820 | return true; |
| 821 | } |
| 822 | } |
| 823 | else |
| 824 | { |
| 825 | // FIXME - not handling wrong options yet: |
| 826 | // Check to see if they are writing a long option & complete it. |
| 827 | // I think we will only get in here if the long option table has two elements |
Greg Clayton | b7ad58a | 2013-04-04 20:35:24 +0000 | [diff] [blame] | 828 | // that are not unique up to this point. getopt_long_only does shortest unique match |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 829 | // for long options already. |
| 830 | |
| 831 | if (cur_opt_str && strlen (cur_opt_str) > 2 |
| 832 | && cur_opt_str[0] == '-' && cur_opt_str[1] == '-') |
| 833 | { |
Greg Clayton | b132097 | 2010-07-14 00:18:15 +0000 | [diff] [blame] | 834 | for (int j = 0 ; opt_defs[j].short_option != 0 ; j++) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 835 | { |
Greg Clayton | b132097 | 2010-07-14 00:18:15 +0000 | [diff] [blame] | 836 | if (strstr(opt_defs[j].long_option, cur_opt_str + 2) == opt_defs[j].long_option) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 837 | { |
| 838 | std::string full_name ("--"); |
Greg Clayton | b132097 | 2010-07-14 00:18:15 +0000 | [diff] [blame] | 839 | full_name.append (opt_defs[j].long_option); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 840 | // The options definitions table has duplicates because of the |
| 841 | // way the grouping information is stored, so only add once. |
| 842 | bool duplicate = false; |
Andy Gibbs | a297a97 | 2013-06-19 19:04:53 +0000 | [diff] [blame] | 843 | for (size_t k = 0; k < matches.GetSize(); k++) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 844 | { |
Greg Clayton | b132097 | 2010-07-14 00:18:15 +0000 | [diff] [blame] | 845 | if (matches.GetStringAtIndex(k) == full_name) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 846 | { |
| 847 | duplicate = true; |
| 848 | break; |
| 849 | } |
| 850 | } |
| 851 | if (!duplicate) |
| 852 | matches.AppendString(full_name.c_str()); |
| 853 | } |
| 854 | } |
| 855 | } |
| 856 | return true; |
| 857 | } |
| 858 | |
| 859 | |
| 860 | } |
| 861 | else if (opt_arg_pos == cursor_index) |
| 862 | { |
| 863 | // Okay the cursor is on the completion of an argument. |
| 864 | // See if it has a completion, otherwise return no matches. |
| 865 | |
| 866 | if (opt_defs_index != -1) |
| 867 | { |
Greg Clayton | eb0103f | 2011-04-07 22:46:35 +0000 | [diff] [blame] | 868 | HandleOptionArgumentCompletion (input, |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 869 | cursor_index, |
| 870 | strlen (input.GetArgumentAtIndex(cursor_index)), |
| 871 | opt_element_vector, |
| 872 | i, |
| 873 | match_start_point, |
| 874 | max_return_elements, |
Jim Ingham | 558ce12 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 875 | word_complete, |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 876 | matches); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 877 | return true; |
| 878 | } |
| 879 | else |
| 880 | { |
| 881 | // No completion callback means no completions... |
| 882 | return true; |
| 883 | } |
| 884 | |
| 885 | } |
| 886 | else |
| 887 | { |
| 888 | // Not the last element, keep going. |
| 889 | continue; |
| 890 | } |
| 891 | } |
| 892 | return false; |
| 893 | } |
| 894 | |
| 895 | bool |
| 896 | Options::HandleOptionArgumentCompletion |
| 897 | ( |
| 898 | Args &input, |
| 899 | int cursor_index, |
| 900 | int char_pos, |
| 901 | OptionElementVector &opt_element_vector, |
| 902 | int opt_element_index, |
| 903 | int match_start_point, |
| 904 | int max_return_elements, |
Jim Ingham | 558ce12 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 905 | bool &word_complete, |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 906 | lldb_private::StringList &matches |
| 907 | ) |
| 908 | { |
| 909 | const OptionDefinition *opt_defs = GetDefinitions(); |
Greg Clayton | 7b0992d | 2013-04-18 22:45:39 +0000 | [diff] [blame] | 910 | std::unique_ptr<SearchFilter> filter_ap; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 911 | |
| 912 | int opt_arg_pos = opt_element_vector[opt_element_index].opt_arg_pos; |
| 913 | int opt_defs_index = opt_element_vector[opt_element_index].opt_defs_index; |
| 914 | |
| 915 | // See if this is an enumeration type option, and if so complete it here: |
| 916 | |
| 917 | OptionEnumValueElement *enum_values = opt_defs[opt_defs_index].enum_values; |
Ed Maste | d78c957 | 2014-04-20 00:31:37 +0000 | [diff] [blame] | 918 | if (enum_values != nullptr) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 919 | { |
| 920 | bool return_value = false; |
| 921 | std::string match_string(input.GetArgumentAtIndex (opt_arg_pos), input.GetArgumentAtIndex (opt_arg_pos) + char_pos); |
Ed Maste | d78c957 | 2014-04-20 00:31:37 +0000 | [diff] [blame] | 922 | for (int i = 0; enum_values[i].string_value != nullptr; i++) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 923 | { |
| 924 | if (strstr(enum_values[i].string_value, match_string.c_str()) == enum_values[i].string_value) |
| 925 | { |
| 926 | matches.AppendString (enum_values[i].string_value); |
| 927 | return_value = true; |
| 928 | } |
| 929 | } |
| 930 | return return_value; |
| 931 | } |
| 932 | |
| 933 | // If this is a source file or symbol type completion, and there is a |
| 934 | // -shlib option somewhere in the supplied arguments, then make a search filter |
| 935 | // for that shared library. |
| 936 | // FIXME: Do we want to also have an "OptionType" so we don't have to match string names? |
| 937 | |
Greg Clayton | ab65b34 | 2011-04-13 22:47:15 +0000 | [diff] [blame] | 938 | uint32_t completion_mask = opt_defs[opt_defs_index].completion_type; |
| 939 | |
| 940 | if (completion_mask == 0) |
| 941 | { |
| 942 | lldb::CommandArgumentType option_arg_type = opt_defs[opt_defs_index].argument_type; |
| 943 | if (option_arg_type != eArgTypeNone) |
| 944 | { |
Vince Harron | d7e6a4f | 2015-05-13 00:25:54 +0000 | [diff] [blame] | 945 | const CommandObject::ArgumentTableEntry *arg_entry = CommandObject::FindArgumentDataByType (opt_defs[opt_defs_index].argument_type); |
Greg Clayton | ab65b34 | 2011-04-13 22:47:15 +0000 | [diff] [blame] | 946 | if (arg_entry) |
| 947 | completion_mask = arg_entry->completion_type; |
| 948 | } |
| 949 | } |
| 950 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 951 | if (completion_mask & CommandCompletions::eSourceFileCompletion |
| 952 | || completion_mask & CommandCompletions::eSymbolCompletion) |
| 953 | { |
Andy Gibbs | a297a97 | 2013-06-19 19:04:53 +0000 | [diff] [blame] | 954 | for (size_t i = 0; i < opt_element_vector.size(); i++) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 955 | { |
| 956 | int cur_defs_index = opt_element_vector[i].opt_defs_index; |
| 957 | int cur_arg_pos = opt_element_vector[i].opt_arg_pos; |
| 958 | const char *cur_opt_name = opt_defs[cur_defs_index].long_option; |
| 959 | |
| 960 | // If this is the "shlib" option and there was an argument provided, |
| 961 | // restrict it to that shared library. |
Greg Clayton | 99dcbe1 | 2014-01-29 18:25:07 +0000 | [diff] [blame] | 962 | if (cur_opt_name && strcmp(cur_opt_name, "shlib") == 0 && cur_arg_pos != -1) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 963 | { |
| 964 | const char *module_name = input.GetArgumentAtIndex(cur_arg_pos); |
| 965 | if (module_name) |
| 966 | { |
Greg Clayton | 274060b | 2010-10-20 20:54:39 +0000 | [diff] [blame] | 967 | FileSpec module_spec(module_name, false); |
Greg Clayton | eb0103f | 2011-04-07 22:46:35 +0000 | [diff] [blame] | 968 | lldb::TargetSP target_sp = m_interpreter.GetDebugger().GetSelectedTarget(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 969 | // Search filters require a target... |
Greg Clayton | 4d122c4 | 2011-09-17 08:33:22 +0000 | [diff] [blame] | 970 | if (target_sp) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 971 | filter_ap.reset (new SearchFilterByModule (target_sp, module_spec)); |
| 972 | } |
| 973 | break; |
| 974 | } |
| 975 | } |
| 976 | } |
| 977 | |
Greg Clayton | eb0103f | 2011-04-07 22:46:35 +0000 | [diff] [blame] | 978 | return CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter, |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 979 | completion_mask, |
| 980 | input.GetArgumentAtIndex (opt_arg_pos), |
| 981 | match_start_point, |
| 982 | max_return_elements, |
| 983 | filter_ap.get(), |
Jim Ingham | 558ce12 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 984 | word_complete, |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 985 | matches); |
| 986 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 987 | } |
Greg Clayton | f6b8b58 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 988 | |
| 989 | |
Greg Clayton | f6b8b58 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 990 | void |
Greg Clayton | 84c3966 | 2011-04-27 22:04:39 +0000 | [diff] [blame] | 991 | OptionGroupOptions::Append (OptionGroup* group) |
| 992 | { |
| 993 | const OptionDefinition* group_option_defs = group->GetDefinitions (); |
| 994 | const uint32_t group_option_count = group->GetNumDefinitions(); |
| 995 | for (uint32_t i=0; i<group_option_count; ++i) |
| 996 | { |
| 997 | m_option_infos.push_back (OptionInfo (group, i)); |
| 998 | m_option_defs.push_back (group_option_defs[i]); |
| 999 | } |
| 1000 | } |
| 1001 | |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1002 | const OptionGroup* |
| 1003 | OptionGroupOptions::GetGroupWithOption (char short_opt) |
| 1004 | { |
| 1005 | for (uint32_t i = 0; i < m_option_defs.size(); i++) |
| 1006 | { |
| 1007 | OptionDefinition opt_def = m_option_defs[i]; |
| 1008 | if (opt_def.short_option == short_opt) |
| 1009 | return m_option_infos[i].option_group; |
| 1010 | } |
Ed Maste | d78c957 | 2014-04-20 00:31:37 +0000 | [diff] [blame] | 1011 | return nullptr; |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1012 | } |
| 1013 | |
Greg Clayton | 84c3966 | 2011-04-27 22:04:39 +0000 | [diff] [blame] | 1014 | void |
Greg Clayton | ab65b34 | 2011-04-13 22:47:15 +0000 | [diff] [blame] | 1015 | OptionGroupOptions::Append (OptionGroup* group, |
| 1016 | uint32_t src_mask, |
| 1017 | uint32_t dst_mask) |
Greg Clayton | f6b8b58 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 1018 | { |
Greg Clayton | f6b8b58 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 1019 | const OptionDefinition* group_option_defs = group->GetDefinitions (); |
| 1020 | const uint32_t group_option_count = group->GetNumDefinitions(); |
| 1021 | for (uint32_t i=0; i<group_option_count; ++i) |
| 1022 | { |
Greg Clayton | ab65b34 | 2011-04-13 22:47:15 +0000 | [diff] [blame] | 1023 | if (group_option_defs[i].usage_mask & src_mask) |
| 1024 | { |
| 1025 | m_option_infos.push_back (OptionInfo (group, i)); |
| 1026 | m_option_defs.push_back (group_option_defs[i]); |
| 1027 | m_option_defs.back().usage_mask = dst_mask; |
| 1028 | } |
Greg Clayton | f6b8b58 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 1029 | } |
| 1030 | } |
| 1031 | |
| 1032 | void |
| 1033 | OptionGroupOptions::Finalize () |
| 1034 | { |
| 1035 | m_did_finalize = true; |
Zachary Turner | d37221d | 2014-07-09 16:31:49 +0000 | [diff] [blame] | 1036 | OptionDefinition empty_option_def = { 0, false, nullptr, 0, 0, nullptr, nullptr, 0, eArgTypeNone, nullptr }; |
Greg Clayton | f6b8b58 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 1037 | m_option_defs.push_back (empty_option_def); |
| 1038 | } |
| 1039 | |
| 1040 | Error |
| 1041 | OptionGroupOptions::SetOptionValue (uint32_t option_idx, |
| 1042 | const char *option_value) |
| 1043 | { |
| 1044 | // After calling OptionGroupOptions::Append(...), you must finalize the groups |
| 1045 | // by calling OptionGroupOptions::Finlize() |
| 1046 | assert (m_did_finalize); |
Greg Clayton | ab65b34 | 2011-04-13 22:47:15 +0000 | [diff] [blame] | 1047 | assert (m_option_infos.size() + 1 == m_option_defs.size()); |
Greg Clayton | f6b8b58 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 1048 | Error error; |
Greg Clayton | ab65b34 | 2011-04-13 22:47:15 +0000 | [diff] [blame] | 1049 | if (option_idx < m_option_infos.size()) |
| 1050 | { |
| 1051 | error = m_option_infos[option_idx].option_group->SetOptionValue (m_interpreter, |
| 1052 | m_option_infos[option_idx].option_index, |
| 1053 | option_value); |
| 1054 | |
| 1055 | } |
| 1056 | else |
| 1057 | { |
| 1058 | error.SetErrorString ("invalid option index"); // Shouldn't happen... |
| 1059 | } |
Greg Clayton | f6b8b58 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 1060 | return error; |
| 1061 | } |
| 1062 | |
| 1063 | void |
| 1064 | OptionGroupOptions::OptionParsingStarting () |
| 1065 | { |
Greg Clayton | ab65b34 | 2011-04-13 22:47:15 +0000 | [diff] [blame] | 1066 | std::set<OptionGroup*> group_set; |
| 1067 | OptionInfos::iterator pos, end = m_option_infos.end(); |
| 1068 | for (pos = m_option_infos.begin(); pos != end; ++pos) |
| 1069 | { |
| 1070 | OptionGroup* group = pos->option_group; |
| 1071 | if (group_set.find(group) == group_set.end()) |
| 1072 | { |
| 1073 | group->OptionParsingStarting (m_interpreter); |
| 1074 | group_set.insert(group); |
| 1075 | } |
| 1076 | } |
Greg Clayton | f6b8b58 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 1077 | } |
| 1078 | Error |
| 1079 | OptionGroupOptions::OptionParsingFinished () |
| 1080 | { |
Greg Clayton | ab65b34 | 2011-04-13 22:47:15 +0000 | [diff] [blame] | 1081 | std::set<OptionGroup*> group_set; |
Greg Clayton | f6b8b58 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 1082 | Error error; |
Greg Clayton | ab65b34 | 2011-04-13 22:47:15 +0000 | [diff] [blame] | 1083 | OptionInfos::iterator pos, end = m_option_infos.end(); |
| 1084 | for (pos = m_option_infos.begin(); pos != end; ++pos) |
Greg Clayton | f6b8b58 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 1085 | { |
Greg Clayton | ab65b34 | 2011-04-13 22:47:15 +0000 | [diff] [blame] | 1086 | OptionGroup* group = pos->option_group; |
| 1087 | if (group_set.find(group) == group_set.end()) |
| 1088 | { |
| 1089 | error = group->OptionParsingFinished (m_interpreter); |
| 1090 | group_set.insert(group); |
| 1091 | if (error.Fail()) |
| 1092 | return error; |
| 1093 | } |
Greg Clayton | f6b8b58 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 1094 | } |
| 1095 | return error; |
| 1096 | } |