CommandLineArguments.cxx 26.4 KB
Newer Older
1 2 3
/*============================================================================
  KWSys - Kitware System Library
  Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
4

5 6
  Distributed under the OSI-approved BSD License (the "License");
  see accompanying file Copyright.txt for details.
7

8 9 10 11
  This software is distributed WITHOUT ANY WARRANTY; without even the
  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  See the License for more information.
============================================================================*/
12 13 14 15
#include "kwsysPrivate.h"
#include KWSYS_HEADER(CommandLineArguments.hxx)

#include KWSYS_HEADER(Configure.hxx)
16
#include KWSYS_HEADER(String.hxx)
17 18 19 20 21

#include KWSYS_HEADER(stl/vector)
#include KWSYS_HEADER(stl/map)
#include KWSYS_HEADER(stl/set)

22 23 24 25 26 27 28 29
// Work-around CMake dependency scanning limitation.  This must
// duplicate the above list of headers.
#if 0
# include "CommandLineArguments.hxx.in"
# include "Configure.hxx.in"
# include "kwsys_stl.hxx.in"
#endif

30 31 32
#include <sstream>
#include <iostream>

33
#include <stdio.h>
Andy Cedilnik's avatar
Andy Cedilnik committed
34
#include <stdlib.h>
35
#include <string.h>
36

37 38 39 40
#ifdef _MSC_VER
# pragma warning (disable: 4786)
#endif

41 42 43 44
#if defined(__sgi) && !defined(__GNUC__)
# pragma set woff 1375 /* base class destructor not virtual */
#endif

45 46
#if 0
#  define CommandLineArguments_DEBUG(x) \
47
  std::cout << __LINE__ << " CLA: " << x << std::endl
48 49 50 51
#else
#  define CommandLineArguments_DEBUG(x)
#endif

52 53 54 55 56
namespace KWSYS_NAMESPACE
{

//----------------------------------------------------------------------------
//============================================================================
57 58 59 60 61 62 63 64 65 66 67
struct CommandLineArgumentsCallbackStructure
{
  const char* Argument;
  int ArgumentType;
  CommandLineArguments::CallbackType Callback;
  void* CallData;
  void* Variable;
  int VariableType;
  const char* Help;
};
 
68
class CommandLineArgumentsVectorOfStrings : 
69
  public kwsys_stl::vector<kwsys::String> {};
70
class CommandLineArgumentsSetOfStrings :
71
  public kwsys_stl::set<kwsys::String> {};
72
class CommandLineArgumentsMapOfStrucs : 
73
  public kwsys_stl::map<kwsys::String,
74
    CommandLineArgumentsCallbackStructure> {};
75 76 77 78 79 80 81 82 83 84 85 86 87

class CommandLineArgumentsInternal
{
public:
  CommandLineArgumentsInternal()
    {
    this->UnknownArgumentCallback = 0;
    this->ClientData = 0;
    this->LastArgument = 0;
    }

  typedef CommandLineArgumentsVectorOfStrings VectorOfStrings;
  typedef CommandLineArgumentsMapOfStrucs CallbacksMap;
88
  typedef kwsys::String String;
89 90 91
  typedef CommandLineArgumentsSetOfStrings SetOfStrings;

  VectorOfStrings Argv;
92
  String Argv0;
93 94 95 96 97 98
  CallbacksMap Callbacks;

  CommandLineArguments::ErrorCallbackType UnknownArgumentCallback;
  void*             ClientData;

  VectorOfStrings::size_type LastArgument;
99 100

