1 | /* Copyright JS Foundation and other contributors, http://js.foundation |
2 | * |
3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
4 | * you may not use this file except in compliance with the License. |
5 | * You may obtain a copy of the License at |
6 | * |
7 | * http://www.apache.org/licenses/LICENSE-2.0 |
8 | * |
9 | * Unless required by applicable law or agreed to in writing, software |
10 | * distributed under the License is distributed on an "AS IS" BASIS |
11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | * See the License for the specific language governing permissions and |
13 | * limitations under the License. |
14 | */ |
15 | |
16 | #ifndef CLI_H |
17 | #define CLI_H |
18 | |
19 | #include <string.h> |
20 | #include <stdint.h> |
21 | |
22 | /** |
23 | * Command line option definition. |
24 | */ |
25 | typedef struct |
26 | { |
27 | int id; /**< unique ID of the option (CLI_OPT_DEFAULT, or anything >= 0) */ |
28 | const char *opt; /**< short option variant (in the form of "x" without dashes) */ |
29 | const char *longopt; /**< long option variant (in the form of "xxx" without dashes) */ |
30 | const char *meta; /**< name(s) of the argument(s) of the option, for display only */ |
31 | const char *help; /**< descriptive help message of the option */ |
32 | } cli_opt_t; |
33 | |
34 | /** |
35 | * Special marker for default option which also marks the end of the option list. |
36 | */ |
37 | #define CLI_OPT_DEFAULT -1 |
38 | |
39 | /** |
40 | * Returned by cli_consume_option () when no more options are available |
41 | * or an error occured. |
42 | */ |
43 | #define CLI_OPT_END -2 |
44 | |
45 | /** |
46 | * State of the sub-command processor. |
47 | * No fields should be accessed other than error and arg. |
48 | */ |
49 | typedef struct |
50 | { |
51 | /* Public fields. */ |
52 | const char *error; /**< public field for error message */ |
53 | const char *arg; /**< last processed argument as string */ |
54 | |
55 | /* Private fields. */ |
56 | int index; /**< current argument index */ |
57 | int argc; /**< remaining number of arguments */ |
58 | char **argv; /**< remaining arguments */ |
59 | const cli_opt_t *opts; /**< options */ |
60 | } cli_state_t; |
61 | |
62 | /** |
63 | * Macro for writing command line option definition struct literals. |
64 | */ |
65 | #define CLI_OPT_DEF(...) /*(cli_opt_t)*/ { __VA_ARGS__ } |
66 | |
67 | /* |
68 | * Functions for CLI. |
69 | */ |
70 | |
71 | cli_state_t cli_init (const cli_opt_t *options_p, int argc, char **argv); |
72 | void cli_change_opts (cli_state_t *state_p, const cli_opt_t *options_p); |
73 | int cli_consume_option (cli_state_t *state_p); |
74 | const char * cli_consume_string (cli_state_t *state_p); |
75 | int cli_consume_int (cli_state_t *state_p); |
76 | uint32_t cli_consume_path (cli_state_t *state_p); |
77 | void cli_help (const char *prog_name_p, const char *command_name_p, const cli_opt_t *options_p); |
78 | |
79 | #endif /* !CLI_H */ |
80 | |