| 1 |
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying |
| 2 |
file Copyright.txt or https://cmake.org/licensing#kwsys for details. */ |
| 3 |
#include "kwsysPrivate.h" |
| 4 |
#include KWSYS_HEADER(CommandLineArguments.hxx) |
| 5 |
|
| 6 |
#include KWSYS_HEADER(Configure.hxx) |
| 7 |
|
| 8 |
// Work-around CMake dependency scanning limitation. This must |
| 9 |
// duplicate the above list of headers. |
| 10 |
#if 0 |
| 11 |
# include "CommandLineArguments.hxx.in" |
| 12 |
# include "Configure.hxx.in" |
| 13 |
#endif |
| 14 |
|
| 15 |
#include <iostream> |
| 16 |
#include <map> |
| 17 |
#include <set> |
| 18 |
#include <sstream> |
| 19 |
#include <string> |
| 20 |
#include <vector> |
| 21 |
|
| 22 |
#include <cstdio> |
| 23 |
#include <cstdlib> |
| 24 |
#include <cstring> |
| 25 |
|
| 26 |
#ifdef _MSC_VER |
| 27 |
# pragma warning(disable : 4786) |
| 28 |
#endif |
| 29 |
|
| 30 |
#if defined(__sgi) && !defined(__GNUC__) |
| 31 |
# pragma set woff 1375 /* base class destructor not virtual */ |
| 32 |
#endif |
| 33 |
|
| 34 |
#if 0 |
| 35 |
# define CommandLineArguments_DEBUG(x) \ |
| 36 |
std::cout << __LINE__ << " CLA: " << x << std::endl |
| 37 |
#else |
| 38 |
# define CommandLineArguments_DEBUG(x) |
| 39 |
#endif |
| 40 |
|
| 41 |
namespace KWSYS_NAMESPACE { |
| 42 |
|
| 43 |
struct CommandLineArgumentsCallbackStructure |
| 44 |
{ |
| 45 |
const char* Argument; |
| 46 |
int ArgumentType; |
| 47 |
CommandLineArguments::CallbackType Callback; |
| 48 |
void* CallData; |
| 49 |
void* Variable; |
| 50 |
int VariableType; |
| 51 |
const char* Help; |
| 52 |
}; |
| 53 |
|
| 54 |
class CommandLineArgumentsVectorOfStrings : public std::vector<std::string> |
| 55 |
{ |
| 56 |
}; |
| 57 |
class CommandLineArgumentsSetOfStrings : public std::set<std::string> |
| 58 |
{ |
| 59 |
}; |
| 60 |
class CommandLineArgumentsMapOfStrucs |
| 61 |
: public std::map<std::string, CommandLineArgumentsCallbackStructure> |
| 62 |
{ |
| 63 |
}; |
| 64 |
|
| 65 |
class CommandLineArgumentsInternal |
| 66 |
{ |
| 67 |
public: |
| 68 |
CommandLineArgumentsInternal() = default; |
| 69 |
|
| 70 |
using VectorOfStrings = CommandLineArgumentsVectorOfStrings; |
| 71 |
using CallbacksMap = CommandLineArgumentsMapOfStrucs; |
| 72 |
using String = std::string; |
| 73 |
using SetOfStrings = CommandLineArgumentsSetOfStrings; |
| 74 |
|
| 75 |
VectorOfStrings Argv; |
| 76 |
String Argv0; |
| 77 |
CallbacksMap Callbacks; |
| 78 |
|
| 79 |
CommandLineArguments::ErrorCallbackType UnknownArgumentCallback{ nullptr }; |
| 80 |
void* ClientData{ nullptr }; |
| 81 |
|
| 82 |
VectorOfStrings::size_type LastArgument{ 0 }; |
| 83 |
|
| 84 |
VectorOfStrings UnusedArguments; |
| 85 |
}; |
| 86 |
|
| 87 |
CommandLineArguments::CommandLineArguments() |
| 88 |
{ |
| 89 |
this->Internals = new CommandLineArguments::Internal; |
| 90 |
this->Help = ""; |
| 91 |
this->LineLength = 80; |
| 92 |
this->StoreUnusedArgumentsFlag = false; |
| 93 |
} |
| 94 |
|
| 95 |
CommandLineArguments::~CommandLineArguments() |
| 96 |
{ |
| 97 |
delete this->Internals; |
| 98 |
} |
| 99 |
|
| 100 |
void CommandLineArguments::Initialize(int argc, const char* const argv[]) |
| 101 |
{ |
| 102 |
int cc; |
| 103 |
|
| 104 |
this->Initialize(); |
| 105 |
this->Internals->Argv0 = argv[0]; |
| 106 |
for (cc = 1; cc < argc; cc++) { |
| 107 |
this->ProcessArgument(argv[cc]); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
void CommandLineArguments::Initialize(int argc, char* argv[]) |
| 112 |
{ |
| 113 |
this->Initialize(argc, static_cast<const char* const*>(argv)); |
| 114 |
} |
| 115 |
|
| 116 |
void CommandLineArguments::Initialize() |
| 117 |
{ |
| 118 |
this->Internals->Argv.clear(); |
| 119 |
this->Internals->LastArgument = 0; |
| 120 |
} |
| 121 |
|
| 122 |
void CommandLineArguments::ProcessArgument(const char* arg) |
| 123 |
{ |
| 124 |
this->Internals->Argv.push_back(arg); |
| 125 |
} |
| 126 |
|
| 127 |
bool CommandLineArguments::GetMatchedArguments( |
| 128 |
std::vector<std::string>* matches, const std::string& arg) |
| 129 |
{ |
| 130 |
matches->clear(); |
| 131 |
CommandLineArguments::Internal::CallbacksMap::iterator it; |
| 132 |
|
| 133 |
// Does the argument match to any we know about? |
| 134 |
for (it = this->Internals->Callbacks.begin(); |
| 135 |
it != this->Internals->Callbacks.end(); it++) { |
| 136 |
const CommandLineArguments::Internal::String& parg = it->first; |
| 137 |
CommandLineArgumentsCallbackStructure* cs = &it->second; |
| 138 |
if (cs->ArgumentType == CommandLineArguments::NO_ARGUMENT || |
| 139 |
cs->ArgumentType == CommandLineArguments::SPACE_ARGUMENT) { |
| 140 |
if (arg == parg) { |
| 141 |
matches->push_back(parg); |
| 142 |
} |
| 143 |
} else if (arg.find(parg) == 0) { |
| 144 |
matches->push_back(parg); |
| 145 |
} |
| 146 |
} |
| 147 |
return !matches->empty(); |
| 148 |
} |
| 149 |
|
| 150 |
int CommandLineArguments::Parse() |
| 151 |
{ |
| 152 |
std::vector<std::string>::size_type cc; |
| 153 |
std::vector<std::string> matches; |
| 154 |
if (this->StoreUnusedArgumentsFlag) { |
| 155 |
this->Internals->UnusedArguments.clear(); |
| 156 |
} |
| 157 |
for (cc = 0; cc < this->Internals->Argv.size(); cc++) { |
| 158 |
const std::string& arg = this->Internals->Argv[cc]; |
| 159 |
CommandLineArguments_DEBUG("Process argument: " << arg); |
| 160 |
this->Internals->LastArgument = cc; |
| 161 |
if (this->GetMatchedArguments(&matches, arg)) { |
| 162 |
// Ok, we found one or more arguments that match what user specified. |
| 163 |
// Let's find the longest one. |
| 164 |
CommandLineArguments::Internal::VectorOfStrings::size_type kk; |
| 165 |
CommandLineArguments::Internal::VectorOfStrings::size_type maxidx = 0; |
| 166 |
CommandLineArguments::Internal::String::size_type maxlen = 0; |
| 167 |
for (kk = 0; kk < matches.size(); kk++) { |
| 168 |
if (matches[kk].size() > maxlen) { |
| 169 |
maxlen = matches[kk].size(); |
| 170 |
maxidx = kk; |
| 171 |
} |
| 172 |
} |
| 173 |
// So, the longest one is probably the right one. Now see if it has any |
| 174 |
// additional value |
| 175 |
CommandLineArgumentsCallbackStructure* cs = |
| 176 |
&this->Internals->Callbacks[matches[maxidx]]; |
| 177 |
const std::string& sarg = matches[maxidx]; |
| 178 |
if (cs->Argument != sarg) { |
| 179 |
abort(); |
| 180 |
} |
| 181 |
switch (cs->ArgumentType) { |
| 182 |
case NO_ARGUMENT: |
| 183 |
// No value |
| 184 |
if (!this->PopulateVariable(cs, nullptr)) { |
| 185 |
return 0; |
| 186 |
} |
| 187 |
break; |
| 188 |
case SPACE_ARGUMENT: |
| 189 |
if (cc == this->Internals->Argv.size() - 1) { |
| 190 |
this->Internals->LastArgument--; |
| 191 |
return 0; |
| 192 |
} |
| 193 |
CommandLineArguments_DEBUG("This is a space argument: " |
| 194 |
<< arg << " value: " |
| 195 |
<< this->Internals->Argv[cc + 1]); |
| 196 |
// Value is the next argument |
| 197 |
if (!this->PopulateVariable(cs, |
| 198 |
this->Internals->Argv[cc + 1].c_str())) { |
| 199 |
return 0; |
| 200 |
} |
| 201 |
cc++; |
| 202 |
break; |
| 203 |
case EQUAL_ARGUMENT: |
| 204 |
if (arg.size() == sarg.size() || arg.at(sarg.size()) != '=') { |
| 205 |
this->Internals->LastArgument--; |
| 206 |
return 0; |
| 207 |
} |
| 208 |
// Value is everythng followed the '=' sign |
| 209 |
if (!this->PopulateVariable(cs, arg.c_str() + sarg.size() + 1)) { |
| 210 |
return 0; |
| 211 |
} |
| 212 |
break; |
| 213 |
case CONCAT_ARGUMENT: |
| 214 |
// Value is whatever follows the argument |
| 215 |
if (!this->PopulateVariable(cs, arg.c_str() + sarg.size())) { |
| 216 |
return 0; |
| 217 |
} |
| 218 |
break; |
| 219 |
case MULTI_ARGUMENT: |
| 220 |
// Suck in all the rest of the arguments |
| 221 |
CommandLineArguments_DEBUG("This is a multi argument: " << arg); |
| 222 |
for (cc++; cc < this->Internals->Argv.size(); ++cc) { |
| 223 |
const std::string& marg = this->Internals->Argv[cc]; |
| 224 |
CommandLineArguments_DEBUG( |
| 225 |
" check multi argument value: " << marg); |
| 226 |
if (this->GetMatchedArguments(&matches, marg)) { |
| 227 |
CommandLineArguments_DEBUG("End of multi argument " |
| 228 |
<< arg << " with value: " << marg); |
| 229 |
break; |
| 230 |
} |
| 231 |
CommandLineArguments_DEBUG( |
| 232 |
" populate multi argument value: " << marg); |
| 233 |
if (!this->PopulateVariable(cs, marg.c_str())) { |
| 234 |
return 0; |
| 235 |
} |
| 236 |
} |
| 237 |
if (cc != this->Internals->Argv.size()) { |
| 238 |
CommandLineArguments_DEBUG("Again End of multi argument " << arg); |
| 239 |
cc--; |
| 240 |
continue; |
| 241 |
} |
| 242 |
break; |
| 243 |
default: |
| 244 |
std::cerr << "Got unknown argument type: \"" << cs->ArgumentType |
| 245 |
<< "\"" << std::endl; |
| 246 |
this->Internals->LastArgument--; |
| 247 |
return 0; |
| 248 |
} |
| 249 |
} else { |
| 250 |
// Handle unknown arguments |
| 251 |
if (this->Internals->UnknownArgumentCallback) { |
| 252 |
if (!this->Internals->UnknownArgumentCallback( |
| 253 |
arg.c_str(), this->Internals->ClientData)) { |
| 254 |
this->Internals->LastArgument--; |
| 255 |
return 0; |
| 256 |
} |
| 257 |
return 1; |
| 258 |
} else if (this->StoreUnusedArgumentsFlag) { |
| 259 |
CommandLineArguments_DEBUG("Store unused argument " << arg); |
| 260 |
this->Internals->UnusedArguments.push_back(arg); |
| 261 |
} else { |
| 262 |
std::cerr << "Got unknown argument: \"" << arg << "\"" << std::endl; |
| 263 |
this->Internals->LastArgument--; |
| 264 |
return 0; |
| 265 |
} |
| 266 |
} |
| 267 |
} |
| 268 |
return 1; |
| 269 |
} |
| 270 |
|
| 271 |
void CommandLineArguments::GetRemainingArguments(int* argc, char*** argv) |
| 272 |
{ |
| 273 |
CommandLineArguments::Internal::VectorOfStrings::size_type size = |
| 274 |
this->Internals->Argv.size() - this->Internals->LastArgument + 1; |
| 275 |
CommandLineArguments::Internal::VectorOfStrings::size_type cc; |
| 276 |
|
| 277 |
// Copy Argv0 as the first argument |
| 278 |
char** args = new char*[size]; |
| 279 |
args[0] = new char[this->Internals->Argv0.size() + 1]; |
| 280 |
strcpy(args[0], this->Internals->Argv0.c_str()); |
| 281 |
int cnt = 1; |
| 282 |
|
| 283 |
// Copy everything after the LastArgument, since that was not parsed. |
| 284 |
for (cc = this->Internals->LastArgument + 1; |
| 285 |
cc < this->Internals->Argv.size(); cc++) { |
| 286 |
args[cnt] = new char[this->Internals->Argv[cc].size() + 1]; |
| 287 |
strcpy(args[cnt], this->Internals->Argv[cc].c_str()); |
| 288 |
cnt++; |
| 289 |
} |
| 290 |
*argc = cnt; |
| 291 |
*argv = args; |
| 292 |
} |
| 293 |
|
| 294 |
void CommandLineArguments::GetUnusedArguments(int* argc, char*** argv) |
| 295 |
{ |
| 296 |
CommandLineArguments::Internal::VectorOfStrings::size_type size = |
| 297 |
this->Internals->UnusedArguments.size() + 1; |
| 298 |
CommandLineArguments::Internal::VectorOfStrings::size_type cc; |
| 299 |
|
| 300 |
// Copy Argv0 as the first argument |
| 301 |
char** args = new char*[size]; |
| 302 |
args[0] = new char[this->Internals->Argv0.size() + 1]; |
| 303 |
strcpy(args[0], this->Internals->Argv0.c_str()); |
| 304 |
int cnt = 1; |
| 305 |
|
| 306 |
// Copy everything after the LastArgument, since that was not parsed. |
| 307 |
for (cc = 0; cc < this->Internals->UnusedArguments.size(); cc++) { |
| 308 |
std::string& str = this->Internals->UnusedArguments[cc]; |
| 309 |
args[cnt] = new char[str.size() + 1]; |
| 310 |
strcpy(args[cnt], str.c_str()); |
| 311 |
cnt++; |
| 312 |
} |
| 313 |
*argc = cnt; |
| 314 |
*argv = args; |
| 315 |
} |
| 316 |
|
| 317 |
void CommandLineArguments::DeleteRemainingArguments(int argc, char*** argv) |
| 318 |
{ |
| 319 |
int cc; |
| 320 |
for (cc = 0; cc < argc; ++cc) { |
| 321 |
delete[] (*argv)[cc]; |
| 322 |
} |
| 323 |
delete[] * argv; |
| 324 |
} |
| 325 |
|
| 326 |
void CommandLineArguments::AddCallback(const char* argument, |
| 327 |
ArgumentTypeEnum type, |
| 328 |
CallbackType callback, void* call_data, |
| 329 |
const char* help) |
| 330 |
{ |
| 331 |
CommandLineArgumentsCallbackStructure s; |
| 332 |
s.Argument = argument; |
| 333 |
s.ArgumentType = type; |
| 334 |
s.Callback = callback; |
| 335 |
s.CallData = call_data; |
| 336 |
s.VariableType = CommandLineArguments::NO_VARIABLE_TYPE; |
| 337 |
s.Variable = nullptr; |
| 338 |
s.Help = help; |
| 339 |
|
| 340 |
this->Internals->Callbacks[argument] = s; |
| 341 |
this->GenerateHelp(); |
| 342 |
} |
| 343 |
|
| 344 |
void CommandLineArguments::AddArgument(const char* argument, |
| 345 |
ArgumentTypeEnum type, |
| 346 |
VariableTypeEnum vtype, void* variable, |
| 347 |
const char* help) |
| 348 |
{ |
| 349 |
CommandLineArgumentsCallbackStructure s; |
| 350 |
s.Argument = argument; |
| 351 |
s.ArgumentType = type; |
| 352 |
s.Callback = nullptr; |
| 353 |
s.CallData = nullptr; |
| 354 |
s.VariableType = vtype; |
| 355 |
s.Variable = variable; |
| 356 |
s.Help = help; |
| 357 |
|
| 358 |
this->Internals->Callbacks[argument] = s; |
| 359 |
this->GenerateHelp(); |
| 360 |
} |
| 361 |
|
| 362 |
#define CommandLineArgumentsAddArgumentMacro(type, ctype) \ |
| 363 |
void CommandLineArguments::AddArgument(const char* argument, \ |
| 364 |
ArgumentTypeEnum type, \ |
| 365 |
ctype* variable, const char* help) \ |
| 366 |
{ \ |
| 367 |
this->AddArgument(argument, type, CommandLineArguments::type##_TYPE, \ |
| 368 |
variable, help); \ |
| 369 |
} |
| 370 |
|
| 371 |
/* clang-format off */ |
| 372 |
CommandLineArgumentsAddArgumentMacro(BOOL, bool) |
| 373 |
CommandLineArgumentsAddArgumentMacro(INT, int) |
| 374 |
CommandLineArgumentsAddArgumentMacro(DOUBLE, double) |
| 375 |
CommandLineArgumentsAddArgumentMacro(STRING, char*) |
| 376 |
CommandLineArgumentsAddArgumentMacro(STL_STRING, std::string) |
| 377 |
|
| 378 |
CommandLineArgumentsAddArgumentMacro(VECTOR_BOOL, std::vector<bool>) |
| 379 |
CommandLineArgumentsAddArgumentMacro(VECTOR_INT, std::vector<int>) |
| 380 |
CommandLineArgumentsAddArgumentMacro(VECTOR_DOUBLE, std::vector<double>) |
| 381 |
CommandLineArgumentsAddArgumentMacro(VECTOR_STRING, std::vector<char*>) |
| 382 |
CommandLineArgumentsAddArgumentMacro(VECTOR_STL_STRING, |
| 383 |
std::vector<std::string>) |
| 384 |
#ifdef HELP_CLANG_FORMAT |
| 385 |
; |
| 386 |
#endif |
| 387 |
/* clang-format on */ |
| 388 |
|
| 389 |
#define CommandLineArgumentsAddBooleanArgumentMacro(type, ctype) \ |
| 390 |
void CommandLineArguments::AddBooleanArgument( \ |
| 391 |
const char* argument, ctype* variable, const char* help) \ |
| 392 |
{ \ |
| 393 |
this->AddArgument(argument, CommandLineArguments::NO_ARGUMENT, \ |
| 394 |
CommandLineArguments::type##_TYPE, variable, help); \ |
| 395 |
} |
| 396 |
|
| 397 |
/* clang-format off */ |
| 398 |
CommandLineArgumentsAddBooleanArgumentMacro(BOOL, bool) |
| 399 |
CommandLineArgumentsAddBooleanArgumentMacro(INT, int) |
| 400 |
CommandLineArgumentsAddBooleanArgumentMacro(DOUBLE, double) |
| 401 |
CommandLineArgumentsAddBooleanArgumentMacro(STRING, char*) |
| 402 |
CommandLineArgumentsAddBooleanArgumentMacro(STL_STRING, std::string) |
| 403 |
#ifdef HELP_CLANG_FORMAT |
| 404 |
; |
| 405 |
#endif |
| 406 |
/* clang-format on */ |
| 407 |
|
| 408 |
void CommandLineArguments::SetClientData(void* client_data) |
| 409 |
{ |
| 410 |
this->Internals->ClientData = client_data; |
| 411 |
} |
| 412 |
|
| 413 |
void CommandLineArguments::SetUnknownArgumentCallback( |
| 414 |
CommandLineArguments::ErrorCallbackType callback) |
| 415 |
{ |
| 416 |
this->Internals->UnknownArgumentCallback = callback; |
| 417 |
} |
| 418 |
|
| 419 |
const char* CommandLineArguments::GetHelp(const char* arg) |
| 420 |
{ |
| 421 |
auto it = this->Internals->Callbacks.find(arg); |
| 422 |
if (it == this->Internals->Callbacks.end()) { |
| 423 |
return nullptr; |
| 424 |
} |
| 425 |
|
| 426 |
// Since several arguments may point to the same argument, find the one this |
| 427 |
// one point to if this one is pointing to another argument. |
| 428 |
CommandLineArgumentsCallbackStructure* cs = &(it->second); |
| 429 |
for (;;) { |
| 430 |
auto hit = this->Internals->Callbacks.find(cs->Help); |
| 431 |
if (hit == this->Internals->Callbacks.end()) { |
| 432 |
break; |
| 433 |
} |
| 434 |
cs = &(hit->second); |
| 435 |
} |
| 436 |
return cs->Help; |
| 437 |
} |
| 438 |
|
| 439 |
void CommandLineArguments::SetLineLength(unsigned int ll) |
| 440 |
{ |
| 441 |
if (ll < 9 || ll > 1000) { |
| 442 |
return; |
| 443 |
} |
| 444 |
this->LineLength = ll; |
| 445 |
this->GenerateHelp(); |
| 446 |
} |
| 447 |
|
| 448 |
const char* CommandLineArguments::GetArgv0() |
| 449 |
{ |
| 450 |
return this->Internals->Argv0.c_str(); |
| 451 |
} |
| 452 |
|
| 453 |
unsigned int CommandLineArguments::GetLastArgument() |
| 454 |
{ |
| 455 |
return static_cast<unsigned int>(this->Internals->LastArgument + 1); |
| 456 |
} |
| 457 |
|
| 458 |
void CommandLineArguments::GenerateHelp() |
| 459 |
{ |
| 460 |
std::ostringstream str; |
| 461 |
|
| 462 |
// Collapse all arguments into the map of vectors of all arguments that do |
| 463 |
// the same thing. |
| 464 |
CommandLineArguments::Internal::CallbacksMap::iterator it; |
| 465 |
using MapArgs = std::map<CommandLineArguments::Internal::String, |
| 466 |
CommandLineArguments::Internal::SetOfStrings>; |
| 467 |
MapArgs mp; |
| 468 |
MapArgs::iterator mpit, smpit; |
| 469 |
for (it = this->Internals->Callbacks.begin(); |
| 470 |
it != this->Internals->Callbacks.end(); it++) { |
| 471 |
CommandLineArgumentsCallbackStructure* cs = &(it->second); |
| 472 |
mpit = mp.find(cs->Help); |
| 473 |
if (mpit != mp.end()) { |
| 474 |
mpit->second.insert(it->first); |
| 475 |
mp[it->first].insert(it->first); |
| 476 |
} else { |
| 477 |
mp[it->first].insert(it->first); |
| 478 |
} |
| 479 |
} |
| 480 |
for (it = this->Internals->Callbacks.begin(); |
| 481 |
it != this->Internals->Callbacks.end(); it++) { |
| 482 |
CommandLineArgumentsCallbackStructure* cs = &(it->second); |
| 483 |
mpit = mp.find(cs->Help); |
| 484 |
if (mpit != mp.end()) { |
| 485 |
mpit->second.insert(it->first); |
| 486 |
smpit = mp.find(it->first); |
| 487 |
CommandLineArguments::Internal::SetOfStrings::iterator sit; |
| 488 |
for (sit = smpit->second.begin(); sit != smpit->second.end(); sit++) { |
| 489 |
mpit->second.insert(*sit); |
| 490 |
} |
| 491 |
mp.erase(smpit); |
| 492 |
} else { |
| 493 |
mp[it->first].insert(it->first); |
| 494 |
} |
| 495 |
} |
| 496 |
|
| 497 |
// Find the length of the longest string |
| 498 |
CommandLineArguments::Internal::String::size_type maxlen = 0; |
| 499 |
for (mpit = mp.begin(); mpit != mp.end(); mpit++) { |
| 500 |
CommandLineArguments::Internal::SetOfStrings::iterator sit; |
| 501 |
for (sit = mpit->second.begin(); sit != mpit->second.end(); sit++) { |
| 502 |
CommandLineArguments::Internal::String::size_type clen = sit->size(); |
| 503 |
switch (this->Internals->Callbacks[*sit].ArgumentType) { |
| 504 |
case CommandLineArguments::NO_ARGUMENT: |
| 505 |
clen += 0; |
| 506 |
break; |
| 507 |
case CommandLineArguments::CONCAT_ARGUMENT: |
| 508 |
clen += 3; |
| 509 |
break; |
| 510 |
case CommandLineArguments::SPACE_ARGUMENT: |
| 511 |
clen += 4; |
| 512 |
break; |
| 513 |
case CommandLineArguments::EQUAL_ARGUMENT: |
| 514 |
clen += 4; |
| 515 |
break; |
| 516 |
} |
| 517 |
if (clen > maxlen) { |
| 518 |
maxlen = clen; |
| 519 |
} |
| 520 |
} |
| 521 |
} |
| 522 |
|
| 523 |
CommandLineArguments::Internal::String::size_type maxstrlen = maxlen; |
| 524 |
maxlen += 4; // For the space before and after the option |
| 525 |
|
| 526 |
// Print help for each option |
| 527 |
for (mpit = mp.begin(); mpit != mp.end(); mpit++) { |
| 528 |
CommandLineArguments::Internal::SetOfStrings::iterator sit; |
| 529 |
for (sit = mpit->second.begin(); sit != mpit->second.end(); sit++) { |
| 530 |
str << std::endl; |
| 531 |
std::string argument = *sit; |
| 532 |
switch (this->Internals->Callbacks[*sit].ArgumentType) { |
| 533 |
case CommandLineArguments::NO_ARGUMENT: |
| 534 |
break; |
| 535 |
case CommandLineArguments::CONCAT_ARGUMENT: |
| 536 |
argument += "opt"; |
| 537 |
break; |
| 538 |
case CommandLineArguments::SPACE_ARGUMENT: |
| 539 |
argument += " opt"; |
| 540 |
break; |
| 541 |
case CommandLineArguments::EQUAL_ARGUMENT: |
| 542 |
argument += "=opt"; |
| 543 |
break; |
| 544 |
case CommandLineArguments::MULTI_ARGUMENT: |
| 545 |
argument += " opt opt ..."; |
| 546 |
break; |
| 547 |
} |
| 548 |
str << " " << argument.substr(0, maxstrlen) << " "; |
| 549 |
} |
| 550 |
const char* ptr = this->Internals->Callbacks[mpit->first].Help; |
| 551 |
size_t len = strlen(ptr); |
| 552 |
int cnt = 0; |
| 553 |
while (len > 0) { |
| 554 |
// If argument with help is longer than line length, split it on previous |
| 555 |
// space (or tab) and continue on the next line |
| 556 |
CommandLineArguments::Internal::String::size_type cc; |
| 557 |
for (cc = 0; ptr[cc]; cc++) { |
| 558 |
if (*ptr == ' ' || *ptr == '\t') { |
| 559 |
ptr++; |
| 560 |
len--; |
| 561 |
} |
| 562 |
} |
| 563 |
if (cnt > 0) { |
| 564 |
for (cc = 0; cc < maxlen; cc++) { |
| 565 |
str << " "; |
| 566 |
} |
| 567 |
} |
| 568 |
CommandLineArguments::Internal::String::size_type skip = len; |
| 569 |
if (skip > this->LineLength - maxlen) { |
| 570 |
skip = this->LineLength - maxlen; |
| 571 |
for (cc = skip - 1; cc > 0; cc--) { |
| 572 |
if (ptr[cc] == ' ' || ptr[cc] == '\t') { |
| 573 |
break; |
| 574 |
} |
| 575 |
} |
| 576 |
if (cc != 0) { |
| 577 |
skip = cc; |
| 578 |
} |
| 579 |
} |
| 580 |
str.write(ptr, static_cast<std::streamsize>(skip)); |
| 581 |
str << std::endl; |
| 582 |
ptr += skip; |
| 583 |
len -= skip; |
| 584 |
cnt++; |
| 585 |
} |
| 586 |
} |
| 587 |
/* |
| 588 |
// This can help debugging help string |
| 589 |
str << endl; |
| 590 |
unsigned int cc; |
| 591 |
for ( cc = 0; cc < this->LineLength; cc ++ ) |
| 592 |
{ |
| 593 |
str << cc % 10; |
| 594 |
} |
| 595 |
str << endl; |
| 596 |
*/ |
| 597 |
this->Help = str.str(); |
| 598 |
} |
| 599 |
|
| 600 |
void CommandLineArguments::PopulateVariable(bool* variable, |
| 601 |
const std::string& value) |
| 602 |
{ |
| 603 |
if (value == "1" || value == "ON" || value == "on" || value == "On" || |
| 604 |
value == "TRUE" || value == "true" || value == "True" || |
| 605 |
value == "yes" || value == "Yes" || value == "YES") { |
| 606 |
*variable = true; |
| 607 |
} else { |
| 608 |
*variable = false; |
| 609 |
} |
| 610 |
} |
| 611 |
|
| 612 |
void CommandLineArguments::PopulateVariable(int* variable, |
| 613 |
const std::string& value) |
| 614 |
{ |
| 615 |
char* res = nullptr; |
| 616 |
*variable = static_cast<int>(strtol(value.c_str(), &res, 10)); |
| 617 |
// if ( res && *res ) |
| 618 |
// { |
| 619 |
// Can handle non-int |
| 620 |
// } |
| 621 |
} |
| 622 |
|
| 623 |
void CommandLineArguments::PopulateVariable(double* variable, |
| 624 |
const std::string& value) |
| 625 |
{ |
| 626 |
char* res = nullptr; |
| 627 |
*variable = strtod(value.c_str(), &res); |
| 628 |
// if ( res && *res ) |
| 629 |
// { |
| 630 |
// Can handle non-double |
| 631 |
// } |
| 632 |
} |
| 633 |
|
| 634 |
void CommandLineArguments::PopulateVariable(char** variable, |
| 635 |
const std::string& value) |
| 636 |
{ |
| 637 |
delete[] * variable; |
| 638 |
*variable = new char[value.size() + 1]; |
| 639 |
strcpy(*variable, value.c_str()); |
| 640 |
} |
| 641 |
|
| 642 |
void CommandLineArguments::PopulateVariable(std::string* variable, |
| 643 |
const std::string& value) |
| 644 |
{ |
| 645 |
*variable = value; |
| 646 |
} |
| 647 |
|
| 648 |
void CommandLineArguments::PopulateVariable(std::vector<bool>* variable, |
| 649 |
const std::string& value) |
| 650 |
{ |
| 651 |
bool val = false; |
| 652 |
if (value == "1" || value == "ON" || value == "on" || value == "On" || |
| 653 |
value == "TRUE" || value == "true" || value == "True" || |
| 654 |
value == "yes" || value == "Yes" || value == "YES") { |
| 655 |
val = true; |
| 656 |
} |
| 657 |
variable->push_back(val); |
| 658 |
} |
| 659 |
|
| 660 |
void CommandLineArguments::PopulateVariable(std::vector<int>* variable, |
| 661 |
const std::string& value) |
| 662 |
{ |
| 663 |
char* res = nullptr; |
| 664 |
variable->push_back(static_cast<int>(strtol(value.c_str(), &res, 10))); |
| 665 |
// if ( res && *res ) |
| 666 |
// { |
| 667 |
// Can handle non-int |
| 668 |
// } |
| 669 |
} |
| 670 |
|
| 671 |
void CommandLineArguments::PopulateVariable(std::vector<double>* variable, |
| 672 |
const std::string& value) |
| 673 |
{ |
| 674 |
char* res = nullptr; |
| 675 |
variable->push_back(strtod(value.c_str(), &res)); |
| 676 |
// if ( res && *res ) |
| 677 |
// { |
| 678 |
// Can handle non-int |
| 679 |
// } |
| 680 |
} |
| 681 |
|
| 682 |
void CommandLineArguments::PopulateVariable(std::vector<char*>* variable, |
| 683 |
const std::string& value) |
| 684 |
{ |
| 685 |
char* var = new char[value.size() + 1]; |
| 686 |
strcpy(var, value.c_str()); |
| 687 |
variable->push_back(var); |
| 688 |
} |
| 689 |
|
| 690 |
void CommandLineArguments::PopulateVariable(std::vector<std::string>* variable, |
| 691 |
const std::string& value) |
| 692 |
{ |
| 693 |
variable->push_back(value); |
| 694 |
} |
| 695 |
|
| 696 |
bool CommandLineArguments::PopulateVariable( |
| 697 |
CommandLineArgumentsCallbackStructure* cs, const char* value) |
| 698 |
{ |
| 699 |
// Call the callback |
| 700 |
if (cs->Callback) { |
| 701 |
if (!cs->Callback(cs->Argument, value, cs->CallData)) { |
| 702 |
this->Internals->LastArgument--; |
| 703 |
return false; |
| 704 |
} |
| 705 |
} |
| 706 |
CommandLineArguments_DEBUG("Set argument: " << cs->Argument << " to " |
| 707 |
<< value); |
| 708 |
if (cs->Variable) { |
| 709 |
std::string var = "1"; |
| 710 |
if (value) { |
| 711 |
var = value; |
| 712 |
} |
| 713 |
switch (cs->VariableType) { |
| 714 |
case CommandLineArguments::INT_TYPE: |
| 715 |
this->PopulateVariable(static_cast<int*>(cs->Variable), var); |
| 716 |
break; |
| 717 |
case CommandLineArguments::DOUBLE_TYPE: |
| 718 |
this->PopulateVariable(static_cast<double*>(cs->Variable), var); |
| 719 |
break; |
| 720 |
case CommandLineArguments::STRING_TYPE: |
| 721 |
this->PopulateVariable(static_cast<char**>(cs->Variable), var); |
| 722 |
break; |
| 723 |
case CommandLineArguments::STL_STRING_TYPE: |
| 724 |
this->PopulateVariable(static_cast<std::string*>(cs->Variable), var); |
| 725 |
break; |
| 726 |
case CommandLineArguments::BOOL_TYPE: |
| 727 |
this->PopulateVariable(static_cast<bool*>(cs->Variable), var); |
| 728 |
break; |
| 729 |
case CommandLineArguments::VECTOR_BOOL_TYPE: |
| 730 |
this->PopulateVariable(static_cast<std::vector<bool>*>(cs->Variable), |
| 731 |
var); |
| 732 |
break; |
| 733 |
case CommandLineArguments::VECTOR_INT_TYPE: |
| 734 |
this->PopulateVariable(static_cast<std::vector<int>*>(cs->Variable), |
| 735 |
var); |
| 736 |
break; |
| 737 |
case CommandLineArguments::VECTOR_DOUBLE_TYPE: |
| 738 |
this->PopulateVariable(static_cast<std::vector<double>*>(cs->Variable), |
| 739 |
var); |
| 740 |
break; |
| 741 |
case CommandLineArguments::VECTOR_STRING_TYPE: |
| 742 |
this->PopulateVariable(static_cast<std::vector<char*>*>(cs->Variable), |
| 743 |
var); |
| 744 |
break; |
| 745 |
case CommandLineArguments::VECTOR_STL_STRING_TYPE: |
| 746 |
this->PopulateVariable( |
| 747 |
static_cast<std::vector<std::string>*>(cs->Variable), var); |
| 748 |
break; |
| 749 |
default: |
| 750 |
std::cerr << "Got unknown variable type: \"" << cs->VariableType |
| 751 |
<< "\"" << std::endl; |
| 752 |
this->Internals->LastArgument--; |
| 753 |
return false; |
| 754 |
} |
| 755 |
} |
| 756 |
return true; |
| 757 |
} |
| 758 |
|
| 759 |
} // namespace KWSYS_NAMESPACE |