main.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. //! [0]
  2. #include <ctkCommandLineParser.h>
  3. #include <QCoreApplication>
  4. #include <QTextStream>
  5. #include <cstdlib>
  6. int main(int argc, char** argv)
  7. {
  8. QCoreApplication app(argc, argv);
  9. // This is used by QSettings
  10. QCoreApplication::setOrganizationName("MyOrg");
  11. QCoreApplication::setApplicationName("MyApp");
  12. ctkCommandLineParser parser;
  13. // Use Unix-style argument names
  14. parser.setArgumentPrefix("--", "-");
  15. // Enable QSettings support
  16. parser.enableSettings("disable-settings");
  17. // Add command line argument names
  18. parser.addArgument("disable-settings", "", QVariant::Bool, "Do not use QSettings");
  19. parser.addArgument("help", "h", QVariant::Bool, "Show this help text");
  20. parser.addArgument("search-paths", "s", QVariant::StringList, "A list of paths to search");
  21. // Parse the command line arguments
  22. bool ok = false;
  23. QHash<QString, QVariant> parsedArgs = parser.parseArguments(QCoreApplication::arguments(), &ok);
  24. if (!ok)
  25. {
  26. QTextStream(stderr, QIODevice::WriteOnly) << "Error parsing arguments: "
  27. << parser.errorString() << "\n";
  28. return EXIT_FAILURE;
  29. }
  30. // Show a help message
  31. if (parsedArgs.contains("help") || parsedArgs.contains("h"))
  32. {
  33. QTextStream(stdout, QIODevice::WriteOnly) << parser.helpText();
  34. return EXIT_SUCCESS;
  35. }
  36. // Do something
  37. return EXIT_SUCCESS;
  38. }
  39. //! [0]