main.cpp 1.8 KB

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