ctkCmdLineModuleTestBed.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 <QDebug>
  20. #include <QTime>
  21. #include <cstdlib>
  22. #include <time.h>
  23. int main(int argc, char* argv[])
  24. {
  25. QCoreApplication app(argc, argv);
  26. // This is used by QSettings
  27. QCoreApplication::setOrganizationName("CommonTK");
  28. QCoreApplication::setApplicationName("CmdLineModuleTestBed");
  29. ctkCommandLineParser parser;
  30. // Use Unix-style argument names
  31. parser.setArgumentPrefix("--", "-");
  32. // Add command line argument names
  33. parser.addArgument("help", "h", QVariant::Bool, "Show this help text");
  34. parser.addArgument("xml", "", QVariant::Bool, "Print a XML description of this modules command line interface");
  35. parser.addArgument("runtime", "", QVariant::Int, "Runtime in seconds", 1);
  36. parser.addArgument("exitCode", "", QVariant::Int, "Exit code", 0);
  37. parser.addArgument("exitTime", "", QVariant::Int, "Exit time", 0);
  38. parser.addArgument("errorText", "", QVariant::String, "Error text (will not be printed on exit code 0)");
  39. QTextStream out(stdout, QIODevice::WriteOnly);
  40. QTextStream err(stderr, QIODevice::WriteOnly);
  41. // Parse the command line arguments
  42. bool ok = false;
  43. QHash<QString, QVariant> parsedArgs = parser.parseArguments(QCoreApplication::arguments(), &ok);
  44. if (!ok)
  45. {
  46. err << "Error parsing arguments: " << parser.errorString() << "\n";
  47. return EXIT_FAILURE;
  48. }
  49. // Show a help message
  50. if (parsedArgs.contains("help") || parsedArgs.contains("h"))
  51. {
  52. out << parser.helpText();
  53. out.setFieldWidth(parser.fieldWidth());
  54. out.setFieldAlignment(QTextStream::AlignLeft);
  55. out << " <output>...";
  56. out << "One or more output strings\n";
  57. return EXIT_SUCCESS;
  58. }
  59. if (parsedArgs.contains("xml"))
  60. {
  61. QFile xmlDescription(":/ctkCmdLineModuleTestBed.xml");
  62. xmlDescription.open(QIODevice::ReadOnly);
  63. out << xmlDescription.readAll();
  64. return EXIT_SUCCESS;
  65. }
  66. // Do something
  67. float runtime = parsedArgs["runtime"].toFloat();
  68. float exitTime = parsedArgs["exitTime"].toFloat();
  69. int exitTimeMillis = static_cast<long>(exitTime/2.0 * 1000.0);
  70. int exitCode = parsedArgs["exitCode"].toInt();
  71. QString errorText = parsedArgs["errorText"].toString();
  72. QStringList outputs = parser.unparsedArguments();
  73. if (outputs.empty())
  74. {
  75. // no outputs given, just return
  76. if (exitCode != 0)
  77. {
  78. err << errorText;
  79. }
  80. return exitCode;
  81. }
  82. float stepTime = runtime / static_cast<float>(outputs.size());
  83. QTime time;
  84. time.start();
  85. struct timespec nanostep;
  86. foreach(QString output, outputs)
  87. {
  88. if (exitTimeMillis != 0 && exitTimeMillis < time.elapsed())
  89. {
  90. if (exitCode != 0)
  91. {
  92. err << errorText;
  93. }
  94. return exitCode;
  95. }
  96. // simulate some work
  97. nanostep.tv_sec = static_cast<time_t>(stepTime);
  98. double millisecs = (stepTime - static_cast<time_t>(stepTime)) * 1000.0;
  99. nanostep.tv_nsec = static_cast<long>(millisecs * 1000.0 * 1000.0);
  100. nanosleep(&nanostep, NULL);
  101. // print the first output
  102. out << output; endl(out);
  103. }
  104. return EXIT_SUCCESS;
  105. }