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