cmdline.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. #ifndef GRPC_TEST_CORE_UTIL_CMDLINE_H
  19. #define GRPC_TEST_CORE_UTIL_CMDLINE_H
  20. #include <grpc/support/port_platform.h>
  21. #include <string>
  22. /** Simple command line parser.
  23. Supports flags that can be specified as -foo, --foo, --no-foo, -no-foo, etc
  24. And integers, strings that can be specified as -foo=4, -foo blah, etc
  25. No support for short command line options (but we may get that in the
  26. future.)
  27. Usage (for a program with a single flag argument 'foo'):
  28. int main(int argc, char **argv) {
  29. gpr_cmdline *cl;
  30. int verbose = 0;
  31. cl = gpr_cmdline_create("My cool tool");
  32. gpr_cmdline_add_int(cl, "verbose", "Produce verbose output?", &verbose);
  33. gpr_cmdline_parse(cl, argc, argv);
  34. gpr_cmdline_destroy(cl);
  35. if (verbose) {
  36. gpr_log(GPR_INFO, "Goodbye cruel world!");
  37. }
  38. return 0;
  39. } */
  40. typedef struct gpr_cmdline gpr_cmdline;
  41. /** Construct a command line parser: takes a short description of the tool
  42. doing the parsing */
  43. gpr_cmdline* gpr_cmdline_create(const char* description);
  44. /** Add an integer parameter, with a name (used on the command line) and some
  45. helpful text (used in the command usage) */
  46. void gpr_cmdline_add_int(gpr_cmdline* cl, const char* name, const char* help,
  47. int* value);
  48. /** The same, for a boolean flag */
  49. void gpr_cmdline_add_flag(gpr_cmdline* cl, const char* name, const char* help,
  50. int* value);
  51. /** And for a string */
  52. void gpr_cmdline_add_string(gpr_cmdline* cl, const char* name, const char* help,
  53. const char** value);
  54. /** Set a callback for non-named arguments */
  55. void gpr_cmdline_on_extra_arg(
  56. gpr_cmdline* cl, const char* name, const char* help,
  57. void (*on_extra_arg)(void* user_data, const char* arg), void* user_data);
  58. /** Enable surviving failure: default behavior is to exit the process */
  59. void gpr_cmdline_set_survive_failure(gpr_cmdline* cl);
  60. /** Parse the command line; returns 1 on success, on failure either dies
  61. (by default) or returns 0 if gpr_cmdline_set_survive_failure() has been
  62. called */
  63. int gpr_cmdline_parse(gpr_cmdline* cl, int argc, char** argv);
  64. /** Destroy the parser */
  65. void gpr_cmdline_destroy(gpr_cmdline* cl);
  66. /** Get a string describing usage */
  67. std::string gpr_cmdline_usage_string(gpr_cmdline* cl, const char* argv0);
  68. #endif /* GRPC_TEST_CORE_UTIL_CMDLINE_H */