ctkCommandLineParser.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  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(QObject* newParent) : Superclass(newParent)
  242. {
  243. this->Internal = new ctkInternal(0);
  244. }
  245. // --------------------------------------------------------------------------
  246. ctkCommandLineParser::ctkCommandLineParser(QSettings* settings, QObject* newParent) :
  247. Superclass(newParent)
  248. {
  249. this->Internal = new ctkInternal(settings);
  250. }
  251. // --------------------------------------------------------------------------
  252. ctkCommandLineParser::~ctkCommandLineParser()
  253. {
  254. delete this->Internal;
  255. }
  256. // --------------------------------------------------------------------------
  257. QHash<QString, QVariant> ctkCommandLineParser::parseArguments(const QStringList& arguments,
  258. bool* ok)
  259. {
  260. // Reset
  261. this->Internal->UnparsedArguments.clear();
  262. this->Internal->ProcessedArguments.clear();
  263. this->Internal->ErrorString.clear();
  264. foreach (CommandLineParserArgumentDescription* desc,
  265. this->Internal->ArgumentDescriptionList)
  266. {
  267. desc->Value = QVariant(desc->ValueType);
  268. if (desc->DefaultValue.isValid())
  269. {
  270. desc->Value = desc->DefaultValue;
  271. }
  272. }
  273. bool error = false;
  274. bool ignoreRest = false;
  275. bool useSettings = this->Internal->UseQSettings;
  276. CommandLineParserArgumentDescription * currentArgDesc = 0;
  277. QList<CommandLineParserArgumentDescription*> parsedArgDescriptions;
  278. for(int i = 1; i < arguments.size(); ++i)
  279. {
  280. QString argument = arguments.at(i);
  281. if (this->Internal->Debug) { qDebug() << "Processing" << argument; }
  282. // should argument be ignored ?
  283. if (ignoreRest)
  284. {
  285. if (this->Internal->Debug)
  286. {
  287. qDebug() << " Skipping: IgnoreRest flag was been set";
  288. }
  289. this->Internal->UnparsedArguments << argument;
  290. continue;
  291. }
  292. // Skip if the argument does not start with the defined prefix
  293. if (!(argument.startsWith(this->Internal->LongPrefix)
  294. || argument.startsWith(this->Internal->ShortPrefix)))
  295. {
  296. if (this->Internal->StrictMode)
  297. {
  298. this->Internal->ErrorString = QString("Unknown argument %1").arg(argument);
  299. error = true;
  300. break;
  301. }
  302. if (this->Internal->Debug)
  303. {
  304. qDebug() << " Skipping: It does not start with the defined prefix";
  305. }
  306. this->Internal->UnparsedArguments << argument;
  307. continue;
  308. }
  309. // Skip if argument has already been parsed ...
  310. if (this->Internal->ProcessedArguments.contains(argument))
  311. {
  312. if (this->Internal->StrictMode)
  313. {
  314. this->Internal->ErrorString = QString("Argument %1 already processed !").arg(argument);
  315. error = true;
  316. break;
  317. }
  318. if (this->Internal->Debug)
  319. {
  320. qDebug() << " Skipping: Already processed !";
  321. }
  322. continue;
  323. }
  324. // Retrieve corresponding argument description
  325. currentArgDesc = this->Internal->argumentDescription(argument);
  326. // Is there a corresponding argument description ?
  327. if (currentArgDesc)
  328. {
  329. // If the argument is deprecated, print the help text but continue processing
  330. if (currentArgDesc->Deprecated)
  331. {
  332. qWarning().nospace() << "Deprecated argument " << argument << ": " << currentArgDesc->ArgHelp;
  333. }
  334. else
  335. {
  336. parsedArgDescriptions.push_back(currentArgDesc);
  337. }
  338. // Is the argument the special "disable QSettings" argument?
  339. if ((!currentArgDesc->LongArg.isEmpty() && currentArgDesc->LongArg == this->Internal->DisableQSettingsLongArg)
  340. || (!currentArgDesc->ShortArg.isEmpty() && currentArgDesc->ShortArg == this->Internal->DisableQSettingsShortArg))
  341. {
  342. useSettings = false;
  343. }
  344. this->Internal->ProcessedArguments << currentArgDesc->ShortArg << currentArgDesc->LongArg;
  345. int numberOfParametersToProcess = currentArgDesc->NumberOfParametersToProcess;
  346. ignoreRest = currentArgDesc->IgnoreRest;
  347. if (this->Internal->Debug && ignoreRest)
  348. {
  349. qDebug() << " IgnoreRest flag is True";
  350. }
  351. // Is the number of parameters associated with the argument being processed known ?
  352. if (numberOfParametersToProcess == 0)
  353. {
  354. currentArgDesc->addParameter("true");
  355. }
  356. else if (numberOfParametersToProcess > 0)
  357. {
  358. QString missingParameterError =
  359. "Argument %1 has %2 value(s) associated whereas exacly %3 are expected.";
  360. for(int j=1; j <= numberOfParametersToProcess; ++j)
  361. {
  362. if (i + j >= arguments.size())
  363. {
  364. this->Internal->ErrorString =
  365. missingParameterError.arg(argument).arg(j-1).arg(numberOfParametersToProcess);
  366. if (this->Internal->Debug) { qDebug() << this->Internal->ErrorString; }
  367. if (ok) { *ok = false; }
  368. return QHash<QString, QVariant>();
  369. }
  370. QString parameter = arguments.at(i + j);
  371. if (this->Internal->Debug)
  372. {
  373. qDebug() << " Processing parameter" << j << ", value:" << parameter;
  374. }
  375. if (this->argumentAdded(parameter))
  376. {
  377. this->Internal->ErrorString =
  378. missingParameterError.arg(argument).arg(j-1).arg(numberOfParametersToProcess);
  379. if (this->Internal->Debug) { qDebug() << this->Internal->ErrorString; }
  380. if (ok) { *ok = false; }
  381. return QHash<QString, QVariant>();
  382. }
  383. if (!currentArgDesc->addParameter(parameter))
  384. {
  385. this->Internal->ErrorString = QString(
  386. "Value(s) associated with argument %1 are incorrect. %2").
  387. arg(argument).arg(currentArgDesc->ExactMatchFailedMessage);
  388. if (this->Internal->Debug) { qDebug() << this->Internal->ErrorString; }
  389. if (ok) { *ok = false; }
  390. return QHash<QString, QVariant>();
  391. }
  392. }
  393. // Update main loop increment
  394. i = i + numberOfParametersToProcess;
  395. }
  396. else if (numberOfParametersToProcess == -1)
  397. {
  398. if (this->Internal->Debug)
  399. {
  400. qDebug() << " Proccessing StringList ...";
  401. }
  402. int j = 1;
  403. while(j + i < arguments.size())
  404. {
  405. if (this->argumentAdded(arguments.at(j + i)))
  406. {
  407. if (this->Internal->Debug)
  408. {
  409. qDebug() << " No more parameter for" << argument;
  410. }
  411. break;
  412. }
  413. QString parameter = arguments.at(j + i);
  414. if (this->Internal->Debug)
  415. {
  416. qDebug() << " Processing parameter" << j << ", value:" << parameter;
  417. }
  418. if (!currentArgDesc->addParameter(parameter))
  419. {
  420. this->Internal->ErrorString = QString(
  421. "Value(s) associated with argument %1 are incorrect. %2").
  422. arg(argument).arg(currentArgDesc->ExactMatchFailedMessage);
  423. if (this->Internal->Debug) { qDebug() << this->Internal->ErrorString; }
  424. if (ok) { *ok = false; }
  425. return QHash<QString, QVariant>();
  426. }
  427. j++;
  428. }
  429. // Update main loop increment
  430. i = i + j;
  431. }
  432. }
  433. else
  434. {
  435. if (this->Internal->StrictMode)
  436. {
  437. this->Internal->ErrorString = QString("Unknown argument %1").arg(argument);
  438. error = true;
  439. break;
  440. }
  441. if (this->Internal->Debug)
  442. {
  443. qDebug() << " Skipping: Unknown argument";
  444. }
  445. this->Internal->UnparsedArguments << argument;
  446. }
  447. }
  448. if (ok)
  449. {
  450. *ok = !error;
  451. }
  452. QSettings* settings = 0;
  453. if (this->Internal->UseQSettings && useSettings)
  454. {
  455. if (this->Internal->Settings)
  456. {
  457. settings = this->Internal->Settings;
  458. }
  459. else
  460. {
  461. // Use a default constructed QSettings instance
  462. settings = new QSettings();
  463. }
  464. }
  465. QHash<QString, QVariant> parsedArguments;
  466. QListIterator<CommandLineParserArgumentDescription*> it(this->Internal->ArgumentDescriptionList);
  467. while (it.hasNext())
  468. {
  469. QString key;
  470. CommandLineParserArgumentDescription* desc = it.next();
  471. if (!desc->LongArg.isEmpty())
  472. {
  473. key = desc->LongArg;
  474. }
  475. else
  476. {
  477. key = desc->ShortArg;
  478. }
  479. if (parsedArgDescriptions.contains(desc))
  480. {
  481. // The argument was supplied on the command line, so use the given value
  482. if (this->Internal->MergeSettings && settings)
  483. {
  484. // Merge with QSettings
  485. QVariant settingsVal = settings->value(key);
  486. if (desc->ValueType == QVariant::StringList &&
  487. settingsVal.canConvert(QVariant::StringList))
  488. {
  489. QStringList stringList = desc->Value.toStringList();
  490. stringList.append(settingsVal.toStringList());
  491. parsedArguments.insert(key, stringList);
  492. }
  493. else
  494. {
  495. // do a normal insert
  496. parsedArguments.insert(key, desc->Value);
  497. }
  498. }
  499. else
  500. {
  501. // No merging, just insert all user values
  502. parsedArguments.insert(key, desc->Value);
  503. }
  504. }
  505. else
  506. {
  507. if (settings)
  508. {
  509. // If there is a valid QSettings entry for the argument, use the value
  510. QVariant settingsVal = settings->value(key, desc->Value);
  511. if (!settingsVal.isNull())
  512. {
  513. parsedArguments.insert(key, settingsVal);
  514. }
  515. }
  516. else
  517. {
  518. // Just insert the arguments with valid default values
  519. if (!desc->Value.isNull())
  520. {
  521. parsedArguments.insert(key, desc->Value);
  522. }
  523. }
  524. }
  525. }
  526. // If we created a default QSettings instance, delete it
  527. if (settings && !this->Internal->Settings)
  528. {
  529. delete settings;
  530. }
  531. return parsedArguments;
  532. }
  533. // -------------------------------------------------------------------------
  534. QHash<QString, QVariant> ctkCommandLineParser::parseArguments(int argc, char** argv, bool* ok)
  535. {
  536. QStringList arguments;
  537. // Create a QStringList of arguments
  538. for(int i = 0; i < argc; ++i)
  539. {
  540. arguments << argv[i];
  541. }
  542. return this->parseArguments(arguments, ok);
  543. }
  544. // -------------------------------------------------------------------------
  545. QString ctkCommandLineParser::errorString() const
  546. {
  547. return this->Internal->ErrorString;
  548. }
  549. // -------------------------------------------------------------------------
  550. const QStringList& ctkCommandLineParser::unparsedArguments() const
  551. {
  552. return this->Internal->UnparsedArguments;
  553. }
  554. // --------------------------------------------------------------------------
  555. void ctkCommandLineParser::addArgument(const QString& longarg, const QString& shortarg,
  556. QVariant::Type type, const QString& argHelp,
  557. const QVariant& defaultValue, bool ignoreRest,
  558. bool deprecated)
  559. {
  560. Q_ASSERT_X(!(longarg.isEmpty() && shortarg.isEmpty()), "addArgument",
  561. "both long and short argument names are empty");
  562. if (longarg.isEmpty() && shortarg.isEmpty()) { return; }
  563. Q_ASSERT_X(!defaultValue.isValid() || defaultValue.type() == type, "addArgument",
  564. "defaultValue type does not match");
  565. if (defaultValue.isValid() && defaultValue.type() != type)
  566. throw std::logic_error("The QVariant type of defaultValue does not match the specified type");
  567. /* Make sure it's not already added */
  568. bool added = this->Internal->ArgNameToArgumentDescriptionMap.contains(longarg);
  569. Q_ASSERT_X(!added, "addArgument", "long argument already added");
  570. if (added) { return; }
  571. added = this->Internal->ArgNameToArgumentDescriptionMap.contains(shortarg);
  572. Q_ASSERT_X(!added, "addArgument", "short argument already added");
  573. if (added) { return; }
  574. CommandLineParserArgumentDescription* argDesc =
  575. new CommandLineParserArgumentDescription(longarg, this->Internal->LongPrefix,
  576. shortarg, this->Internal->ShortPrefix, type,
  577. argHelp, defaultValue, ignoreRest, deprecated);
  578. int argWidth = 0;
  579. if (!longarg.isEmpty())
  580. {
  581. this->Internal->ArgNameToArgumentDescriptionMap[longarg] = argDesc;
  582. argWidth += longarg.length() + this->Internal->LongPrefix.length();
  583. }
  584. if (!shortarg.isEmpty())
  585. {
  586. this->Internal->ArgNameToArgumentDescriptionMap[shortarg] = argDesc;
  587. argWidth += shortarg.length() + this->Internal->ShortPrefix.length() + 2;
  588. }
  589. argWidth += 5;
  590. // Set the field width for the arguments
  591. if (argWidth > this->Internal->FieldWidth)
  592. {
  593. this->Internal->FieldWidth = argWidth;
  594. }
  595. this->Internal->ArgumentDescriptionList << argDesc;
  596. this->Internal->GroupToArgumentDescriptionListMap[this->Internal->CurrentGroup] << argDesc;
  597. }
  598. // --------------------------------------------------------------------------
  599. void ctkCommandLineParser::addDeprecatedArgument(
  600. const QString& longarg, const QString& shortarg, const QString& argHelp)
  601. {
  602. addArgument(longarg, shortarg, QVariant::StringList, argHelp, QVariant(), false, true);
  603. }
  604. // --------------------------------------------------------------------------
  605. bool ctkCommandLineParser::setExactMatchRegularExpression(
  606. const QString& argument, const QString& expression, const QString& exactMatchFailedMessage)
  607. {
  608. CommandLineParserArgumentDescription * argDesc =
  609. this->Internal->argumentDescription(argument);
  610. if (!argDesc)
  611. {
  612. return false;
  613. }
  614. if (argDesc->Value.type() == QVariant::Bool)
  615. {
  616. return false;
  617. }
  618. argDesc->RegularExpression = expression;
  619. argDesc->ExactMatchFailedMessage = exactMatchFailedMessage;
  620. return true;
  621. }
  622. // --------------------------------------------------------------------------
  623. int ctkCommandLineParser::fieldWidth() const
  624. {
  625. return this->Internal->FieldWidth;
  626. }
  627. // --------------------------------------------------------------------------
  628. void ctkCommandLineParser::beginGroup(const QString& description)
  629. {
  630. this->Internal->CurrentGroup = description;
  631. }
  632. // --------------------------------------------------------------------------
  633. void ctkCommandLineParser::endGroup()
  634. {
  635. this->Internal->CurrentGroup.clear();
  636. }
  637. // --------------------------------------------------------------------------
  638. void ctkCommandLineParser::enableSettings(const QString& disableLongArg, const QString& disableShortArg)
  639. {
  640. this->Internal->UseQSettings = true;
  641. this->Internal->DisableQSettingsLongArg = disableLongArg;
  642. this->Internal->DisableQSettingsShortArg = disableShortArg;
  643. }
  644. // --------------------------------------------------------------------------
  645. void ctkCommandLineParser::mergeSettings(bool merge)
  646. {
  647. this->Internal->MergeSettings = merge;
  648. }
  649. // --------------------------------------------------------------------------
  650. bool ctkCommandLineParser::settingsEnabled() const
  651. {
  652. return this->Internal->UseQSettings;
  653. }
  654. // --------------------------------------------------------------------------
  655. QString ctkCommandLineParser::helpText(const char charPad) const
  656. {
  657. QString text;
  658. QTextStream stream(&text);
  659. QList<CommandLineParserArgumentDescription*> deprecatedArgs;
  660. // Loop over grouped argument descriptions
  661. QMapIterator<QString, QList<CommandLineParserArgumentDescription*> > it(
  662. this->Internal->GroupToArgumentDescriptionListMap);
  663. while(it.hasNext())
  664. {
  665. it.next();
  666. if (!it.key().isEmpty())
  667. {
  668. stream << "\n" << it.key() << "\n";
  669. }
  670. foreach(CommandLineParserArgumentDescription* argDesc, it.value())
  671. {
  672. if (argDesc->Deprecated)
  673. {
  674. deprecatedArgs << argDesc;
  675. }
  676. else
  677. {
  678. // Extract associated value from settings if any
  679. QString settingsValue;
  680. if (this->Internal->Settings)
  681. {
  682. QString key;
  683. if (!argDesc->LongArg.isEmpty())
  684. {
  685. key = argDesc->LongArg;
  686. }
  687. else
  688. {
  689. key = argDesc->ShortArg;
  690. }
  691. settingsValue = this->Internal->Settings->value(key).toString();
  692. }
  693. stream << argDesc->helpText(this->Internal->FieldWidth, charPad, settingsValue);
  694. }
  695. }
  696. }
  697. if (!deprecatedArgs.empty())
  698. {
  699. stream << "\nDeprecated arguments:\n";
  700. foreach(CommandLineParserArgumentDescription* argDesc, deprecatedArgs)
  701. {
  702. stream << argDesc->helpText(this->Internal->FieldWidth, charPad);
  703. }
  704. }
  705. return text;
  706. }
  707. // --------------------------------------------------------------------------
  708. bool ctkCommandLineParser::argumentAdded(const QString& argument) const
  709. {
  710. return this->Internal->ArgNameToArgumentDescriptionMap.contains(argument);
  711. }
  712. // --------------------------------------------------------------------------
  713. bool ctkCommandLineParser::argumentParsed(const QString& argument) const
  714. {
  715. return this->Internal->ProcessedArguments.contains(argument);
  716. }
  717. // --------------------------------------------------------------------------
  718. void ctkCommandLineParser::setArgumentPrefix(const QString& longPrefix, const QString& shortPrefix)
  719. {
  720. this->Internal->LongPrefix = longPrefix;
  721. this->Internal->ShortPrefix = shortPrefix;
  722. }
  723. // --------------------------------------------------------------------------
  724. void ctkCommandLineParser::setStrictModeEnabled(bool strictMode)
  725. {
  726. this->Internal->StrictMode = strictMode;
  727. }