CommandLineArguments.cxx 27 KB

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