ctkCommandLineModuleExplorerMain.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. // Qt includes
  16. #include <QApplication>
  17. #include <QDebug>
  18. #include <QFile>
  19. #include <QBuffer>
  20. #include <QWidget>
  21. #include <QProcess>
  22. #include <QDebug>
  23. #include <QtXmlPatterns/QXmlQuery>
  24. #include <QtUiTools/QUiLoader>
  25. // CTK includes
  26. #include <ctkCommandLineParser.h>
  27. #include <ctkCmdLineModuleXmlValidator.h>
  28. #include "ctkCmdLineModuleExplorerMainWindow.h"
  29. int main(int argc, char** argv)
  30. {
  31. QApplication myApp(argc, argv);
  32. myApp.setOrganizationName("CommonTK");
  33. myApp.setApplicationName("CommandLineModuleExplorer");
  34. ctkCommandLineParser cmdLineParser;
  35. cmdLineParser.setArgumentPrefix("--", "-");
  36. cmdLineParser.setStrictModeEnabled(true);
  37. cmdLineParser.addArgument("module", "", QVariant::String, "Path to a CLI module (executable)");
  38. //cmdLineParser.addArgument("module-xml", "", QVariant::String, "Path to a CLI XML description.");
  39. cmdLineParser.addArgument("validate-module", "", QVariant::String, "Path to a CLI module");
  40. cmdLineParser.addArgument("validate-xml", "", QVariant::String, "Path to a CLI XML description.");
  41. cmdLineParser.addArgument("verbose", "v", QVariant::Bool, "Be verbose.");
  42. cmdLineParser.addArgument("help", "h", QVariant::Bool, "Print this help text.");
  43. bool parseOkay = false;
  44. QHash<QString, QVariant> args = cmdLineParser.parseArguments(argc, argv, &parseOkay);
  45. QTextStream out(stdout, QIODevice::WriteOnly);
  46. if(!parseOkay)
  47. {
  48. out << "Error parsing command line arguments: " << cmdLineParser.errorString() << '\n';
  49. return EXIT_FAILURE;
  50. }
  51. if (args.contains("help"))
  52. {
  53. out << "Usage:\n" << cmdLineParser.helpText();
  54. out.flush();
  55. return EXIT_SUCCESS;
  56. }
  57. if (args.contains("validate-module"))
  58. {
  59. if (args.contains("validate-xml"))
  60. {
  61. out << "Ignoring \"validate-xml\" option.\n\n";
  62. }
  63. QString input = args["validate-module"].toString();
  64. if (!QFile::exists(input))
  65. {
  66. qCritical() << "Module does not exist:" << input;
  67. return EXIT_FAILURE;
  68. }
  69. QProcess process;
  70. process.setReadChannel(QProcess::StandardOutput);
  71. process.start(input, QStringList("--xml"));
  72. if (!process.waitForFinished() || process.exitStatus() == QProcess::CrashExit ||
  73. process.error() != QProcess::UnknownError)
  74. {
  75. qWarning() << "The executable at" << input << "could not be started:" << process.errorString();
  76. return EXIT_FAILURE;
  77. }
  78. process.waitForReadyRead();
  79. QByteArray xml = process.readAllStandardOutput();
  80. if (args.contains("verbose"))
  81. {
  82. qDebug() << xml;
  83. }
  84. // validate the outputted xml description
  85. QBuffer xmlInput(&xml);
  86. xmlInput.open(QIODevice::ReadOnly);
  87. ctkCmdLineModuleXmlValidator validator(&xmlInput);
  88. if (!validator.validateInput())
  89. {
  90. qCritical() << validator.errorString();
  91. return EXIT_FAILURE;
  92. }
  93. return EXIT_SUCCESS;
  94. }
  95. else if (args.contains("validate-xml"))
  96. {
  97. QFile input(args["validate-xml"].toString());
  98. if (!input.exists())
  99. {
  100. qCritical() << "XML description does not exist:" << input.fileName();
  101. return EXIT_FAILURE;
  102. }
  103. input.open(QIODevice::ReadOnly);
  104. ctkCmdLineModuleXmlValidator validator(&input);
  105. if (!validator.validateInput())
  106. {
  107. qCritical() << validator.errorString();
  108. return EXIT_FAILURE;
  109. }
  110. return EXIT_SUCCESS;
  111. }
  112. //ctkCmdLineModuleDescription* descr = ctkCmdLineModuleDescription::parse(&input);
  113. ctkCLModuleExplorerMainWindow mainWindow;
  114. if (args.contains("module"))
  115. {
  116. mainWindow.addModule(args["module"].toString());
  117. }
  118. mainWindow.show();
  119. return myApp.exec();
  120. }