liboptcfg

view 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
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 /* special value function which returns if the option is enabled or disabled
54 * For config files it works similar to calling optcfg_next_value, and
55 * optcfg_bool_value in sequence.
56 * For argument parsing however, it doesn't consume further arguments. Merely
57 * the presence of the option makes it enabled, and its presence with a -no-
58 * prefix disables it. (TODO the second part of this)
59 */
60 int optcfg_enabled_value(struct optcfg *oc, int *enabledp);
62 /* call optcfg_next_value in the option callback to retrieve the next value
63 * of the current option. returns 0 if there is no next value.
64 */
65 char *optcfg_next_value(struct optcfg *oc);
67 /* helper function which can be used to print the available options */
68 void optcfg_print_options(struct optcfg *oc);
70 /* helper functions to convert value strings to typed values
71 * returns 0 for success and value is returned through the valret pointer,
72 * otherwise it returns -1 for type mismatch, and valret contents are undefined
73 */
74 int optcfg_bool_value(char *str, int *valret); /* accepts yes/no, true/false, 1/0 */
75 int optcfg_int_value(char *str, int *valret);
76 int optcfg_float_value(char *str, float *valret);
79 #endif /* LIBOPTCFG_H_ */