liboptcfg

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