ctkCommandLineParser.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  1. // STL includes
  2. #include <stdexcept>
  3. // Qt includes
  4. #include <QHash>
  5. #include <QStringList>
  6. #include <QTextStream>
  7. #include <QDebug>
  8. #include <QSettings>
  9. #include <QPointer>
  10. // CTK includes
  11. #include "ctkCommandLineParser.h"
  12. namespace
  13. {
  14. // --------------------------------------------------------------------------
  15. class CommandLineParserArgumentDescription
  16. {
  17. public:
  18. CommandLineParserArgumentDescription(
  19. const QString& longArg, const QString& longArgPrefix,
  20. const QString& shortArg, const QString& shortArgPrefix,
  21. QVariant::Type type, const QString& argHelp,
  22. const QVariant& defaultValue, bool ignoreRest,
  23. bool deprecated)
  24. : LongArg(longArg), LongArgPrefix(longArgPrefix),
  25. ShortArg(shortArg), ShortArgPrefix(shortArgPrefix),
  26. ArgHelp(argHelp), IgnoreRest(ignoreRest), NumberOfParametersToProcess(0),
  27. Deprecated(deprecated), DefaultValue(defaultValue), Value(type), ValueType(type)
  28. {
  29. if (defaultValue.isValid())
  30. {
  31. Value = defaultValue;
  32. }
  33. switch (type)
  34. {
  35. case QVariant::String:
  36. {
  37. NumberOfParametersToProcess = 1;
  38. RegularExpression = ".*";
  39. }
  40. break;
  41. case QVariant::Bool:
  42. {
  43. NumberOfParametersToProcess = 0;
  44. RegularExpression = "";
  45. }
  46. break;
  47. case QVariant::StringList:
  48. {
  49. NumberOfParametersToProcess = -1;
  50. RegularExpression = ".*";
  51. }
  52. break;
  53. case QVariant::Int:
  54. {
  55. NumberOfParametersToProcess = 1;
  56. RegularExpression = "-?[0-9]+";
  57. ExactMatchFailedMessage = "A negative or positive integer is expected.";
  58. }
  59. break;
  60. default:
  61. ExactMatchFailedMessage = QString("Type %1 not supported.").arg(static_cast<int>(type));
  62. }
  63. }
  64. ~CommandLineParserArgumentDescription(){}
  65. bool addParameter(const QString& value);
  66. QString helpText(int fieldWidth, const char charPad, const QString& settingsValue = "");
  67. QString LongArg;
  68. QString LongArgPrefix;
  69. QString ShortArg;
  70. QString ShortArgPrefix;
  71. QString ArgHelp;
  72. bool IgnoreRest;
  73. int NumberOfParametersToProcess;
  74. QString RegularExpression;
  75. QString ExactMatchFailedMessage;
  76. bool Deprecated;
  77. QVariant DefaultValue;
  78. QVariant Value;
  79. QVariant::Type ValueType;
  80. };
  81. // --------------------------------------------------------------------------
  82. bool CommandLineParserArgumentDescription::addParameter(const QString& value)
  83. {
  84. if (!RegularExpression.isEmpty())
  85. {
  86. // Validate value
  87. QRegExp regexp(this->RegularExpression);
  88. if (!regexp.exactMatch(value))
  89. {
  90. return false;
  91. }
  92. }
  93. switch (Value.type())
  94. {
  95. case QVariant::String:
  96. {
  97. Value.setValue(value);
  98. }
  99. break;
  100. case QVariant::Bool:
  101. {
  102. Value.setValue(!QString::compare(value, "true", Qt::CaseInsensitive));
  103. }
  104. break;
  105. case QVariant::StringList:
  106. {
  107. if (Value.isNull())
  108. {
  109. QStringList list;
  110. list << value;
  111. Value.setValue(list);
  112. }
  113. else
  114. {
  115. QStringList list = Value.toStringList();
  116. list << value;
  117. Value.setValue(list);
  118. }
  119. }
  120. break;
  121. case QVariant::Int:
  122. {
  123. Value.setValue(value.toInt());
  124. }
  125. break;
  126. default:
  127. return false;
  128. }
  129. return true;
  130. }
  131. // --------------------------------------------------------------------------
  132. QString CommandLineParserArgumentDescription::helpText(int fieldWidth, const char charPad,
  133. const QString& settingsValue)
  134. {
  135. QString text;
  136. QTextStream stream(&text);
  137. stream.setFieldAlignment(QTextStream::AlignLeft);
  138. stream.setPadChar(charPad);
  139. QString shortAndLongArg;
  140. if (!this->ShortArg.isEmpty())
  141. {
  142. shortAndLongArg += QString(" %1%2").arg(this->ShortArgPrefix).arg(this->ShortArg);
  143. }
  144. if (!this->LongArg.isEmpty())
  145. {
  146. if (this->ShortArg.isEmpty())
  147. {
  148. shortAndLongArg.append(" ");
  149. }
  150. else
  151. {
  152. shortAndLongArg.append(", ");
  153. }
  154. shortAndLongArg += QString("%1%2").arg(this->LongArgPrefix).arg(this->LongArg);
  155. }
  156. if(!this->ArgHelp.isEmpty())
  157. {
  158. stream.setFieldWidth(fieldWidth);
  159. }
  160. stream << shortAndLongArg;
  161. stream.setFieldWidth(0);
  162. stream << this->ArgHelp;
  163. if (!settingsValue.isNull())
  164. {
  165. stream << " (default: " << settingsValue << ")";
  166. }
  167. else if (!this->DefaultValue.isNull())
  168. {
  169. stream << " (default: " << this->DefaultValue.toString() << ")";
  170. }
  171. stream << "\n";
  172. return text;
  173. }
  174. }
  175. // --------------------------------------------------------------------------
  176. // ctkCommandLineParser::ctkInternal class
  177. // --------------------------------------------------------------------------
  178. class ctkCommandLineParser::ctkInternal
  179. {
  180. public:
  181. ctkInternal(QSettings* settings)
  182. : Debug(false), FieldWidth(0), UseQSettings(false),
  183. Settings(settings), MergeSettings(true), StrictMode(false)
  184. {}
  185. ~ctkInternal() { qDeleteAll(ArgumentDescriptionList); }
  186. CommandLineParserArgumentDescription* argumentDescription(const QString& argument);
  187. QList<CommandLineParserArgumentDescription*> ArgumentDescriptionList;
  188. QHash<QString, CommandLineParserArgumentDescription*> ArgNameToArgumentDescriptionMap;
  189. QMap<QString, QList<CommandLineParserArgumentDescription*> > GroupToArgumentDescriptionListMap;
  190. QStringList UnparsedArguments;
  191. QStringList ProcessedArguments;
  192. QString ErrorString;
  193. bool Debug;
  194. int FieldWidth;
  195. QString LongPrefix;
  196. QString ShortPrefix;
  197. QString CurrentGroup;
  198. bool UseQSettings;
  199. QPointer<QSettings> Settings;
  200. QString DisableQSettingsLongArg;
  201. QString DisableQSettingsShortArg;
  202. bool MergeSettings;
  203. bool StrictMode;
  204. };
  205. // --------------------------------------------------------------------------
  206. // ctkCommandLineParser::ctkInternal methods
  207. // --------------------------------------------------------------------------
  208. CommandLineParserArgumentDescription*
  209. ctkCommandLineParser::ctkInternal::argumentDescription(const QString& argument)
  210. {
  211. QString unprefixedArg = argument;
  212. if (!LongPrefix.isEmpty() && argument.startsWith(LongPrefix))
  213. {
  214. // Case when (ShortPrefix + UnPrefixedArgument) matches LongPrefix
  215. if (argument == LongPrefix && !ShortPrefix.isEmpty() && argument.startsWith(ShortPrefix))
  216. {
  217. unprefixedArg = argument.mid(ShortPrefix.length());
  218. }
  219. else
  220. {
  221. unprefixedArg = argument.mid(LongPrefix.length());
  222. }
  223. }
  224. else if (!ShortPrefix.isEmpty() && argument.startsWith(ShortPrefix))
  225. {
  226. unprefixedArg = argument.mid(ShortPrefix.length());
  227. }
  228. else if (!LongPrefix.isEmpty() && !ShortPrefix.isEmpty())
  229. {
  230. return 0;
  231. }
  232. if (this->ArgNameToArgumentDescriptionMap.contains(unprefixedArg))
  233. {
  234. return this->ArgNameToArgumentDescriptionMap[unprefixedArg];
  235. }
  236. return 0;
  237. }
  238. // --------------------------------------------------------------------------
  239. // ctkCommandLineParser methods
  240. // --------------------------------------------------------------------------
  241. ctkCommandLineParser::ctkCommandLineParser(QSettings* settings)
  242. {
  243. this->Internal = new ctkInternal(settings);
  244. }
  245. // --------------------------------------------------------------------------
  246. ctkCommandLineParser::~ctkCommandLineParser()
  247. {
  248. delete this->Internal;
  249. }
  250. // --------------------------------------------------------------------------
  251. QHash<QString, QVariant> ctkCommandLineParser::parseArguments(const QStringList& arguments,
  252. bool* ok)
  253. {
  254. // Reset
  255. this->Internal->UnparsedArguments.clear();
  256. this->Internal->ProcessedArguments.clear();
  257. this->Internal->ErrorString.clear();
  258. foreach (CommandLineParserArgumentDescription* desc,
  259. this->Internal->ArgumentDescriptionList)
  260. {
  261. desc->Value = QVariant(desc->ValueType);
  262. if (desc->DefaultValue.isValid())
  263. {
  264. desc->Value = desc->DefaultValue;
  265. }
  266. }
  267. bool error = false;
  268. bool ignoreRest = false;
  269. bool useSettings = this->Internal->UseQSettings;
  270. CommandLineParserArgumentDescription * currentArgDesc = 0;
  271. QList<CommandLineParserArgumentDescription*> parsedArgDescriptions;
  272. for(int i = 1; i < arguments.size(); ++i)
  273. {
  274. QString argument = arguments.at(i);
  275. if (this->Internal->Debug) { qDebug() << "Processing" << argument; }
  276. // should argument be ignored ?
  277. if (ignoreRest)
  278. {
  279. if (this->Internal->Debug)
  280. {
  281. qDebug() << " Skipping: IgnoreRest flag was been set";
  282. }
  283. this->Internal->UnparsedArguments << argument;
  284. continue;
  285. }
  286. // Skip if the argument does not start with the defined prefix
  287. if (!(argument.startsWith(this->Internal->LongPrefix)
  288. || argument.startsWith(this->Internal->ShortPrefix)))
  289. {
  290. if (this->Internal->StrictMode)
  291. {
  292. this->Internal->ErrorString = QString("Unknown argument %1").arg(argument);
  293. error = true;
  294. break;
  295. }
  296. if (this->Internal->Debug)
  297. {
  298. qDebug() << " Skipping: It does not start with the defined prefix";
  299. }
  300. this->Internal->UnparsedArguments << argument;
  301. continue;
  302. }
  303. // Skip if argument has already been parsed ...
  304. if (this->Internal->ProcessedArguments.contains(argument))
  305. {
  306. if (this->Internal->StrictMode)
  307. {
  308. this->Internal->ErrorString = QString("Argument %1 already processed !").arg(argument);
  309. error = true;
  310. break;
  311. }
  312. if (this->Internal->Debug)
  313. {
  314. qDebug() << " Skipping: Already processed !";
  315. }
  316. continue;
  317. }
  318. // Retrieve corresponding argument description
  319. currentArgDesc = this->Internal->argumentDescription(argument);
  320. // Is there a corresponding argument description ?
  321. if (currentArgDesc)
  322. {
  323. // If the argument is deprecated, print the help text but continue processing
  324. if (currentArgDesc->Deprecated)
  325. {
  326. qWarning().nospace() << "Deprecated argument " << argument << ": " << currentArgDesc->ArgHelp;
  327. }
  328. else
  329. {
  330. parsedArgDescriptions.push_back(currentArgDesc);
  331. }
  332. // Is the argument the special "disable QSettings" argument?
  333. if ((!currentArgDesc->LongArg.isEmpty() && currentArgDesc->LongArg == this->Internal->DisableQSettingsLongArg)
  334. || (!currentArgDesc->ShortArg.isEmpty() && currentArgDesc->ShortArg == this->Internal->DisableQSettingsShortArg))
  335. {
  336. useSettings = false;
  337. }
  338. this->Internal->ProcessedArguments << currentArgDesc->ShortArg << currentArgDesc->LongArg;
  339. int numberOfParametersToProcess = currentArgDesc->NumberOfParametersToProcess;
  340. ignoreRest = currentArgDesc->IgnoreRest;
  341. if (this->Internal->Debug && ignoreRest)
  342. {
  343. qDebug() << " IgnoreRest flag is True";
  344. }
  345. // Is the number of parameters associated with the argument being processed known ?
  346. if (numberOfParametersToProcess == 0)
  347. {
  348. currentArgDesc->addParameter("true");
  349. }
  350. else if (numberOfParametersToProcess > 0)
  351. {
  352. QString missingParameterError =
  353. "Argument %1 has %2 value(s) associated whereas exacly %3 are expected.";
  354. for(int j=1; j <= numberOfParametersToProcess; ++j)
  355. {
  356. if (i + j >= arguments.size())
  357. {
  358. this->Internal->ErrorString =
  359. missingParameterError.arg(argument).arg(j-1).arg(numberOfParametersToProcess);
  360. if (this->Internal->Debug) { qDebug() << this->Internal->ErrorString; }
  361. if (ok) { *ok = false; }
  362. return QHash<QString, QVariant>();
  363. }
  364. QString parameter = arguments.at(i + j);
  365. if (this->Internal->Debug)
  366. {
  367. qDebug() << " Processing parameter" << j << ", value:" << parameter;
  368. }
  369. if (this->argumentAdded(parameter))
  370. {
  371. this->Internal->ErrorString =
  372. missingParameterError.arg(argument).arg(j-1).arg(numberOfParametersToProcess);
  373. if (this->Internal->Debug) { qDebug() << this->Internal->ErrorString; }
  374. if (ok) { *ok = false; }
  375. return QHash<QString, QVariant>();
  376. }
  377. if (!currentArgDesc->addParameter(parameter))
  378. {
  379. this->Internal->ErrorString = QString(
  380. "Value(s) associated with argument %1 are incorrect. %2").
  381. arg(argument).arg(currentArgDesc->ExactMatchFailedMessage);
  382. if (this->Internal->Debug) { qDebug() << this->Internal->ErrorString; }
  383. if (ok) { *ok = false; }
  384. return QHash<QString, QVariant>();
  385. }
  386. }
  387. // Update main loop increment
  388. i = i + numberOfParametersToProcess;
  389. }
  390. else if (numberOfParametersToProcess == -1)
  391. {
  392. if (this->Internal->Debug)
  393. {
  394. qDebug() << " Proccessing StringList ...";
  395. }
  396. int j = 1;
  397. while(j + i < arguments.size())
  398. {
  399. if (this->argumentAdded(arguments.at(j + i)))
  400. {
  401. if (this->Internal->Debug)
  402. {
  403. qDebug() << " No more parameter for" << argument;
  404. }
  405. break;
  406. }
  407. QString parameter = arguments.at(j + i);
  408. if (this->Internal->Debug)
  409. {
  410. qDebug() << " Processing parameter" << j << ", value:" << parameter;
  411. }
  412. if (!currentArgDesc->addParameter(parameter))
  413. {
  414. this->Internal->ErrorString = QString(
  415. "Value(s) associated with argument %1 are incorrect. %2").
  416. arg(argument).arg(currentArgDesc->ExactMatchFailedMessage);
  417. if (this->Internal->Debug) { qDebug() << this->Internal->ErrorString; }
  418. if (ok) { *ok = false; }
  419. return QHash<QString, QVariant>();
  420. }
  421. j++;
  422. }
  423. // Update main loop increment
  424. i = i + j;
  425. }
  426. }
  427. else
  428. {
  429. if (this->Internal->StrictMode)
  430. {
  431. this->Internal->ErrorString = QString("Unknown argument %1").arg(argument);
  432. error = true;
  433. break;
  434. }
  435. if (this->Internal->Debug)
  436. {
  437. qDebug() << " Skipping: Unknown argument";
  438. }
  439. this->Internal->UnparsedArguments << argument;
  440. }
  441. }
  442. if (ok)
  443. {
  444. *ok = !error;
  445. }
  446. QSettings* settings = 0;
  447. if (this->Internal->UseQSettings && useSettings)
  448. {
  449. if (this->Internal->Settings)
  450. {
  451. settings = this->Internal->Settings;
  452. }
  453. else
  454. {
  455. // Use a default constructed QSettings instance
  456. settings = new QSettings();
  457. }
  458. }
  459. QHash<QString, QVariant> parsedArguments;
  460. QListIterator<CommandLineParserArgumentDescription*> it(this->Internal->ArgumentDescriptionList);
  461. while (it.hasNext())
  462. {
  463. QString key;
  464. CommandLineParserArgumentDescription* desc = it.next();
  465. if (!desc->LongArg.isEmpty())
  466. {
  467. key = desc->LongArg;
  468. }
  469. else
  470. {
  471. key = desc->ShortArg;
  472. }
  473. if (parsedArgDescriptions.contains(desc))
  474. {
  475. // The argument was supplied on the command line, so use the given value
  476. if (this->Internal->MergeSettings && settings)
  477. {
  478. // Merge with QSettings
  479. QVariant settingsVal = settings->value(key);
  480. if (desc->ValueType == QVariant::StringList &&
  481. settingsVal.canConvert(QVariant::StringList))
  482. {
  483. QStringList stringList = desc->Value.toStringList();
  484. stringList.append(settingsVal.toStringList());
  485. parsedArguments.insert(key, stringList);
  486. }
  487. else
  488. {
  489. // do a normal insert
  490. parsedArguments.insert(key, desc->Value);
  491. }
  492. }
  493. else
  494. {
  495. // No merging, just insert all user values
  496. parsedArguments.insert(key, desc->Value);
  497. }
  498. }
  499. else
  500. {
  501. if (settings)
  502. {
  503. // If there is a valid QSettings entry for the argument, use the value
  504. QVariant settingsVal = settings->value(key, desc->Value);
  505. if (!settingsVal.isNull())
  506. {
  507. parsedArguments.insert(key, settingsVal);
  508. }
  509. }
  510. else
  511. {
  512. // Just insert the arguments with valid default values
  513. if (!desc->Value.isNull())
  514. {
  515. parsedArguments.insert(key, desc->Value);
  516. }
  517. }
  518. }
  519. }
  520. // If we created a default QSettings instance, delete it
  521. if (settings && !this->Internal->Settings)
  522. {
  523. delete settings;
  524. }
  525. return parsedArguments;
  526. }
  527. // -------------------------------------------------------------------------
  528. QHash<QString, QVariant> ctkCommandLineParser::parseArguments(int argc, char** argv, bool* ok)
  529. {
  530. QStringList arguments;
  531. // Create a QStringList of arguments
  532. for(int i = 0; i < argc; ++i)
  533. {
  534. arguments << argv[i];
  535. }
  536. return this->parseArguments(arguments, ok);
  537. }
  538. // -------------------------------------------------------------------------
  539. QString ctkCommandLineParser::errorString() const
  540. {
  541. return this->Internal->ErrorString;
  542. }
  543. // -------------------------------------------------------------------------
  544. const QStringList& ctkCommandLineParser::unparsedArguments() const
  545. {
  546. return this->Internal->UnparsedArguments;
  547. }
  548. // --------------------------------------------------------------------------
  549. void ctkCommandLineParser::addArgument(const QString& longarg, const QString& shortarg,
  550. QVariant::Type type, const QString& argHelp,
  551. const QVariant& defaultValue, bool ignoreRest,
  552. bool deprecated)
  553. {
  554. Q_ASSERT_X(!(longarg.isEmpty() && shortarg.isEmpty()), "addArgument",
  555. "both long and short argument names are empty");
  556. if (longarg.isEmpty() && shortarg.isEmpty()) { return; }
  557. Q_ASSERT_X(!defaultValue.isValid() || defaultValue.type() == type, "addArgument",
  558. "defaultValue type does not match");
  559. if (defaultValue.isValid() && defaultValue.type() != type)
  560. throw std::logic_error("The QVariant type of defaultValue does not match the specified type");
  561. /* Make sure it's not already added */
  562. bool added = this->Internal->ArgNameToArgumentDescriptionMap.contains(longarg);
  563. Q_ASSERT_X(!added, "addArgument", "long argument already added");
  564. if (added) { return; }
  565. added = this->Internal->ArgNameToArgumentDescriptionMap.contains(shortarg);
  566. Q_ASSERT_X(!added, "addArgument", "short argument already added");
  567. if (added) { return; }
  568. CommandLineParserArgumentDescription* argDesc =
  569. new CommandLineParserArgumentDescription(longarg, this->Internal->LongPrefix,
  570. shortarg, this->Internal->ShortPrefix, type,
  571. argHelp, defaultValue, ignoreRest, deprecated);
  572. int argWidth = 0;
  573. if (!longarg.isEmpty())
  574. {
  575. this->Internal->ArgNameToArgumentDescriptionMap[longarg] = argDesc;
  576. argWidth += longarg.length() + this->Internal->LongPrefix.length();
  577. }
  578. if (!shortarg.isEmpty())
  579. {
  580. this->Internal->ArgNameToArgumentDescriptionMap[shortarg] = argDesc;
  581. argWidth += shortarg.length() + this->Internal->ShortPrefix.length() + 2;
  582. }
  583. argWidth += 5;
  584. // Set the field width for the arguments
  585. if (argWidth > this->Internal->FieldWidth)
  586. {
  587. this->Internal->FieldWidth = argWidth;
  588. }
  589. this->Internal->ArgumentDescriptionList << argDesc;
  590. this->Internal->GroupToArgumentDescriptionListMap[this->Internal->CurrentGroup] << argDesc;
  591. }
  592. // --------------------------------------------------------------------------
  593. void ctkCommandLineParser::addDeprecatedArgument(
  594. const QString& longarg, const QString& shortarg, const QString& argHelp)
  595. {
  596. addArgument(longarg, shortarg, QVariant::StringList, argHelp, QVariant(), false, true);
  597. }
  598. // --------------------------------------------------------------------------
  599. bool ctkCommandLineParser::setExactMatchRegularExpression(
  600. const QString& argument, const QString& expression, const QString& exactMatchFailedMessage)
  601. {
  602. CommandLineParserArgumentDescription * argDesc =
  603. this->Internal->argumentDescription(argument);
  604. if (!argDesc)
  605. {
  606. return false;
  607. }
  608. if (argDesc->Value.type() == QVariant::Bool)
  609. {
  610. return false;
  611. }
  612. argDesc->RegularExpression = expression;
  613. argDesc->ExactMatchFailedMessage = exactMatchFailedMessage;
  614. return true;
  615. }
  616. // --------------------------------------------------------------------------
  617. int ctkCommandLineParser::fieldWidth() const
  618. {
  619. return this->Internal->FieldWidth;
  620. }
  621. // --------------------------------------------------------------------------
  622. void ctkCommandLineParser::beginGroup(const QString& description)
  623. {
  624. this->Internal->CurrentGroup = description;
  625. }
  626. // --------------------------------------------------------------------------
  627. void ctkCommandLineParser::endGroup()
  628. {
  629. this->Internal->CurrentGroup.clear();
  630. }
  631. // --------------------------------------------------------------------------
  632. void ctkCommandLineParser::enableSettings(const QString& disableLongArg, const QString& disableShortArg)
  633. {
  634. this->Internal->UseQSettings = true;
  635. this->Internal->DisableQSettingsLongArg = disableLongArg;
  636. this->Internal->DisableQSettingsShortArg = disableShortArg;
  637. }
  638. // --------------------------------------------------------------------------
  639. void ctkCommandLineParser::mergeSettings(bool merge)
  640. {
  641. this->Internal->MergeSettings = merge;
  642. }
  643. // --------------------------------------------------------------------------
  644. bool ctkCommandLineParser::settingsEnabled() const
  645. {
  646. return this->Internal->UseQSettings;
  647. }
  648. // --------------------------------------------------------------------------
  649. QString ctkCommandLineParser::helpText(const char charPad) const
  650. {
  651. QString text;
  652. QTextStream stream(&text);
  653. QList<CommandLineParserArgumentDescription*> deprecatedArgs;
  654. // Loop over grouped argument descriptions
  655. QMapIterator<QString, QList<CommandLineParserArgumentDescription*> > it(
  656. this->Internal->GroupToArgumentDescriptionListMap);
  657. while(it.hasNext())
  658. {
  659. it.next();
  660. if (!it.key().isEmpty())
  661. {
  662. stream << "\n" << it.key() << "\n";
  663. }
  664. foreach(CommandLineParserArgumentDescription* argDesc, it.value())
  665. {
  666. if (argDesc->Deprecated)
  667. {
  668. deprecatedArgs << argDesc;
  669. }
  670. else
  671. {
  672. // Extract associated value from settings if any
  673. QString settingsValue;
  674. if (this->Internal->Settings)
  675. {
  676. QString key;
  677. if (!argDesc->LongArg.isEmpty())
  678. {
  679. key = argDesc->LongArg;
  680. }
  681. else
  682. {
  683. key = argDesc->ShortArg;
  684. }
  685. settingsValue = this->Internal->Settings->value(key).toString();
  686. }
  687. stream << argDesc->helpText(this->Internal->FieldWidth, charPad, settingsValue);
  688. }
  689. }
  690. }
  691. if (!deprecatedArgs.empty())
  692. {
  693. stream << "\nDeprecated arguments:\n";
  694. foreach(CommandLineParserArgumentDescription* argDesc, deprecatedArgs)
  695. {
  696. stream << argDesc->helpText(this->Internal->FieldWidth, charPad);
  697. }
  698. }
  699. return text;
  700. }
  701. // --------------------------------------------------------------------------
  702. bool ctkCommandLineParser::argumentAdded(const QString& argument) const
  703. {
  704. return this->Internal->ArgNameToArgumentDescriptionMap.contains(argument);
  705. }
  706. // --------------------------------------------------------------------------
  707. bool ctkCommandLineParser::argumentParsed(const QString& argument) const
  708. {
  709. return this->Internal->ProcessedArguments.contains(argument);
  710. }
  711. // --------------------------------------------------------------------------
  712. void ctkCommandLineParser::setArgumentPrefix(const QString& longPrefix, const QString& shortPrefix)
  713. {
  714. this->Internal->LongPrefix = longPrefix;
  715. this->Internal->ShortPrefix = shortPrefix;
  716. }
  717. // --------------------------------------------------------------------------
  718. void ctkCommandLineParser::setStrictModeEnabled(bool strictMode)
  719. {
  720. this->Internal->StrictMode = strictMode;
  721. }