liboptcfg

annotate src/optcfg.h @ 2:9c73004c7af3

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