#ifndef __ctkCommandLineParser_h #define __ctkCommandLineParser_h // Qt includes #include #include #include // CTK includes #include "CTKCoreExport.h" // -------------------------------------------------------------------------- class CTK_CORE_EXPORT ctkCommandLineParser { public: ctkCommandLineParser(); ~ctkCommandLineParser(); /** * Parse a given list of command line arguments. * * This method parses a list of QString elements considering the known arguments * added by calls to addArgument(). If any one of the argument * values does not match the corresponding regular expression, * ok is set to false and an empty QHash object is returned. * * The keys in the returned QHash object correspond to the long argument string, * if it is not empty. Otherwise, the short argument string is used as key. The * QVariant values can safely be converted to the type specified in the * addArgument() method call. * * @param arguments A QStringList containing command line arguments. Usually * given by QCoreApplication::arguments(). * @param ok A pointer to a boolean variable. Will be set to true * if all regular expressions matched, false otherwise. * @return A QHash object mapping the long argument (if empty, the short one) * to a QVariant containing the value. */ QHash parseArguments(const QStringList &arguments, bool* ok = 0); /** * Returns a detailed error description if a call to parseArguments() * failed. * * @return The error description, empty if no error occured. * @see parseArguments(const QStringList&, bool*) */ QString errorString(); /** * This method returns all unparsed arguments, i.e. all arguments * for which no long or short name has been registered via a call * to addArgument(). * * @see addArgument() * * @return A list containing unparsed arguments. */ const QStringList& unparsedArguments(); /** * Checks if the given argument has been added via a call * to addArgument(). * * @see addArgument() * * @param argument The argument to be checked. * @return true if the argument was added, false * otherwise. */ bool argumentAdded(const QString& argument); /** * Checks if the given argument has been parsed successfully by a previous * call to parseArguments(). * * @param argument The argument to be checked. * @return true if the argument was parsed, false * otherwise. */ bool argumentParsed(const QString& argument); /** * Adds a command line argument. An argument can have a long name * (like --long-argument-name), a short name (like -l), or both. The type * of the argument can be specified by using the type parameter. * The following types are supported: * * * * * * * * *
Type# of parametersDefault regular exprExample
QVariant::String1.*--test-string StringParameter
QVariant::Bool0does not apply--enable-something
QVariant::StringList-1.*--test-list string1 string2
QVariant::Int1-?[0-9]+--test-int -5
* * The regular expressions are used to validate the parameters of command line * arguments. You can restrict the valid set of parameters by calling * setExactMatchRegularExpression() for your argument. * * Optionally, a help string and a default value can be provided for the argument. If * the QVariant type of the default value does not match type, an * exception is thrown. * * If the long or short argument has already been added, or if both are empty strings, * the method call has no effect. * * @param longarg The long argument name. * @param shortarg The short argument name. * @param type The argument type (see the list above for supported types). * @param argHelp A help string describing the argument. * @param defaultValue A default value for the argument. * @param ignoreRest All arguments after the current one will be ignored. * * @see setExactMatchRegularExpression() * @throws std::logic_error If the QVariant type of defaultValue * does not match type, a std::logic_error is thrown. */ void addArgument(const QString& longarg, const QString& shortarg, QVariant::Type type, const QString& argHelp = QString(), const QVariant& defaultValue = QVariant(), bool ignoreRest = false); /** * Sets a custom regular expression for validating argument parameters. The method * errorString() can be used the get the last error description. * * @param argument The previously added long or short argument name. * @param expression A regular expression which the arugment parameters must match. * @param exactMatchFailedMessage An error message explaining why the parameter did * not match. * * @return true if the argument was found and the regular expression was set, * false otherwise. * * @see errorString() */ bool setExactMatchRegularExpression(const QString& argument, const QString& expression, const QString& exactMatchFailedMessage); int fieldWidth(); /** * Creates a help text containing properly formatted argument names and help strings * provided by calls to addArgument(). * * @param charPad The padding character. * @return The formatted help text. */ QString helpText(const char charPad = ' '); private: class ctkInternal; ctkInternal * Internal; }; #endif