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 21 /** 22 * Command line option definition. 23 */ 24 typedef struct 25 { 26 int id; /**< unique ID of the option (CLI_OPT_DEFAULT, or anything >= 0) */ 27 const char *opt; /**< short option variant (in the form of "x" without dashes) */ 28 const char *longopt; /**< long option variant (in the form of "xxx" without dashes) */ 29 const char *meta; /**< name(s) of the argument(s) of the option, for display only */ 30 const char *help; /**< descriptive help message of the option */ 31 } cli_opt_t; 32 33 /** 34 * Special marker for default option which also marks the end of the option list. 35 */ 36 #define CLI_OPT_DEFAULT -1 37 38 /** 39 * Returned by cli_consume_option () when no more options are available 40 * or an error occured. 41 */ 42 #define CLI_OPT_END -2 43 44 /** 45 * State of the sub-command processor. 46 * No fields should be accessed other than error and arg. 47 */ 48 typedef struct 49 { 50 /* Public fields. */ 51 const char *error; /**< public field for error message */ 52 const char *arg; /**< last processed argument as string */ 53 54 /* Private fields. */ 55 int argc; /**< remaining number of arguments */ 56 char **argv; /**< remaining arguments */ 57 const cli_opt_t *opts; /**< options */ 58 } cli_state_t; 59 60 /** 61 * Macro for writing command line option definition struct literals. 62 */ 63 #define CLI_OPT_DEF(...) /*(cli_opt_t)*/ { __VA_ARGS__ } 64 65 /* 66 * Functions for CLI. 67 */ 68 69 cli_state_t cli_init (const cli_opt_t *options_p, int argc, char **argv); 70 void cli_change_opts (cli_state_t *state_p, const cli_opt_t *options_p); 71 int cli_consume_option (cli_state_t *state_p); 72 const char * cli_consume_string (cli_state_t *state_p); 73 int cli_consume_int (cli_state_t *state_p); 74 void cli_help (const char *prog_name_p, const char *command_name_p, const cli_opt_t *options_p); 75 76 #endif /* !CLI_H */ 77