main.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #include "../include/batchioman.h"
  2. #include "../include/machineioman.h"
  3. #include "../include/userioman.h"
  4. #include <boost/program_options.hpp>
  5. #include <iostream>
  6. #include <string>
  7. namespace bpo = boost::program_options;
  8. /* this is provided for the readline callback in IoMan */
  9. IoMan *gIOMAN = NULL;
  10. void show_help(char *exec) {
  11. std::printf("ccats command line interface\n");
  12. std::printf("usage: %s IP [Options]\n", exec);
  13. std::printf("IP should be in the format \"xxx.xxx.xxx.xxx\"\n");
  14. }
  15. int main(int argc, char **argv) {
  16. bpo::options_description desc("Options");
  17. desc.add_options()("help", "show this help")("machine", "switch to machine mode for I/O (not designed for user interaction)")(
  18. "batch", bpo::value<std::string>(), "run operations from arg as batch file")("usessl", "enable ssl for connection to server")("verbose",
  19. "enable debug output");
  20. bpo::variables_map vm;
  21. bool machine = false, usessl = false, batch = false, verbose = false;
  22. const char *file = NULL;
  23. IoMan *ioman;
  24. if (argc < 2 || !std::strncmp(argv[1], "--help", 6)) {
  25. show_help(argv[0]);
  26. std::cout << desc;
  27. return 1;
  28. }
  29. try {
  30. store(parse_command_line(argc - 1, argv + 1, desc), vm);
  31. notify(vm);
  32. if (vm.count("help")) {
  33. show_help(argv[0]);
  34. std::cout << desc;
  35. return 1;
  36. }
  37. if (vm.count("machine")) {
  38. // enable machine/gui mode
  39. machine = true;
  40. }
  41. if (vm.count("batch")) {
  42. // handle batch file mode
  43. file = vm["batch"].as<std::string>().c_str();
  44. batch = true;
  45. }
  46. if (vm.count("usessl")) {
  47. usessl = true;
  48. }
  49. if (vm.count("verbose")) {
  50. verbose = true;
  51. }
  52. } catch (const bpo::error &ex) {
  53. std::fprintf(stderr, "%s\n", ex.what());
  54. }
  55. std::printf("ip %s machine mode is %d file is %s enablessl is %d verbose is %d\n", argv[1], machine, file ? file : "", usessl, verbose);
  56. if (batch) {
  57. ioman = new BatchIoMan(argv[1], usessl, verbose, file);
  58. } else if (machine) {
  59. ioman = new MachineIoMan(argv[1], usessl, verbose);
  60. } else {
  61. ioman = new UserIoMan(argv[1], usessl, verbose);
  62. }
  63. gIOMAN = ioman;
  64. if (ioman->init()) {
  65. ioman->run();
  66. }
  67. delete ioman;
  68. gIOMAN = NULL;
  69. std::printf("done\n");
  70. }