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