  VectorOfStrings UnusedArguments;
101 102 103 104 105 106 107 108 109 110
};
//============================================================================
//----------------------------------------------------------------------------

//----------------------------------------------------------------------------
CommandLineArguments::CommandLineArguments()
{
  this->Internals = new CommandLineArguments::Internal;
  this->Help = "";
  this->LineLength = 80;
111
  this->StoreUnusedArgumentsFlag = false;
112 113 114 115 116 117 118 119 120
}

//----------------------------------------------------------------------------
CommandLineArguments::~CommandLineArguments()
{
  delete this->Internals;
}

//----------------------------------------------------------------------------
121
void CommandLineArguments::Initialize(int argc, const char* const argv[])
122 123 124 125
{
  int cc;

  this->Initialize();
126
  this->Internals->Argv0 = argv[0];
127 128 129 130 131 132
  for ( cc = 1; cc < argc; cc ++ )
    {
    this->ProcessArgument(argv[cc]);
    }
}

Ken Martin's avatar
Ken Martin committed
133 134 135
//----------------------------------------------------------------------------
void CommandLineArguments::Initialize(int argc, char* argv[])
{
136
  this->Initialize(argc, static_cast<const char* const*>(argv));
Ken Martin's avatar
Ken Martin committed
137 138
}

139 140 141 142 143 144 145 146 147 148 149 150 151 152
//----------------------------------------------------------------------------
void CommandLineArguments::Initialize()
{
  this->Internals->Argv.clear();
  this->Internals->LastArgument = 0;
}

//----------------------------------------------------------------------------
void CommandLineArguments::ProcessArgument(const char* arg)
{
  this->Internals->Argv.push_back(arg);
}

//----------------------------------------------------------------------------
153 154 155
bool CommandLineArguments::GetMatchedArguments(
  kwsys_stl::vector<kwsys_stl::string>* matches,
  const kwsys_stl::string& arg)
156
{
157 158 159 160 161 162 163
  matches->clear();
  CommandLineArguments::Internal::CallbacksMap::iterator it;

  // Does the argument match to any we know about?
  for ( it = this->Internals->Callbacks.begin();
    it != this->Internals->Callbacks.end();
    it ++ )
164
    {
165 166 167 168
    const CommandLineArguments::Internal::String& parg = it->first;
    CommandLineArgumentsCallbackStructure *cs = &it->second;
    if (cs->ArgumentType == CommandLineArguments::NO_ARGUMENT ||
      cs->ArgumentType == CommandLineArguments::SPACE_ARGUMENT) 
169
      {
170
      if ( arg == parg )
171
        {
172
        matches->push_back(parg);
173 174
        }
      }
175 176 177 178 179
    else if ( arg.find( parg ) == 0 )
      {
      matches->push_back(parg);
      }
    }
180
  return !matches->empty();
181 182 183 184 185 186 187
}

//----------------------------------------------------------------------------
int CommandLineArguments::Parse()
{
  kwsys_stl::vector<kwsys_stl::string>::size_type cc;
  kwsys_stl::vector<kwsys_stl::string> matches;
188 189 190 191
  if ( this->StoreUnusedArgumentsFlag )
    {
    this->Internals->UnusedArguments.clear();
    }
192 193 194 195 196 197
  for ( cc = 0; cc < this->Internals->Argv.size(); cc ++ )
    {
    const kwsys_stl::string& arg = this->Internals->Argv[cc]; 
    CommandLineArguments_DEBUG("Process argument: " << arg);
    this->Internals->LastArgument = cc;
    if ( this->GetMatchedArguments(&matches, arg) )
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
      {
      // Ok, we found one or more arguments that match what user specified.
      // Let's find the longest one.
      CommandLineArguments::Internal::VectorOfStrings::size_type kk;
      CommandLineArguments::Internal::VectorOfStrings::size_type maxidx = 0;
      CommandLineArguments::Internal::String::size_type maxlen = 0;
      for ( kk = 0; kk < matches.size(); kk ++ )
        {
        if ( matches[kk].size() > maxlen )
          {
          maxlen = matches[kk].size();
          maxidx = kk;
          }
        }
      // So, the longest one is probably the right one. Now see if it has any
      // additional value
214
      CommandLineArgumentsCallbackStructure *cs 
215
        = &this->Internals->Callbacks[matches[maxidx]];
216
      const kwsys_stl::string& sarg = matches[maxidx];
217
      if ( cs->Argument != sarg )
218
        {
219
        abort();
220
        }
221
      switch ( cs->ArgumentType )
222
        {
223 224 225 226 227 228 229 230
      case NO_ARGUMENT:
        // No value
        if ( !this->PopulateVariable(cs, 0) )
          {
          return 0;
          }
        break;
      case SPACE_ARGUMENT:
231 232
        if ( cc == this->Internals->Argv.size()-1 )
          {
233
          this->Internals->LastArgument --;
234 235
          return 0;
          }
236
        CommandLineArguments_DEBUG("This is a space argument: " << arg
237
          << " value: " << this->Internals->Argv[cc+1]);
238
        // Value is the next argument
239 240 241 242
        if ( !this->PopulateVariable(cs, this->Internals->Argv[cc+1].c_str()) )
          {
          return 0;
          }
243
        cc ++;
244 245
        break;
      case EQUAL_ARGUMENT:
246
        if ( arg.size() == sarg.size() || arg.at(sarg.size()) != '=' )
247
          {
248
          this->Internals->LastArgument --;
249 250 251
          return 0;
          }
        // Value is everythng followed the '=' sign
252
        if ( !this->PopulateVariable(cs, arg.c_str() + sarg.size() + 1) )
253 254 255
          {
          return 0;
          }
256 257 258 259
        break;
      case CONCAT_ARGUMENT:
        // Value is whatever follows the argument
        if ( !this->PopulateVariable(cs, arg.c_str() + sarg.size()) )
260
          {
261
          return 0;
262
          }
263 264 265 266 267
        break;
      case MULTI_ARGUMENT:
        // Suck in all the rest of the arguments
        CommandLineArguments_DEBUG("This is a multi argument: " << arg);
        for (cc++; cc < this->Internals->Argv.size(); ++ cc )
268
          {
269 270 271
          const kwsys_stl::string& marg = this->Internals->Argv[cc];
          CommandLineArguments_DEBUG(" check multi argument value: " << marg);
          if ( this->GetMatchedArguments(&matches, marg) )
272
            {
273 274
            CommandLineArguments_DEBUG("End of multi argument " << arg << " with value: " << marg);
            break;
275
            }
276 277
          CommandLineArguments_DEBUG(" populate multi argument value: " << marg);
          if ( !this->PopulateVariable(cs, marg.c_str()) )
278
            {
279
            return 0;
280 281
            }
          }
282
        if ( cc != this->Internals->Argv.size() )
283
          {
284 285 286
          CommandLineArguments_DEBUG("Again End of multi argument " << arg);
          cc--;
          continue;
287
          }
288
        break;
289
      default:
290
        std::cerr << "Got unknown argument type: \"" << cs->ArgumentType << "\"" << std::endl;
291 292
        this->Internals->LastArgument --;
        return 0;
293 294 295 296 297 298 299 300 301 302
        }
      }
    else
      {
      // Handle unknown arguments
      if ( this->Internals->UnknownArgumentCallback )
        {
        if ( !this->Internals->UnknownArgumentCallback(arg.c_str(), 
            this->Internals->ClientData) )
          {
303
          this->Internals->LastArgument --;
304 305
          return 0;
          }
306
        return 1;
307
        }
308 309 310
      else if ( this->StoreUnusedArgumentsFlag )
        {
        CommandLineArguments_DEBUG("Store unused argument " << arg);
311
        this->Internals->UnusedArguments.push_back(arg);
312
        }
313 314
      else
        {
315
        std::cerr << "Got unknown argument: \"" << arg << "\"" << std::endl;
316
        this->Internals->LastArgument --;
317 318 319 320 321 322 323 324 325 326 327
        return 0;
        }
      }
    }
  return 1;
}

//----------------------------------------------------------------------------
void CommandLineArguments::GetRemainingArguments(int* argc, char*** argv)
{
  CommandLineArguments::Internal::VectorOfStrings::size_type size 
Andy Cedilnik's avatar
Andy Cedilnik committed
328
    = this->Internals->Argv.size() - this->Internals->LastArgument + 1;
329 330
  CommandLineArguments::Internal::VectorOfStrings::size_type cc;

331
  // Copy Argv0 as the first argument
332
  char** args = new char*[ size ];
333 334
  args[0] = new char[ this->Internals->Argv0.size() + 1 ];
  strcpy(args[0], this->Internals->Argv0.c_str());
335
  int cnt = 1;
336 337 338

  // Copy everything after the LastArgument, since that was not parsed.
  for ( cc = this->Internals->LastArgument+1; 
339 340 341 342 343 344 345 346 347 348
    cc < this->Internals->Argv.size(); cc ++ )
    {
    args[cnt] = new char[ this->Internals->Argv[cc].size() + 1];
    strcpy(args[cnt], this->Internals->Argv[cc].c_str());
    cnt ++;
    }
  *argc = cnt;
  *argv = args;
}

349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
//----------------------------------------------------------------------------
void CommandLineArguments::GetUnusedArguments(int* argc, char*** argv)
{
  CommandLineArguments::Internal::VectorOfStrings::size_type size 
    = this->Internals->UnusedArguments.size() + 1;
  CommandLineArguments::Internal::VectorOfStrings::size_type cc;

  // Copy Argv0 as the first argument
  char** args = new char*[ size ];
  args[0] = new char[ this->Internals->Argv0.size() + 1 ];
  strcpy(args[0], this->Internals->Argv0.c_str());
  int cnt = 1;

  // Copy everything after the LastArgument, since that was not parsed.
  for ( cc = 0;
    cc < this->Internals->UnusedArguments.size(); cc ++ )
    {
    kwsys::String &str = this->Internals->UnusedArguments[cc];
    args[cnt] = new char[ str.size() + 1];
    strcpy(args[cnt], str.c_str());
    cnt ++;
    }
  *argc = cnt;
  *argv = args;
}

375 376 377 378 379 380
//----------------------------------------------------------------------------
void CommandLineArguments::DeleteRemainingArguments(int argc, char*** argv)
{
  int cc;
  for ( cc = 0; cc < argc; ++ cc )
    {
381
    delete [] (*argv)[cc];
382 383 384 385
    }
  delete [] *argv;
}

386 387 388 389
//----------------------------------------------------------------------------
void CommandLineArguments::AddCallback(const char* argument, ArgumentTypeEnum type, 
  CallbackType callback, void* call_data, const char* help)
{
390
  CommandLineArgumentsCallbackStructure s;
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
  s.Argument     = argument;
  s.ArgumentType = type;
  s.Callback     = callback;
  s.CallData     = call_data;
  s.VariableType = CommandLineArguments::NO_VARIABLE_TYPE;
  s.Variable     = 0;
  s.Help         = help;

  this->Internals->Callbacks[argument] = s;
  this->GenerateHelp();
}

//----------------------------------------------------------------------------
void CommandLineArguments::AddArgument(const char* argument, ArgumentTypeEnum type,
  VariableTypeEnum vtype, void* variable, const char* help)
{
407
  CommandLineArgumentsCallbackStructure s;
408 409 410 411 412 413 414 415 416 417 418 419 420
  s.Argument     = argument;
  s.ArgumentType = type;
  s.Callback     = 0;
  s.CallData     = 0;
  s.VariableType = vtype;
  s.Variable     = variable;
  s.Help         = help;

  this->Internals->Callbacks[argument] = s;
  this->GenerateHelp();
}

//----------------------------------------------------------------------------
421 422 423 424 425 426 427
#define CommandLineArgumentsAddArgumentMacro(type, ctype) \
  void CommandLineArguments::AddArgument(const char* argument, ArgumentTypeEnum type, \
    ctype* variable, const char* help) \
  { \
    this->AddArgument(argument, type, CommandLineArguments::type##_TYPE, variable, help); \
  }

Mathieu Malaterre's avatar
Mathieu Malaterre committed
428 429 430 431 432 433 434 435 436 437 438
CommandLineArgumentsAddArgumentMacro(BOOL,       bool)
CommandLineArgumentsAddArgumentMacro(INT,        int)
CommandLineArgumentsAddArgumentMacro(DOUBLE,     double)
CommandLineArgumentsAddArgumentMacro(STRING,     char*)
CommandLineArgumentsAddArgumentMacro(STL_STRING, kwsys_stl::string)

CommandLineArgumentsAddArgumentMacro(VECTOR_BOOL,       kwsys_stl::vector<bool>)
CommandLineArgumentsAddArgumentMacro(VECTOR_INT,        kwsys_stl::vector<int>)
CommandLineArgumentsAddArgumentMacro(VECTOR_DOUBLE,     kwsys_stl::vector<double>)
CommandLineArgumentsAddArgumentMacro(VECTOR_STRING,     kwsys_stl::vector<char*>)
CommandLineArgumentsAddArgumentMacro(VECTOR_STL_STRING, kwsys_stl::vector<kwsys_stl::string>)
439 440

//----------------------------------------------------------------------------
441 442 443 444 445 446 447 448
#define CommandLineArgumentsAddBooleanArgumentMacro(type, ctype) \
  void CommandLineArguments::AddBooleanArgument(const char* argument, \
    ctype* variable, const char* help) \
  { \
    this->AddArgument(argument, CommandLineArguments::NO_ARGUMENT, \
      CommandLineArguments::type##_TYPE, variable, help); \
  }

Mathieu Malaterre's avatar
Mathieu Malaterre committed
449 450 451 452 453
CommandLineArgumentsAddBooleanArgumentMacro(BOOL,       bool)
CommandLineArgumentsAddBooleanArgumentMacro(INT,        int)
CommandLineArgumentsAddBooleanArgumentMacro(DOUBLE,     double)
CommandLineArgumentsAddBooleanArgumentMacro(STRING,     char*)
CommandLineArgumentsAddBooleanArgumentMacro(STL_STRING, kwsys_stl::string)
454

455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
//----------------------------------------------------------------------------
void CommandLineArguments::SetClientData(void* client_data)
{
  this->Internals->ClientData = client_data;
}

//----------------------------------------------------------------------------
void CommandLineArguments::SetUnknownArgumentCallback(
  CommandLineArguments::ErrorCallbackType callback)
{
  this->Internals->UnknownArgumentCallback = callback;
}

//----------------------------------------------------------------------------
const char* CommandLineArguments::GetHelp(const char* arg)
{
  CommandLineArguments::Internal::CallbacksMap::iterator it 
    = this->Internals->Callbacks.find(arg);
  if ( it == this->Internals->Callbacks.end() )
    {
    return 0;
    }

  // Since several arguments may point to the same argument, find the one this
  // one point to if this one is pointing to another argument.
480
  CommandLineArgumentsCallbackStructure *cs = &(it->second);
481
  for(;;)
482 483 484 485 486
    {
    CommandLineArguments::Internal::CallbacksMap::iterator hit 
      = this->Internals->Callbacks.find(cs->Help);
    if ( hit == this->Internals->Callbacks.end() )
      {
Andy Cedilnik's avatar
Andy Cedilnik committed
487
      break;
488 489 490
      }
    cs = &(hit->second);
    }
Andy Cedilnik's avatar
Andy Cedilnik committed
491
  return cs->Help;
492 493
}

494 495 496 497 498 499 500 501 502 503 504
//----------------------------------------------------------------------------
void CommandLineArguments::SetLineLength(unsigned int ll)
{
  if ( ll < 9 || ll > 1000 )
    {
    return;
    }
  this->LineLength = ll;
  this->GenerateHelp();
}

Andy Cedilnik's avatar
Andy Cedilnik committed
505 506 507 508 509 510
//----------------------------------------------------------------------------
const char* CommandLineArguments::GetArgv0()
{
  return this->Internals->Argv0.c_str();
}

511 512 513
//----------------------------------------------------------------------------
unsigned int CommandLineArguments::GetLastArgument()
{
Andy Cedilnik's avatar
Andy Cedilnik committed
514
  return static_cast<unsigned int>(this->Internals->LastArgument + 1);
515 516
}

517 518 519
//----------------------------------------------------------------------------
void CommandLineArguments::GenerateHelp()
{
520
  std::ostringstream str;
Francois Bertel's avatar
Francois Bertel committed
521
  
522 523 524 525 526 527 528 529 530 531 532
  // Collapse all arguments into the map of vectors of all arguments that do
  // the same thing.
  CommandLineArguments::Internal::CallbacksMap::iterator it;
  typedef kwsys_stl::map<CommandLineArguments::Internal::String, 
     CommandLineArguments::Internal::SetOfStrings > MapArgs;
  MapArgs mp;
  MapArgs::iterator mpit, smpit;
  for ( it = this->Internals->Callbacks.begin();
    it != this->Internals->Callbacks.end();
    it ++ )
    {
533
    CommandLineArgumentsCallbackStructure *cs = &(it->second);
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548
    mpit = mp.find(cs->Help);
    if ( mpit != mp.end() )
      {
      mpit->second.insert(it->first);
      mp[it->first].insert(it->first);
      }
    else
      {
      mp[it->first].insert(it->first);
      }
    }
  for ( it = this->Internals->Callbacks.begin();
    it != this->Internals->Callbacks.end();
    it ++ )
    {
549
    CommandLineArgumentsCallbackStructure *cs = &(it->second);
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
    mpit = mp.find(cs->Help);
    if ( mpit != mp.end() )
      {
      mpit->second.insert(it->first);
      smpit = mp.find(it->first);
      CommandLineArguments::Internal::SetOfStrings::iterator sit;
      for ( sit = smpit->second.begin(); sit != smpit->second.end(); sit++ )
        {
        mpit->second.insert(*sit);
        }
      mp.erase(smpit);
      }
    else
      {
      mp[it->first].insert(it->first);
      }
    }
 
  // Find the length of the longest string
  CommandLineArguments::Internal::String::size_type maxlen = 0;
  for ( mpit = mp.begin();
    mpit != mp.end();
    mpit ++ )
    {
    CommandLineArguments::Internal::SetOfStrings::iterator sit;
    for ( sit = mpit->second.begin(); sit != mpit->second.end(); sit++ )
      {
      CommandLineArguments::Internal::String::size_type clen = sit->size();
      switch ( this->Internals->Callbacks[*sit].ArgumentType )
        {
        case CommandLineArguments::NO_ARGUMENT:     clen += 0; break;
581 582 583
        case CommandLineArguments::CONCAT_ARGUMENT: clen += 3; break;
        case CommandLineArguments::SPACE_ARGUMENT:  clen += 4; break;
        case CommandLineArguments::EQUAL_ARGUMENT:  clen += 4; break;
584 585 586 587 588 589 590 591 592 593
        }
      if ( clen > maxlen )
        {
        maxlen = clen;
        }
      }
    }

  // Create format for that string
  char format[80];
594
  sprintf(format, "  %%-%us  ", static_cast<unsigned int>(maxlen));
595

596
  maxlen += 4; // For the space before and after the option
597 598 599 600 601 602 603 604 605

  // Print help for each option
  for ( mpit = mp.begin();
    mpit != mp.end();
    mpit ++ )
    {
    CommandLineArguments::Internal::SetOfStrings::iterator sit;
    for ( sit = mpit->second.begin(); sit != mpit->second.end(); sit++ )
      {
606
      str << std::endl;
607
      char argument[100];
608
      sprintf(argument, "%s", sit->c_str());
609 610 611
      switch ( this->Internals->Callbacks[*sit].ArgumentType )
        {
        case CommandLineArguments::NO_ARGUMENT: break;
612 613 614
        case CommandLineArguments::CONCAT_ARGUMENT: strcat(argument, "opt"); break;
        case CommandLineArguments::SPACE_ARGUMENT:  strcat(argument, " opt"); break;
        case CommandLineArguments::EQUAL_ARGUMENT:  strcat(argument, "=opt"); break;
615
        case CommandLineArguments::MULTI_ARGUMENT:  strcat(argument, " opt opt ..."); break;
616 617 618 619 620 621
        }
      char buffer[80];
      sprintf(buffer, format, argument);
      str << buffer;
      }
    const char* ptr = this->Internals->Callbacks[mpit->first].Help;
622
    size_t len = strlen(ptr);
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
    int cnt = 0;
    while ( len > 0)
      {
      // If argument with help is longer than line length, split it on previous
      // space (or tab) and continue on the next line
      CommandLineArguments::Internal::String::size_type cc;
      for ( cc = 0; ptr[cc]; cc ++ )
        {
        if ( *ptr == ' ' || *ptr == '\t' )
          {
          ptr ++;
          len --;
          }
        }
      if ( cnt > 0 )
        {
        for ( cc = 0; cc < maxlen; cc ++ )
          {
          str << " ";
          }
        }
      CommandLineArguments::Internal::String::size_type skip = len;
      if ( skip > this->LineLength - maxlen )
        {
        skip = this->LineLength - maxlen;
        for ( cc = skip-1; cc > 0; cc -- )
          {
          if ( ptr[cc] == ' ' || ptr[cc] == '\t' )
            {
            break;
            }
          }
        if ( cc != 0 )
          {
          skip = cc;
          }
        }
660 661
      str.write(ptr, static_cast<std::streamsize>(skip));
      str << std::endl;
662 663 664 665 666
      ptr += skip;
      len -= skip;
      cnt ++;
      }
    }
667 668 669 670 671 672 673 674 675 676
  /*
  // This can help debugging help string
  str << endl;
  unsigned int cc;
  for ( cc = 0; cc < this->LineLength; cc ++ )
    {
    str << cc % 10;
    }
  str << endl;
  */
677 678 679
  this->Help = str.str();
}

680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700
//----------------------------------------------------------------------------
void CommandLineArguments::PopulateVariable(
  bool* variable, const kwsys_stl::string& value)
{
  if ( value == "1" || value == "ON" || value == "on" || value == "On" ||
    value == "TRUE" || value == "true" || value == "True" ||
    value == "yes" || value == "Yes" || value == "YES" )
    {
    *variable = true;
    }
  else
    {
    *variable = false;
    }
}

//----------------------------------------------------------------------------
void CommandLineArguments::PopulateVariable(
  int* variable, const kwsys_stl::string& value)
{
  char* res = 0;
Francois Bertel's avatar
Francois Bertel committed
701
  *variable = static_cast<int>(strtol(value.c_str(), &res, 10));
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758
  //if ( res && *res )
  //  {
  //  Can handle non-int
  //  }
}

//----------------------------------------------------------------------------
void CommandLineArguments::PopulateVariable(
  double* variable, const kwsys_stl::string& value)
{
  char* res = 0;
  *variable = strtod(value.c_str(), &res);
  //if ( res && *res )
  //  {
  //  Can handle non-double
  //  }
}

//----------------------------------------------------------------------------
void CommandLineArguments::PopulateVariable(
  char** variable, const kwsys_stl::string& value)
{
  if ( *variable )
    {
    delete [] *variable;
    *variable = 0;
    }
  *variable = new char[ value.size() + 1 ];
  strcpy(*variable, value.c_str());
}

//----------------------------------------------------------------------------
void CommandLineArguments::PopulateVariable(
  kwsys_stl::string* variable, const kwsys_stl::string& value)
{
  *variable = value;
}

//----------------------------------------------------------------------------
void CommandLineArguments::PopulateVariable(
  kwsys_stl::vector<bool>* variable, const kwsys_stl::string& value)
{
  bool val = false;
  if ( value == "1" || value == "ON" || value == "on" || value == "On" ||
    value == "TRUE" || value == "true" || value == "True" ||
    value == "yes" || value == "Yes" || value == "YES" )
    {
    val = true;
    }
  variable->push_back(val);
}

//----------------------------------------------------------------------------
void CommandLineArguments::PopulateVariable(
  kwsys_stl::vector<int>* variable, const kwsys_stl::string& value)
{
  char* res = 0;
Francois Bertel's avatar
Francois Bertel committed
759
  variable->push_back(static_cast<int>(strtol(value.c_str(), &res, 10)));
760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807
  //if ( res && *res )
  //  {
  //  Can handle non-int
  //  }
}

//----------------------------------------------------------------------------
void CommandLineArguments::PopulateVariable(
  kwsys_stl::vector<double>* variable, const kwsys_stl::string& value)
{
  char* res = 0;
  variable->push_back(strtod(value.c_str(), &res));
  //if ( res && *res )
  //  {
  //  Can handle non-int
  //  }
}

//----------------------------------------------------------------------------
void CommandLineArguments::PopulateVariable(
  kwsys_stl::vector<char*>* variable, const kwsys_stl::string& value)
{
  char* var = new char[ value.size() + 1 ];
  strcpy(var, value.c_str());
  variable->push_back(var);
}

//----------------------------------------------------------------------------
void CommandLineArguments::PopulateVariable(
  kwsys_stl::vector<kwsys_stl::string>* variable,
  const kwsys_stl::string& value)
{
  variable->push_back(value);
}

//----------------------------------------------------------------------------
bool CommandLineArguments::PopulateVariable(CommandLineArgumentsCallbackStructure* cs,
  const char* value)
{
  // Call the callback
  if ( cs->Callback )
    {
    if ( !cs->Callback(cs->Argument, value, cs->CallData) )
      {
      this->Internals->LastArgument --;
      return 0;
      }
    }
808
  CommandLineArguments_DEBUG("Set argument: " << cs->Argument << " to " << value);
809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848
  if ( cs->Variable )
    {
    kwsys_stl::string var = "1";
    if ( value )
      {
      var = value;
      }
    switch ( cs->VariableType )
      {
    case CommandLineArguments::INT_TYPE:
      this->PopulateVariable(static_cast<int*>(cs->Variable), var);
      break;
    case CommandLineArguments::DOUBLE_TYPE:
      this->PopulateVariable(static_cast<double*>(cs->Variable), var);
      break;
    case CommandLineArguments::STRING_TYPE:
      this->PopulateVariable(static_cast<char**>(cs->Variable), var);
      break;
    case CommandLineArguments::STL_STRING_TYPE:
      this->PopulateVariable(static_cast<kwsys_stl::string*>(cs->Variable), var);
      break;
    case CommandLineArguments::BOOL_TYPE:
      this->PopulateVariable(static_cast<bool*>(cs->Variable), var);
      break;
    case CommandLineArguments::VECTOR_BOOL_TYPE:
      this->PopulateVariable(static_cast<kwsys_stl::vector<bool>*>(cs->Variable), var);
      break;
    case CommandLineArguments::VECTOR_INT_TYPE:
      this->PopulateVariable(static_cast<kwsys_stl::vector<int>*>(cs->Variable), var);
      break;
    case CommandLineArguments::VECTOR_DOUBLE_TYPE:
      this->PopulateVariable(static_cast<kwsys_stl::vector<double>*>(cs->Variable), var);
      break;
    case CommandLineArguments::VECTOR_STRING_TYPE:
      this->PopulateVariable(static_cast<kwsys_stl::vector<char*>*>(cs->Variable), var);
      break;
    case CommandLineArguments::VECTOR_STL_STRING_TYPE:
      this->PopulateVariable(static_cast<kwsys_stl::vector<kwsys_stl::string>*>(cs->Variable), var);
      break;
    default:
849
      std::cerr << "Got unknown variable type: \"" << cs->VariableType << "\"" << std::endl;
850 851 852 853 854 855 856 857
      this->Internals->LastArgument --;
      return 0;
      }
    }
  return 1;
}


858
} // namespace KWSYS_NAMESPACE