ctkCommandLineParser.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #ifndef __ctkCommandLineParser_h
  2. #define __ctkCommandLineParser_h
  3. // Qt includes
  4. #include <QString>
  5. #include <QStringList>
  6. #include <QVariant>
  7. // CTK includes
  8. #include "CTKCoreExport.h"
  9. // --------------------------------------------------------------------------
  10. class CTK_CORE_EXPORT ctkCommandLineParser
  11. {
  12. public:
  13. ctkCommandLineParser();
  14. ~ctkCommandLineParser();
  15. /**
  16. * Parse a given list of command line arguments.
  17. *
  18. * This method parses a list of QString elements considering the known arguments
  19. * added by calls to <code>addArgument()</code>. If any one of the argument
  20. * values does not match the corresponding regular expression,
  21. * <code>ok</code> is set to false and an empty QHash object is returned.
  22. *
  23. * The keys in the returned QHash object correspond to the long argument string,
  24. * if it is not empty. Otherwise, the short argument string is used as key. The
  25. * QVariant values can safely be converted to the type specified in the
  26. * <code>addArgument()</code> method call.
  27. *
  28. * @param arguments A QStringList containing command line arguments. Usually
  29. * given by <code>QCoreApplication::arguments()</code>.
  30. * @param ok A pointer to a boolean variable. Will be set to <code>true</code>
  31. * if all regular expressions matched, <code>false</code> otherwise.
  32. * @return A QHash object mapping the long argument (if empty, the short one)
  33. * to a QVariant containing the value.
  34. */
  35. QHash<QString, QVariant> parseArguments(const QStringList &arguments, bool* ok = 0);
  36. /**
  37. * Returns a detailed error description if a call to <code>parseArguments()</code>
  38. * failed.
  39. *
  40. * @return The error description, empty if no error occured.
  41. * @see parseArguments(const QStringList&, bool*)
  42. */
  43. QString errorString();
  44. /**
  45. * This method returns all unparsed arguments, i.e. all arguments
  46. * for which no long or short name has been registered via a call
  47. * to <code>addArgument()</code>.
  48. *
  49. * @see addArgument()
  50. *
  51. * @return A list containing unparsed arguments.
  52. */
  53. const QStringList& unparsedArguments();
  54. /**
  55. * Checks if the given argument has been added via a call
  56. * to <code>addArgument()</code>.
  57. *
  58. * @see addArgument()
  59. *
  60. * @param argument The argument to be checked.
  61. * @return <code>true</code> if the argument was added, <code>false</code>
  62. * otherwise.
  63. */
  64. bool argumentAdded(const QString& argument);
  65. /**
  66. * Checks if the given argument has been parsed successfully by a previous
  67. * call to <code>parseArguments()</code>.
  68. *
  69. * @param argument The argument to be checked.
  70. * @return <code>true</code> if the argument was parsed, <code>false</code>
  71. * otherwise.
  72. */
  73. bool argumentParsed(const QString& argument);
  74. /**
  75. * Adds a command line argument. An argument can have a long name
  76. * (like --long-argument-name), a short name (like -l), or both. The type
  77. * of the argument can be specified by using the <code>type</code> parameter.
  78. * The following types are supported:
  79. *
  80. * <table>
  81. * <tr><td><b>Type</b></td><td><b># of parameters</b></td><td><b>Default regular expr</b></td>
  82. * <td><b>Example</b></td></tr>
  83. * <tr><td>QVariant::String</td><td>1</td><td>.*</td><td>--test-string StringParameter</td></tr>
  84. * <tr><td>QVariant::Bool</td><td>0</td><td>does not apply</td><td>--enable-something</td></tr>
  85. * <tr><td>QVariant::StringList</td><td>-1</td><td>.*</td><td>--test-list string1 string2</td></tr>
  86. * <tr><td>QVariant::Int</td><td>1</td><td>-?[0-9]+</td><td>--test-int -5</td></tr>
  87. * </table>
  88. *
  89. * The regular expressions are used to validate the parameters of command line
  90. * arguments. You can restrict the valid set of parameters by calling
  91. * <code>setExactMatchRegularExpression()</code> for your argument.
  92. *
  93. * Optionally, a help string and a default value can be provided for the argument. If
  94. * the QVariant type of the default value does not match <code>type</code>, an
  95. * exception is thrown.
  96. *
  97. * If the long or short argument has already been added, or if both are empty strings,
  98. * the method call has no effect.
  99. *
  100. * @param longarg The long argument name.
  101. * @param shortarg The short argument name.
  102. * @param type The argument type (see the list above for supported types).
  103. * @param argHelp A help string describing the argument.
  104. * @param defaultValue A default value for the argument.
  105. * @param ignoreRest All arguments after the current one will be ignored.
  106. *
  107. * @see setExactMatchRegularExpression()
  108. * @throws std::logic_error If the QVariant type of <code>defaultValue</code>
  109. * does not match <code>type</code>, a <code>std::logic_error</code> is thrown.
  110. */
  111. void addArgument(const QString& longarg, const QString& shortarg,
  112. QVariant::Type type, const QString& argHelp = QString(),
  113. const QVariant& defaultValue = QVariant(), bool ignoreRest = false);
  114. /**
  115. * Sets a custom regular expression for validating argument parameters. The method
  116. * <code>errorString()</code> can be used the get the last error description.
  117. *
  118. * @param argument The previously added long or short argument name.
  119. * @param expression A regular expression which the arugment parameters must match.
  120. * @param exactMatchFailedMessage An error message explaining why the parameter did
  121. * not match.
  122. *
  123. * @return <code>true</code> if the argument was found and the regular expression was set,
  124. * <code>false</code> otherwise.
  125. *
  126. * @see errorString()
  127. */
  128. bool setExactMatchRegularExpression(const QString& argument, const QString& expression,
  129. const QString& exactMatchFailedMessage);
  130. int fieldWidth();
  131. /**
  132. * Creates a help text containing properly formatted argument names and help strings
  133. * provided by calls to <code>addArgument()</code>.
  134. *
  135. * @param charPad The padding character.
  136. * @return The formatted help text.
  137. */
  138. QString helpText(const char charPad = ' ');
  139. private:
  140. class ctkInternal;
  141. ctkInternal * Internal;
  142. };
  143. #endif