liboptcfg

view src/optcfg.h @ 0:10b89befcaa9

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