Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 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 | |
Nicolas "Pixel" Noble | 1ff52d5 | 2015-03-01 05:24:36 +0100 | [diff] [blame] | 34 | #ifndef GRPC_SUPPORT_CMDLINE_H |
| 35 | #define GRPC_SUPPORT_CMDLINE_H |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 36 | |
| 37 | #ifdef __cplusplus |
| 38 | extern "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 | |
| 67 | typedef struct gpr_cmdline gpr_cmdline; |
| 68 | |
| 69 | /* Construct a command line parser: takes a short description of the tool |
| 70 | doing the parsing */ |
| 71 | gpr_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) */ |
| 74 | void gpr_cmdline_add_int(gpr_cmdline *cl, const char *name, const char *help, |
| 75 | int *value); |
| 76 | /* The same, for a boolean flag */ |
| 77 | void gpr_cmdline_add_flag(gpr_cmdline *cl, const char *name, const char *help, |
| 78 | int *value); |
| 79 | /* And for a string */ |
| 80 | void gpr_cmdline_add_string(gpr_cmdline *cl, const char *name, const char *help, |
| 81 | char **value); |
| 82 | /* Set a callback for non-named arguments */ |
| 83 | void 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 */ |
| 87 | void gpr_cmdline_parse(gpr_cmdline *cl, int argc, char **argv); |
| 88 | /* Destroy the parser */ |
| 89 | void gpr_cmdline_destroy(gpr_cmdline *cl); |
Craig Tiller | e702361 | 2015-05-28 08:00:14 -0700 | [diff] [blame] | 90 | /* Get a string describing usage */ |
| 91 | char *gpr_cmdline_usage_string(gpr_cmdline *cl, const char *argv0); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 92 | |
| 93 | #ifdef __cplusplus |
| 94 | } |
| 95 | #endif |
| 96 | |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame^] | 97 | #endif /* GRPC_SUPPORT_CMDLINE_H */ |