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