You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
2.8KB

  1. /** zstreedb2 args.cxx
  2. (C) 2018 Erik Zscheile
  3. License: GPLv2
  4. **/
  5. #include <stdio.h>
  6. #include "args.hpp"
  7. namespace ztdb {
  8. bool args_t::parse(const int argc, char *argv[]) {
  9. // vars
  10. bool ret = true;
  11. int i = 1;
  12. compress = rocksdb::kNoCompression;
  13. cmd = UNKNOWN;
  14. // options
  15. for(; i < argc; ++i) {
  16. const char *cca = argv[i];
  17. if(cca[0] != '-') break;
  18. if(!cca[1]) {
  19. ++i;
  20. break;
  21. }
  22. if(!cca[2]) {
  23. // parse short opts
  24. switch(cca[1]) {
  25. case 'j':
  26. compress = rocksdb::kBZip2Compression;
  27. break;
  28. case 'J':
  29. compress = rocksdb::kLZ4Compression;
  30. break;
  31. case 'z':
  32. compress = rocksdb::kZlibCompression;
  33. break;
  34. case 'Z':
  35. compress = rocksdb::kZSTD;
  36. break;
  37. default:
  38. fprintf(stderr, "zstreedb2: unknown option '%s'\n", cca);
  39. return false;
  40. }
  41. } else {
  42. // parse long opts
  43. const std::string arg = cca + 1;
  44. if(arg == "-snappy") {
  45. compress = rocksdb::kSnappyCompression;
  46. } else {
  47. fprintf(stderr, "zstreedb2: unknown option '%s'\n", cca);
  48. return false;
  49. }
  50. }
  51. }
  52. if((argc - i) < 2) return false;
  53. if(argv[i][0] == 0 || argv[i][1] != 0) {
  54. fprintf(stderr, "zstreedb2: invalid command '%s'\n", argv[i]);
  55. return false;
  56. }
  57. switch(argv[i++][0]) {
  58. case 'g': cmd = GET; break;
  59. case 's': cmd = SET; break;
  60. case 'l': cmd = LS ; break;
  61. case 'r': cmd = RM ; break;
  62. case 'v': cmd = MV ; break;
  63. case 'm': cmd = MATCH; break;
  64. case 'p': cmd = PRINT; break;
  65. default:
  66. fprintf(stderr, "zstreedb2: invalid command '%s'\n", argv[i]);
  67. return false;
  68. }
  69. sel_node = argv[i++];
  70. cmd_args.reserve(argc - i);
  71. for(; i < argc; ++i) cmd_args.emplace_back(argv[i]);
  72. return true;
  73. }
  74. bool args_t::flag_use_ro() const noexcept {
  75. switch(cmd) {
  76. case GET:
  77. case LS:
  78. case MATCH:
  79. case PRINT:
  80. return true;
  81. default:
  82. return false;
  83. }
  84. }
  85. void usage() noexcept {
  86. puts("USAGE: zstreedb2 [-j|-J|-z|-Z] CMD NODE ARGS...");
  87. }
  88. void help() noexcept {
  89. usage();
  90. puts("\ncompression options:\n"
  91. " -j use bzip2\n"
  92. " -J use LZ4\n"
  93. " -z use zlib\n"
  94. " -Z use ZSTD\n"
  95. " --snappy use snappy");
  96. puts("\nkey/value commands:\n"
  97. " g retrieve a NODE value\n"
  98. " s assign a NODE value");
  99. puts("\nrecursive node commands:\n"
  100. " l list\n"
  101. " p print [ARG = BEFORE_KEY BEFORE_VALUE AFTER_VALUE]\n"
  102. " --> each line: $1$key$2$value$3\n"
  103. " r recursive remove\n"
  104. " v move [ARGS = FROM TO]");
  105. puts("\n m match [NODE as 'basename'; ARG+ = VALUE]");
  106. puts("\nreturn codes:\n"
  107. " 0 successful\n"
  108. " 1 invocation error\n"
  109. " 2 database error\n");
  110. }
  111. }