Parcourir la source

Converted CommandLineParser example code to a compilable snippet.

Sascha Zelzer il y a 13 ans
Parent
commit
fb10973fa4

+ 2 - 1
Documentation/Doxyfile.txt.in

@@ -721,7 +721,8 @@ EXCLUDE_SYMBOLS        =
 # directories that contain example code fragments that are included (see
 # the \include command).
 
-EXAMPLE_PATH           = @CTK_SOURCE_DIR@/Libs/PluginFramework/Documentation/Snippets/
+EXAMPLE_PATH           = @CTK_SOURCE_DIR@/Libs/Core/Documentation/Snippets/ \
+                         @CTK_SOURCE_DIR@/Libs/PluginFramework/Documentation/Snippets/
 
 # If the value of the EXAMPLE_PATH tag contains directories, you can use the
 # EXAMPLE_PATTERNS tag to specify one or more wildcard pattern (like *.cpp

+ 3 - 0
Libs/Core/CMakeLists.txt

@@ -134,4 +134,7 @@ endif()
 # Testing
 if(BUILD_TESTING)
   add_subdirectory(Testing)
+  
+  # Compile source code snippets
+  add_subdirectory(Documentation/Snippets)
 endif()

+ 4 - 0
Libs/Core/Documentation/Snippets/CMakeLists.txt

@@ -0,0 +1,4 @@
+if(BUILD_TESTING)
+  include_directories(${CTKCore_SOURCE_DIR})
+  ctkFunctionCompileSnippets("${CMAKE_CURRENT_SOURCE_DIR}" CTKCore)
+endif()

+ 48 - 0
Libs/Core/Documentation/Snippets/CommandLineParser/main.cpp

@@ -0,0 +1,48 @@
+//! [0]
+#include <ctkCommandLineParser.h>
+
+#include <QCoreApplication>
+#include <QTextStream>
+
+#include <cstdlib>
+
+int main(int argc, char** argv)
+{
+  QCoreApplication app(argc, argv);
+  // This is used by QSettings
+  QCoreApplication::setOrganizationName("MyOrg");
+  QCoreApplication::setApplicationName("MyApp");
+
+  ctkCommandLineParser parser;
+  // Use Unix-style argument names
+  parser.setArgumentPrefix("--", "-");
+  // Enable QSettings support
+  parser.enableSettings("disable-settings");
+
+  // Add command line argument names
+  parser.addArgument("disable-settings", "", QVariant::Bool, "Do not use QSettings");
+  parser.addArgument("help", "h", QVariant::Bool, "Show this help text");
+  parser.addArgument("search-paths", "s", QVariant::StringList, "A list of paths to search");
+
+  // Parse the command line arguments
+  bool ok = false;
+  QHash<QString, QVariant> parsedArgs = parser.parseArguments(QCoreApplication::arguments(), &ok);
+  if (!ok)
+  {
+    QTextStream(stderr, QIODevice::WriteOnly) << "Error parsing arguments: "
+                                              << parser.errorString() << "\n";
+    return EXIT_FAILURE;
+  }
+
+  // Show a help message
+  if (parsedArgs.contains("help") || parsedArgs.contains("h"))
+  {
+    QTextStream(stdout, QIODevice::WriteOnly) << parser.helpText();
+    return EXIT_SUCCESS;
+  }
+
+  // Do something
+
+  return EXIT_SUCCESS;
+}
+//! [0]

+ 1 - 45
Libs/Core/ctkCommandLineParser.h

@@ -57,51 +57,7 @@ class QSettings;
  *
  * Here is an example how to use this class inside a main function:
  *
- * \code
- * #include <ctkCommandLineParser.h>
- * #include <QCoreApplication>
- * #include <QTextStream>
- *
- * int main(int argc, char** argv)
- * {
- *   QCoreApplication app(argc, argv);
- *   // This is used by QSettings
- *   QCoreApplication::setOrganizationName("MyOrg");
- *   QCoreApplication::setApplicationName("MyApp");
- *
- *   ctkCommandLineParser parser;
- *   // Use Unix-style argument names
- *   parser.setArgumentPrefix("--", "-");
- *   // Enable QSettings support
- *   parser.enableSettings("disable-settings");
- *
- *   // Add command line argument names
- *   parser.addArgument("disable-settings", "", QVariant::Bool, "Do not use QSettings");
- *   parser.addArgument("help", "h", QVariant::Bool, "Show this help text");
- *   parser.addArgument("search-paths", "s", QVariant::StringList, "A list of paths to search");
- *
- *   // Parse the command line arguments
- *   bool ok = false;
- *   QHash<QString, QVariant> parsedArgs = parser.parseArguments(QCoreApplication::arguments(), &ok);
- *   if (!ok)
- *   {
- *     QTextStream(stderr, QIODevice::WriteOnly) << "Error parsing arguments: "
- *                                               << parser.errorString() << "\n";
- *     return EXIT_FAILURE;
- *   }
- *
- *   // Show a help message
- *   if (parsedArgs.contains("help") || parsedArgs.contains("h"))
- *   {
- *     QTextStream(stdout, QIODevice::WriteOnly) << parser.helpText();
- *     return EXIT_SUCCESS;
- *   }
- *
- *   // Do something
- *
- *   return EXIT_SUCCESS;
- * }
- * \endcode
+ * \snippet CommandLineParser/main.cpp 0
  */
 class CTK_CORE_EXPORT ctkCommandLineParser : public QObject
 {