main.cpp 1.8 KB

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