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