1 /** 2 * Copyright (C) 2012-2015 Yecheng Fu <cofyc.jackson at gmail dot com> 3 * All rights reserved. 4 * 5 * Use of this source code is governed by a MIT-style license that can be found 6 * in the LICENSE file. 7 * 8 * module: argarse, developped by cofyc.jackson 9 * project: https://github.com/cofyc/argparse 10 * 11 */ 12 13 #ifndef SRC_TESTS_RT_MPI_COMMON_TEST_COMM_ARGPARSE_H_ 14 #define SRC_TESTS_RT_MPI_COMMON_TEST_COMM_ARGPARSE_H_ 15 16 /* for c++ compatibility */ 17 #ifdef __cplusplus 18 extern "C" { 19 #endif 20 21 #include <stdint.h> 22 23 struct argparse; 24 struct argparse_option; 25 26 typedef int argparse_callback (struct argparse *self, 27 const struct argparse_option *option); 28 29 enum argparse_flag { 30 ARGPARSE_STOP_AT_NON_OPTION = 1, 31 }; 32 33 enum argparse_option_type { 34 /* special */ 35 ARGPARSE_OPT_END, 36 ARGPARSE_OPT_GROUP, 37 /* options with no arguments */ 38 ARGPARSE_OPT_BOOLEAN, 39 ARGPARSE_OPT_BIT, 40 /* options with arguments (optional or required) */ 41 ARGPARSE_OPT_INTEGER, 42 ARGPARSE_OPT_FLOAT, 43 ARGPARSE_OPT_STRING, 44 }; 45 46 enum argparse_option_flags { 47 OPT_NONEG = 1, /* disable negation */ 48 }; 49 50 /** 51 * argparse option 52 * 53 * `type`: 54 * holds the type of the option, you must have an ARGPARSE_OPT_END last in your 55 * array. 56 * 57 * `short_name`: 58 * the character to use as a short option name, '\0' if none. 59 * 60 * `long_name`: 61 * the long option name, without the leading dash, NULL if none. 62 * 63 * `value`: 64 * stores pointer to the value to be filled. 65 * 66 * `help`: 67 * the short help message associated to what the option does. 68 * Must never be NULL (except for ARGPARSE_OPT_END). 69 * 70 * `callback`: 71 * function is called when corresponding argument is parsed. 72 * 73 * `data`: 74 * associated data. Callbacks can use it like they want. 75 * 76 * `flags`: 77 * option flags. 78 */ 79 struct argparse_option { 80 enum argparse_option_type type; 81 const char short_name; 82 const char *long_name; 83 void *value; 84 const char *help; 85 argparse_callback *callback; 86 intptr_t data; 87 int flags; 88 }; 89 90 /** 91 * argpparse 92 */ 93 struct argparse { 94 // user supplied 95 const struct argparse_option *options; 96 const char *const *usages; 97 int flags; 98 const char *description; // a description after usage 99 const char *epilog; // a description at the end 100 // internal context 101 int argc; 102 const char **argv; 103 const char **out; 104 int cpidx; 105 const char *optvalue; // current option value 106 }; 107 108 // built-in callbacks 109 int argparse_help_cb(struct argparse *self, 110 const struct argparse_option *option); 111 112 // built-in option macros 113 #define OPT_END() { ARGPARSE_OPT_END, 0, NULL, NULL, 0, NULL, 0, 0 } 114 #define OPT_BOOLEAN(...) { ARGPARSE_OPT_BOOLEAN, __VA_ARGS__ } 115 #define OPT_BIT(...) { ARGPARSE_OPT_BIT, __VA_ARGS__ } 116 #define OPT_INTEGER(...) { ARGPARSE_OPT_INTEGER, __VA_ARGS__ } 117 #define OPT_FLOAT(...) { ARGPARSE_OPT_FLOAT, __VA_ARGS__ } 118 #define OPT_STRING(...) { ARGPARSE_OPT_STRING, __VA_ARGS__ } 119 #define OPT_GROUP(h) { ARGPARSE_OPT_GROUP, 0, NULL, NULL, h, NULL, 0, 0 } 120 #define OPT_HELP() OPT_BOOLEAN('\0', "help", NULL, \ 121 "show this help message and exit", \ 122 argparse_help_cb, 0, OPT_NONEG) 123 124 int argparse_init(struct argparse *self, struct argparse_option *options, 125 const char *const *usages, int flags); 126 int argparse_describe(struct argparse *self, const char *description, 127 const char *epilog); 128 int argparse_parse(struct argparse *self, int argc, const char **argv); 129 int argparse_usage(struct argparse *self); 130 131 #ifdef __cplusplus 132 } 133 #endif 134 135 #endif // SRC_TESTS_RT_MPI_COMMON_TEST_COMM_ARGPARSE_H_ 136