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