blob: 028dac2955c015f282785c530988f3edb6bc1574 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * Copyright 2015, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * 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
Nicolas "Pixel" Noble1ff52d52015-03-01 05:24:36 +010034#ifndef GRPC_SUPPORT_CMDLINE_H
35#define GRPC_SUPPORT_CMDLINE_H
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080036
37#ifdef __cplusplus
38extern "C" {
39#endif
40
41/* Simple command line parser.
42
43 Supports flags that can be specified as -foo, --foo, --no-foo, -no-foo, etc
44 And integers, strings that can be specified as -foo=4, -foo blah, etc
45
46 No support for short command line options (but we may get that in the
47 future.)
48
49 Usage (for a program with a single flag argument 'foo'):
50
51 int main(int argc, char **argv) {
52 gpr_cmdline *cl;
53 int verbose = 0;
54
55 cl = gpr_cmdline_create("My cool tool");
56 gpr_cmdline_add_int(cl, "verbose", "Produce verbose output?", &verbose);
57 gpr_cmdline_parse(cl, argc, argv);
58 gpr_cmdline_destroy(cl);
59
60 if (verbose) {
61 gpr_log(GPR_INFO, "Goodbye cruel world!");
62 }
63
64 return 0;
65 } */
66
67typedef struct gpr_cmdline gpr_cmdline;
68
69/* Construct a command line parser: takes a short description of the tool
70 doing the parsing */
71gpr_cmdline *gpr_cmdline_create(const char *description);
72/* Add an integer parameter, with a name (used on the command line) and some
73 helpful text (used in the command usage) */
74void gpr_cmdline_add_int(gpr_cmdline *cl, const char *name, const char *help,
75 int *value);
76/* The same, for a boolean flag */
77void gpr_cmdline_add_flag(gpr_cmdline *cl, const char *name, const char *help,
78 int *value);
79/* And for a string */
80void gpr_cmdline_add_string(gpr_cmdline *cl, const char *name, const char *help,
81 char **value);
82/* Set a callback for non-named arguments */
83void gpr_cmdline_on_extra_arg(
84 gpr_cmdline *cl, const char *name, const char *help,
85 void (*on_extra_arg)(void *user_data, const char *arg), void *user_data);
86/* Parse the command line */
87void gpr_cmdline_parse(gpr_cmdline *cl, int argc, char **argv);
88/* Destroy the parser */
89void gpr_cmdline_destroy(gpr_cmdline *cl);
Craig Tillere7023612015-05-28 08:00:14 -070090/* Get a string describing usage */
91char *gpr_cmdline_usage_string(gpr_cmdline *cl, const char *argv0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080092
93#ifdef __cplusplus
94}
95#endif
96
Craig Tillerd6c98df2015-08-18 09:33:44 -070097#endif /* GRPC_SUPPORT_CMDLINE_H */