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