ctkSimplePythonShellMain.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Qt includes
  2. #include <QApplication>
  3. #include <QTextStream>
  4. // CTK includes
  5. #include <ctkPythonShell.h>
  6. #include <ctkCommandLineParser.h>
  7. #include "ctkSimplePythonManager.h"
  8. int main(int argc, char** argv)
  9. {
  10. QApplication app(argc, argv);
  11. ctkCommandLineParser parser;
  12. // Use Unix-style argument names
  13. parser.setArgumentPrefix("--", "-");
  14. // Add command line argument names
  15. parser.addArgument("help", "h", QVariant::Bool, "Print usage information and exit.");
  16. parser.addArgument("interactive", "I", QVariant::Bool, "Enable interactive mode");
  17. // Parse the command line arguments
  18. bool ok = false;
  19. QHash<QString, QVariant> parsedArgs = parser.parseArguments(QCoreApplication::arguments(), &ok);
  20. if (!ok)
  21. {
  22. QTextStream(stderr, QIODevice::WriteOnly) << "Error parsing arguments: "
  23. << parser.errorString() << "\n";
  24. return EXIT_FAILURE;
  25. }
  26. // Show a help message
  27. if (parsedArgs.contains("help") || parsedArgs.contains("h"))
  28. {
  29. QTextStream(stdout, QIODevice::WriteOnly) << "ctkSimplePythonShell\n"
  30. << "Usage\n\n"
  31. << " ctkSimplePythonShell [options] [<path-to-python-script> ...]\n\n"
  32. << "Options\n"
  33. << parser.helpText();
  34. return EXIT_SUCCESS;
  35. }
  36. ctkSimplePythonManager pythonManager;
  37. ctkPythonShell shell(&pythonManager);
  38. shell.setAttribute(Qt::WA_QuitOnClose, true);
  39. shell.resize(600, 280);
  40. shell.show();
  41. shell.setProperty("isInteractive", parsedArgs.contains("interactive"));
  42. pythonManager.addObjectToPythonMain("_ctkPythonShellInstance", &shell);
  43. foreach(const QString& script, parser.unparsedArguments())
  44. {
  45. pythonManager.executeFile(script);
  46. }
  47. return app.exec();
  48. }