cmdline.cc 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. *
  3. * Copyright 2015 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #include "test/core/util/cmdline.h"
  19. #include <limits.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <vector>
  23. #include "absl/strings/str_cat.h"
  24. #include "absl/strings/str_format.h"
  25. #include "absl/strings/str_join.h"
  26. #include <grpc/support/alloc.h>
  27. #include <grpc/support/log.h>
  28. #include <grpc/support/string_util.h>
  29. #include "src/core/lib/gpr/string.h"
  30. #include "src/core/lib/gprpp/memory.h"
  31. typedef enum { ARGTYPE_INT, ARGTYPE_BOOL, ARGTYPE_STRING } argtype;
  32. typedef struct arg {
  33. const char* name;
  34. const char* help;
  35. argtype type;
  36. void* value;
  37. struct arg* next;
  38. } arg;
  39. struct gpr_cmdline {
  40. const char* description;
  41. arg* args;
  42. const char* argv0;
  43. const char* extra_arg_name;
  44. const char* extra_arg_help;
  45. void (*extra_arg)(void* user_data, const char* arg);
  46. void* extra_arg_user_data;
  47. int (*state)(gpr_cmdline* cl, char* arg);
  48. arg* cur_arg;
  49. int survive_failure;
  50. };
  51. static int normal_state(gpr_cmdline* cl, char* str);
  52. gpr_cmdline* gpr_cmdline_create(const char* description) {
  53. gpr_cmdline* cl = grpc_core::Zalloc<gpr_cmdline>();
  54. cl->description = description;
  55. cl->state = normal_state;
  56. return cl;
  57. }
  58. void gpr_cmdline_set_survive_failure(gpr_cmdline* cl) {
  59. cl->survive_failure = 1;
  60. }
  61. void gpr_cmdline_destroy(gpr_cmdline* cl) {
  62. while (cl->args) {
  63. arg* a = cl->args;
  64. cl->args = a->next;
  65. gpr_free(a);
  66. }
  67. gpr_free(cl);
  68. }
  69. static void add_arg(gpr_cmdline* cl, const char* name, const char* help,
  70. argtype type, void* value) {
  71. arg* a;
  72. for (a = cl->args; a; a = a->next) {
  73. GPR_ASSERT(0 != strcmp(a->name, name));
  74. }
  75. a = static_cast<arg*>(gpr_zalloc(sizeof(arg)));
  76. a->name = name;
  77. a->help = help;
  78. a->type = type;
  79. a->value = value;
  80. a->next = cl->args;
  81. cl->args = a;
  82. }
  83. void gpr_cmdline_add_int(gpr_cmdline* cl, const char* name, const char* help,
  84. int* value) {
  85. add_arg(cl, name, help, ARGTYPE_INT, value);
  86. }
  87. void gpr_cmdline_add_flag(gpr_cmdline* cl, const char* name, const char* help,
  88. int* value) {
  89. add_arg(cl, name, help, ARGTYPE_BOOL, value);
  90. }
  91. void gpr_cmdline_add_string(gpr_cmdline* cl, const char* name, const char* help,
  92. const char** value) {
  93. add_arg(cl, name, help, ARGTYPE_STRING, value);
  94. }
  95. void gpr_cmdline_on_extra_arg(
  96. gpr_cmdline* cl, const char* name, const char* help,
  97. void (*on_extra_arg)(void* user_data, const char* arg), void* user_data) {
  98. GPR_ASSERT(!cl->extra_arg);
  99. GPR_ASSERT(on_extra_arg);
  100. cl->extra_arg = on_extra_arg;
  101. cl->extra_arg_user_data = user_data;
  102. cl->extra_arg_name = name;
  103. cl->extra_arg_help = help;
  104. }
  105. /* recursively descend argument list, adding the last element
  106. to s first - so that arguments are added in the order they were
  107. added to the list by api calls */
  108. static void add_args_to_usage(arg* a, std::vector<std::string>* s) {
  109. if (a == nullptr) return;
  110. add_args_to_usage(a->next, s);
  111. switch (a->type) {
  112. case ARGTYPE_BOOL:
  113. s->push_back(absl::StrFormat(" [--%s|--no-%s]", a->name, a->name));
  114. break;
  115. case ARGTYPE_STRING:
  116. s->push_back(absl::StrFormat(" [--%s=string]", a->name));
  117. break;
  118. case ARGTYPE_INT:
  119. s->push_back(absl::StrFormat(" [--%s=int]", a->name));
  120. break;
  121. }
  122. }
  123. std::string gpr_cmdline_usage_string(gpr_cmdline* cl, const char* argv0) {
  124. const char* name = strrchr(argv0, '/');
  125. if (name != nullptr) {
  126. name++;
  127. } else {
  128. name = argv0;
  129. }
  130. std::vector<std::string> s;
  131. s.push_back(absl::StrCat("Usage: ", name));
  132. add_args_to_usage(cl->args, &s);
  133. if (cl->extra_arg) {
  134. s.push_back(absl::StrFormat(" [%s...]", cl->extra_arg_name));
  135. }
  136. s.push_back("\n");
  137. return absl::StrJoin(s, "");
  138. }
  139. static int print_usage_and_die(gpr_cmdline* cl) {
  140. fprintf(stderr, "%s", gpr_cmdline_usage_string(cl, cl->argv0).c_str());
  141. if (!cl->survive_failure) {
  142. exit(1);
  143. }
  144. return 0;
  145. }
  146. static int extra_state(gpr_cmdline* cl, char* str) {
  147. if (!cl->extra_arg) {
  148. return print_usage_and_die(cl);
  149. }
  150. cl->extra_arg(cl->extra_arg_user_data, str);
  151. return 1;
  152. }
  153. static arg* find_arg(gpr_cmdline* cl, char* name) {
  154. arg* a;
  155. for (a = cl->args; a; a = a->next) {
  156. if (0 == strcmp(a->name, name)) {
  157. break;
  158. }
  159. }
  160. if (!a) {
  161. fprintf(stderr, "Unknown argument: %s\n", name);
  162. return nullptr;
  163. }
  164. return a;
  165. }
  166. static int value_state(gpr_cmdline* cl, char* str) {
  167. long intval;
  168. char* end;
  169. GPR_ASSERT(cl->cur_arg);
  170. switch (cl->cur_arg->type) {
  171. case ARGTYPE_INT:
  172. intval = strtol(str, &end, 0);
  173. if (*end || intval < INT_MIN || intval > INT_MAX) {
  174. fprintf(stderr, "expected integer, got '%s' for %s\n", str,
  175. cl->cur_arg->name);
  176. return print_usage_and_die(cl);
  177. }
  178. *static_cast<int*>(cl->cur_arg->value) = static_cast<int>(intval);
  179. break;
  180. case ARGTYPE_BOOL:
  181. if (0 == strcmp(str, "1") || 0 == strcmp(str, "true")) {
  182. *static_cast<int*>(cl->cur_arg->value) = 1;
  183. } else if (0 == strcmp(str, "0") || 0 == strcmp(str, "false")) {
  184. *static_cast<int*>(cl->cur_arg->value) = 0;
  185. } else {
  186. fprintf(stderr, "expected boolean, got '%s' for %s\n", str,
  187. cl->cur_arg->name);
  188. return print_usage_and_die(cl);
  189. }
  190. break;
  191. case ARGTYPE_STRING:
  192. *static_cast<char**>(cl->cur_arg->value) = str;
  193. break;
  194. }
  195. cl->state = normal_state;
  196. return 1;
  197. }
  198. static int normal_state(gpr_cmdline* cl, char* str) {
  199. char* eq = nullptr;
  200. char* tmp = nullptr;
  201. char* arg_name = nullptr;
  202. int r = 1;
  203. if (0 == strcmp(str, "-help") || 0 == strcmp(str, "--help") ||
  204. 0 == strcmp(str, "-h")) {
  205. return print_usage_and_die(cl);
  206. }
  207. cl->cur_arg = nullptr;
  208. if (str[0] == '-') {
  209. if (str[1] == '-') {
  210. if (str[2] == 0) {
  211. /* handle '--' to move to just extra args */
  212. cl->state = extra_state;
  213. return 1;
  214. }
  215. str += 2;
  216. } else {
  217. str += 1;
  218. }
  219. /* first byte of str is now past the leading '-' or '--' */
  220. if (str[0] == 'n' && str[1] == 'o' && str[2] == '-') {
  221. /* str is of the form '--no-foo' - it's a flag disable */
  222. str += 3;
  223. cl->cur_arg = find_arg(cl, str);
  224. if (cl->cur_arg == nullptr) {
  225. return print_usage_and_die(cl);
  226. }
  227. if (cl->cur_arg->type != ARGTYPE_BOOL) {
  228. fprintf(stderr, "%s is not a flag argument\n", str);
  229. return print_usage_and_die(cl);
  230. }
  231. *static_cast<int*>(cl->cur_arg->value) = 0;
  232. return 1; /* early out */
  233. }
  234. eq = strchr(str, '=');
  235. if (eq != nullptr) {
  236. /* copy the string into a temp buffer and extract the name */
  237. tmp = arg_name =
  238. static_cast<char*>(gpr_malloc(static_cast<size_t>(eq - str + 1)));
  239. memcpy(arg_name, str, static_cast<size_t>(eq - str));
  240. arg_name[eq - str] = 0;
  241. } else {
  242. arg_name = str;
  243. }
  244. cl->cur_arg = find_arg(cl, arg_name);
  245. if (cl->cur_arg == nullptr) {
  246. return print_usage_and_die(cl);
  247. }
  248. if (eq != nullptr) {
  249. /* str was of the type --foo=value, parse the value */
  250. r = value_state(cl, eq + 1);
  251. } else if (cl->cur_arg->type != ARGTYPE_BOOL) {
  252. /* flag types don't have a '--foo value' variant, other types do */
  253. cl->state = value_state;
  254. } else {
  255. /* flag parameter: just set the value */
  256. *static_cast<int*>(cl->cur_arg->value) = 1;
  257. }
  258. } else {
  259. r = extra_state(cl, str);
  260. }
  261. gpr_free(tmp);
  262. return r;
  263. }
  264. int gpr_cmdline_parse(gpr_cmdline* cl, int argc, char** argv) {
  265. int i;
  266. GPR_ASSERT(argc >= 1);
  267. cl->argv0 = argv[0];
  268. for (i = 1; i < argc; i++) {
  269. if (!cl->state(cl, argv[i])) {
  270. return 0;
  271. }
  272. }
  273. return 1;
  274. }