GRPC Core  0.13.1-pre1
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
cmdline.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015-2016, Google Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above
13  * copyright notice, this list of conditions and the following disclaimer
14  * in the documentation and/or other materials provided with the
15  * distribution.
16  * * Neither the name of Google Inc. nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  */
33 
34 #ifndef GRPC_SUPPORT_CMDLINE_H
35 #define GRPC_SUPPORT_CMDLINE_H
36 
38 
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42 
43 /* Simple command line parser.
44 
45  Supports flags that can be specified as -foo, --foo, --no-foo, -no-foo, etc
46  And integers, strings that can be specified as -foo=4, -foo blah, etc
47 
48  No support for short command line options (but we may get that in the
49  future.)
50 
51  Usage (for a program with a single flag argument 'foo'):
52 
53  int main(int argc, char **argv) {
54  gpr_cmdline *cl;
55  int verbose = 0;
56 
57  cl = gpr_cmdline_create("My cool tool");
58  gpr_cmdline_add_int(cl, "verbose", "Produce verbose output?", &verbose);
59  gpr_cmdline_parse(cl, argc, argv);
60  gpr_cmdline_destroy(cl);
61 
62  if (verbose) {
63  gpr_log(GPR_INFO, "Goodbye cruel world!");
64  }
65 
66  return 0;
67  } */
68 
69 typedef struct gpr_cmdline gpr_cmdline;
70 
71 /* Construct a command line parser: takes a short description of the tool
72  doing the parsing */
73 GPRAPI gpr_cmdline *gpr_cmdline_create(const char *description);
74 /* Add an integer parameter, with a name (used on the command line) and some
75  helpful text (used in the command usage) */
76 GPRAPI void gpr_cmdline_add_int(gpr_cmdline *cl, const char *name,
77  const char *help, int *value);
78 /* The same, for a boolean flag */
79 GPRAPI void gpr_cmdline_add_flag(gpr_cmdline *cl, const char *name,
80  const char *help, int *value);
81 /* And for a string */
82 GPRAPI void gpr_cmdline_add_string(gpr_cmdline *cl, const char *name,
83  const char *help, char **value);
84 /* Set a callback for non-named arguments */
86  gpr_cmdline *cl, const char *name, const char *help,
87  void (*on_extra_arg)(void *user_data, const char *arg), void *user_data);
88 /* Enable surviving failure: default behavior is to exit the process */
90 /* Parse the command line; returns 1 on success, on failure either dies
91  (by default) or returns 0 if gpr_cmdline_set_survive_failure() has been
92  called */
93 GPRAPI int gpr_cmdline_parse(gpr_cmdline *cl, int argc, char **argv);
94 /* Destroy the parser */
96 /* Get a string describing usage */
97 GPRAPI char *gpr_cmdline_usage_string(gpr_cmdline *cl, const char *argv0);
98 
99 #ifdef __cplusplus
100 }
101 #endif
102 
103 #endif /* GRPC_SUPPORT_CMDLINE_H */
struct gpr_cmdline gpr_cmdline
Definition: cmdline.h:69
GPRAPI char * gpr_cmdline_usage_string(gpr_cmdline *cl, const char *argv0)
GPRAPI int gpr_cmdline_parse(gpr_cmdline *cl, int argc, char **argv)
#define GPRAPI
Definition: port_platform.h:359
GPRAPI void gpr_cmdline_destroy(gpr_cmdline *cl)
GPRAPI gpr_cmdline * gpr_cmdline_create(const char *description)
GPRAPI void gpr_cmdline_add_int(gpr_cmdline *cl, const char *name, const char *help, int *value)
GPRAPI void gpr_cmdline_on_extra_arg(gpr_cmdline *cl, const char *name, const char *help, void(*on_extra_arg)(void *user_data, const char *arg), void *user_data)
GPRAPI void gpr_cmdline_add_flag(gpr_cmdline *cl, const char *name, const char *help, int *value)
GPRAPI void gpr_cmdline_set_survive_failure(gpr_cmdline *cl)
GPRAPI void gpr_cmdline_add_string(gpr_cmdline *cl, const char *name, const char *help, char **value)