ctkCmdLineModuleTour.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) German Cancer Research Center,
  4. Division of Medical and Biological Informatics
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. =============================================================================*/
  15. #include <ctkCommandLineParser.h>
  16. #include <QCoreApplication>
  17. #include <QTextStream>
  18. #include <QFile>
  19. #include <cstdlib>
  20. int main(int argc, char* argv[])
  21. {
  22. QCoreApplication app(argc, argv);
  23. // This is used by QSettings
  24. QCoreApplication::setOrganizationName("CommonTK");
  25. QCoreApplication::setApplicationName("CmdLineModuleTour");
  26. ctkCommandLineParser parser;
  27. // Use Unix-style argument names
  28. parser.setArgumentPrefix("--", "-");
  29. // Add command line argument names
  30. parser.addArgument("help", "h", QVariant::Bool, "Show this help text");
  31. parser.addArgument("xml", "", QVariant::Bool, "Print a XML description of this modules command line interface");
  32. // Parse the command line arguments
  33. bool ok = false;
  34. QHash<QString, QVariant> parsedArgs = parser.parseArguments(QCoreApplication::arguments(), &ok);
  35. if (!ok)
  36. {
  37. QTextStream(stderr, QIODevice::WriteOnly) << "Error parsing arguments: "
  38. << parser.errorString() << "\n";
  39. return EXIT_FAILURE;
  40. }
  41. // Show a help message
  42. if (parsedArgs.contains("help") || parsedArgs.contains("h"))
  43. {
  44. QTextStream(stdout, QIODevice::WriteOnly) << parser.helpText();
  45. return EXIT_SUCCESS;
  46. }
  47. if (parsedArgs.contains("xml"))
  48. {
  49. QFile xmlDescription(":/ctkCmdLineModuleTour.xml");
  50. xmlDescription.open(QIODevice::ReadOnly);
  51. QTextStream(stdout, QIODevice::WriteOnly) << xmlDescription.readAll();
  52. }
  53. // Do something
  54. return EXIT_SUCCESS;
  55. }