liboptcfg

annotate src/optcfg.h @ 4:a6f127f3408d

implemented -no- and -disable- prefixes
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 27 Jul 2016 05:05:45 +0300
parents f971dfa66076
children
rev   line source
nuclear@0 1 /* generic unified commandline option and config file parsing library */
nuclear@0 2 #ifndef LIBOPTCFG_H_
nuclear@0 3 #define LIBOPTCFG_H_
nuclear@0 4
nuclear@3 5 #include <stdio.h>
nuclear@3 6
nuclear@0 7 struct optcfg;
nuclear@0 8
nuclear@0 9 struct optcfg_option {
nuclear@4 10 char c; /* short (optional): used only for argument parsing */
nuclear@4 11 const char *s; /* long: used for long options and config files */
nuclear@4 12 int opt; /* the corresponding option enumeration */
nuclear@4 13 const char *desc; /* text description for printing usage information */
nuclear@0 14 };
nuclear@0 15
nuclear@3 16 #define OPTCFG_OPTIONS_END {0, 0, -1, 0}
nuclear@3 17
nuclear@0 18 typedef int (*optcfg_opt_callback)(struct optcfg *oc, int opt, void *cls);
nuclear@0 19 typedef int (*optcfg_arg_callback)(struct optcfg *oc, const char *arg, void *cls);
nuclear@0 20
nuclear@3 21 #ifdef __cplusplus
nuclear@3 22 extern "C" {
nuclear@3 23 #endif
nuclear@3 24
nuclear@0 25 /* initialize the optcfg object with a valid option vector terminated by an
nuclear@0 26 * entry with an opt value of -1 (other fields ignored for termination purposes)
nuclear@0 27 *
nuclear@0 28 * Example:
nuclear@0 29 * struct optcfg_option options[] = {
nuclear@0 30 * {'f', "foo", OPT_FOO, "Makes sure the foo is bar"},
nuclear@0 31 * {'h', "help", OPT_HELP, "Print usage information and exit"},
nuclear@3 32 * OPTCFG_OPTIONS_END
nuclear@0 33 * };
nuclear@0 34 * struct optcfg *oc = optcfg_init(options);
nuclear@0 35 */
nuclear@0 36 struct optcfg *optcfg_init(struct optcfg_option *optv);
nuclear@0 37 void optcfg_destroy(struct optcfg *oc);
nuclear@0 38
nuclear@0 39 /* The parse_* functions call the option callback for each option.
nuclear@0 40 *
nuclear@0 41 * The option callback can then call optcfg_next_value to retrieve any
nuclear@0 42 * values attached to this option. When optcfg_next_value returns 0, there
nuclear@0 43 * are no more values available.
nuclear@0 44 * The option callback must return 0 for success, and -1 to abort parsing.
nuclear@0 45 */
nuclear@0 46 void optcfg_set_opt_callback(struct optcfg *oc, optcfg_opt_callback func, void *cls);
nuclear@0 47 /* the argument callback is only called from optcfg_parse_args(), when a non-option
nuclear@0 48 * argument is encountered (an argument not starting with a dash)
nuclear@0 49 */
nuclear@0 50 void optcfg_set_arg_callback(struct optcfg *oc, optcfg_arg_callback func, void *cls);
nuclear@0 51
nuclear@0 52 enum { OPTCFG_ERROR_FAIL, OPTCFG_ERROR_IGNORE };
nuclear@0 53 void optcfg_set_error_action(struct optcfg *oc, int act);
nuclear@0 54
nuclear@0 55 int optcfg_parse_args(struct optcfg *oc, int argc, char **argv);
nuclear@0 56 int optcfg_parse_config_file(struct optcfg *oc, const char *fname);
nuclear@0 57 int optcfg_parse_config_stream(struct optcfg *oc, FILE *fp);
nuclear@0 58 int optcfg_parse_config_line(struct optcfg *oc, const char *line);
nuclear@0 59 /* TODO custom I/O callback version of config file parsing */
nuclear@0 60
nuclear@2 61 /* special value function which returns if the option is enabled or disabled
nuclear@2 62 * For config files it works similar to calling optcfg_next_value, and
nuclear@2 63 * optcfg_bool_value in sequence.
nuclear@2 64 * For argument parsing however, it doesn't consume further arguments. Merely
nuclear@2 65 * the presence of the option makes it enabled, and its presence with a -no-
nuclear@4 66 * or -disable- prefix disables it.
nuclear@2 67 */
nuclear@2 68 int optcfg_enabled_value(struct optcfg *oc, int *enabledp);
nuclear@2 69
nuclear@0 70 /* call optcfg_next_value in the option callback to retrieve the next value
nuclear@0 71 * of the current option. returns 0 if there is no next value.
nuclear@0 72 */
nuclear@0 73 char *optcfg_next_value(struct optcfg *oc);
nuclear@0 74
nuclear@0 75 /* helper function which can be used to print the available options */
nuclear@0 76 void optcfg_print_options(struct optcfg *oc);
nuclear@0 77
nuclear@0 78 /* helper functions to convert value strings to typed values
nuclear@0 79 * returns 0 for success and value is returned through the valret pointer,
nuclear@0 80 * otherwise it returns -1 for type mismatch, and valret contents are undefined
nuclear@0 81 */
nuclear@0 82 int optcfg_bool_value(char *str, int *valret); /* accepts yes/no, true/false, 1/0 */
nuclear@0 83 int optcfg_int_value(char *str, int *valret);
nuclear@0 84 int optcfg_float_value(char *str, float *valret);
nuclear@0 85
nuclear@3 86 #ifdef __cplusplus
nuclear@3 87 }
nuclear@3 88 #endif
nuclear@0 89
nuclear@0 90 #endif /* LIBOPTCFG_H_ */