main.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #include <boost/program_options.hpp>
  2. #include <cctype>
  3. #include <iostream>
  4. #define COMMANDLEN 10
  5. #define sizeofarr(a) (sizeof(a) / sizeof(a[0]))
  6. namespace bpo = boost::program_options;
  7. typedef enum {
  8. CMD_HELP,
  9. CMD_CONNECT,
  10. CMD_DISCONNECT,
  11. CMD_PUT,
  12. CMD_REMOVE,
  13. CMD_GET,
  14. CMD_QUERY,
  15. CMD_SETUP,
  16. CMD_LOG,
  17. CMD_UNKNOWN
  18. } COMMANDTYPES;
  19. typedef struct {
  20. COMMANDTYPES cmd;
  21. const char *name;
  22. const char *desc;
  23. } CMD;
  24. CMD commands[]{{CMD_HELP, "help", "show help"},
  25. {CMD_CONNECT, "connect", "connect to IP"},
  26. {CMD_DISCONNECT, "disconnect", "disconnect from IP"},
  27. {CMD_PUT, "put", "upload file to IP and add to queue"},
  28. {CMD_REMOVE, "remove",
  29. "remove file from IP and queue (stops xfer if required)"},
  30. {CMD_GET, "get", "retrieve file from IP"},
  31. {CMD_QUERY, "query", "query status of IP"},
  32. {CMD_SETUP, "setup", "configure server at IP"},
  33. {CMD_LOG, "log", "show log from IP"}};
  34. COMMANDTYPES getCommand(char *str, unsigned int isshort) {
  35. COMMANDTYPES ret = CMD_UNKNOWN;
  36. char temp[11] = {0};
  37. if (strlen(str) > COMMANDLEN)
  38. return ret;
  39. if (isshort)
  40. temp[0] = tolower(str[0]);
  41. else
  42. for (int i = 0; i < 10; i++)
  43. temp[i] = tolower(str[i]);
  44. for (int i = 0; i < sizeofarr(commands); i++) {
  45. if (isshort) {
  46. if (tolower(str[0]) == commands[i].name[0])
  47. ret = commands[i].cmd;
  48. } else {
  49. if (!strncmp(temp, commands[i].name, COMMANDLEN))
  50. ret = commands[i].cmd;
  51. }
  52. }
  53. return ret;
  54. }
  55. void show_help(char *exec) {
  56. std::printf("ccats command line interface\n");
  57. std::printf("usage: %s COMMAND IP [Options]\n", exec);
  58. std::printf("available COMMANDs are:\n");
  59. for (int i = 0; i < sizeofarr(commands); i++) {
  60. std::printf("%10s - %s\n", commands[i].name, commands[i].desc);
  61. }
  62. std::printf("IP should be in the format \"xxx.xxx.xxx.xxx\"\n");
  63. }
  64. int main(int argc, char **argv) {
  65. bpo::options_description desc{"Options"};
  66. desc.add_options()("cooloption", "Cooloption to use with command FOON");
  67. bpo::variables_map vm;
  68. if (argc < 2) {
  69. show_help(argv[0]);
  70. exit(1);
  71. }
  72. COMMANDTYPES cmd = getCommand(argv[1], (strlen(argv[1]) == 1));
  73. switch (cmd) {
  74. case CMD_UNKNOWN:
  75. std::printf("unknown command\n");
  76. case CMD_HELP:
  77. show_help(argv[0]);
  78. std::cout << desc;
  79. exit(1);
  80. }
  81. // have enough valid arguments
  82. switch (cmd) {
  83. case CMD_CONNECT:
  84. std::printf("connecting to %s\n", argv[2]);
  85. break;
  86. case CMD_DISCONNECT:
  87. std::printf("disconnecting from %s\n", argv[2]);
  88. break;
  89. default:
  90. std::printf("command %s not implemented\n", argv[1]);
  91. break;
  92. }
  93. try {
  94. store(parse_command_line(argc - 2, argv + 2, desc), vm);
  95. notify(vm);
  96. if (vm.count("help")) {
  97. std::cout << desc;
  98. } else {
  99. std::printf("no additional options\n");
  100. }
  101. } catch (const bpo::error &ex) {
  102. std::fprintf(stderr, "%s\n", ex.what());
  103. }
  104. }