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