liboptcfg

changeset 1:8fd2858c6a29

example (test) program
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 14 Nov 2015 14:12:30 +0200
parents 10b89befcaa9
children 9c73004c7af3
files Makefile example/Makefile example/example.c example/example.conf src/optcfg.c
diffstat 5 files changed, 108 insertions(+), 6 deletions(-) [+]
line diff
     1.1 --- a/Makefile	Fri Nov 13 23:48:31 2015 +0200
     1.2 +++ b/Makefile	Sat Nov 14 14:12:30 2015 +0200
     1.3 @@ -1,3 +1,7 @@
     1.4 +# installation prefix, change it to install elsewhere
     1.5 +PREFIX = /usr/local
     1.6 +# -----------------------------
     1.7 +
     1.8  src = $(wildcard src/*.c)
     1.9  obj = $(src:.c=.o)
    1.10  name = optcfg
    1.11 @@ -41,6 +45,7 @@
    1.12  	cp $(alib) $(DESTDIR)$(PREFIX)/lib/$(alib)
    1.13  	cp $(solib) $(DESTDIR)$(PREFIX)/lib/$(solib)
    1.14  	[ -n "$(soname)" ] && cd $(DESTDIR)$(PREFIX)/lib && \
    1.15 +		rm -f $(soname) $(ldname) && \
    1.16  		ln -s $(solib) $(soname) && \
    1.17  		ln -s $(soname) $(ldname) || true
    1.18  
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/example/Makefile	Sat Nov 14 14:12:30 2015 +0200
     2.3 @@ -0,0 +1,12 @@
     2.4 +obj = example.o
     2.5 +bin = example
     2.6 +
     2.7 +CFLAGS = -pedantic -Wall -g
     2.8 +LDFLAGS = -loptcfg
     2.9 +
    2.10 +$(bin): $(obj)
    2.11 +	$(CC) -o $@ $(obj) $(LDFLAGS)
    2.12 +
    2.13 +.PHONY: clean
    2.14 +clean:
    2.15 +	rm -f $(obj) $(bin)
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/example/example.c	Sat Nov 14 14:12:30 2015 +0200
     3.3 @@ -0,0 +1,77 @@
     3.4 +#include <stdio.h>
     3.5 +#include <stdlib.h>
     3.6 +#include <optcfg.h>
     3.7 +
     3.8 +enum {
     3.9 +	OPT_FOO,
    3.10 +	OPT_BAR,
    3.11 +	OPT_XYZZY,
    3.12 +	OPT_HELP
    3.13 +};
    3.14 +
    3.15 +struct optcfg_option options[] = {
    3.16 +	/* short, long, id, description */
    3.17 +	{'f', "foo", OPT_FOO, "foo does nothing really"},
    3.18 +	{'b', "bar", OPT_BAR, "bar also does nothing"},
    3.19 +	{0, "xyzzy", OPT_XYZZY, "xyzzy doesn't have a short option"},
    3.20 +	{'h', "help", OPT_HELP, "print usage and exit"},
    3.21 +	{0, 0, -1, 0}	/* terminate with id=-1 */
    3.22 +};
    3.23 +
    3.24 +int foo, bar;
    3.25 +int xyzzy;
    3.26 +
    3.27 +int opt_handler(struct optcfg *oc, int opt, void *cls);
    3.28 +
    3.29 +int main(int argc, char **argv)
    3.30 +{
    3.31 +	/* pass the options array to initialize the optcfg object */
    3.32 +	struct optcfg *oc = optcfg_init(options);
    3.33 +	/* set the option callback function */
    3.34 +	optcfg_set_opt_callback(oc, opt_handler, 0);
    3.35 +	/* first let's load options from a config file */
    3.36 +	printf("parsing config file: example.conf\n");
    3.37 +	if(optcfg_parse_config_file(oc, "example.conf") == -1) {
    3.38 +		return 1;
    3.39 +	}
    3.40 +	/* then let's override those with commandline options */
    3.41 +	printf("parsing commandline arguments\n");
    3.42 +	if(optcfg_parse_args(oc, argc, argv) == -1) {
    3.43 +		return 1;
    3.44 +	}
    3.45 +
    3.46 +	printf("\nfoo: %s\n", foo ? "true" : "false");
    3.47 +	printf("bar: %s\n", bar ? "true" : "false");
    3.48 +	printf("xyzzy: %d\n", xyzzy);
    3.49 +
    3.50 +	optcfg_destroy(oc);
    3.51 +	return 0;
    3.52 +}
    3.53 +
    3.54 +int opt_handler(struct optcfg *oc, int opt, void *cls)
    3.55 +{
    3.56 +	char *val;
    3.57 +
    3.58 +	switch(opt) {
    3.59 +	case OPT_FOO:
    3.60 +		foo = 1;
    3.61 +		break;
    3.62 +	case OPT_BAR:
    3.63 +		bar = 1;
    3.64 +		break;
    3.65 +	case OPT_XYZZY:
    3.66 +		/* this option needs an integer value */
    3.67 +		if(!(val = optcfg_next_value(oc)) || optcfg_int_value(val, &xyzzy) == -1) {
    3.68 +			fprintf(stderr, "xyzzy must be followed by a number\n");
    3.69 +			return -1;
    3.70 +		}
    3.71 +		break;
    3.72 +	case OPT_HELP:
    3.73 +		/* print usage and exit */
    3.74 +		printf("Usage: example [options]\n");
    3.75 +		printf("Options:\n");
    3.76 +		optcfg_print_options(oc);
    3.77 +		exit(0);
    3.78 +	}
    3.79 +	return 0;
    3.80 +}
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/example/example.conf	Sat Nov 14 14:12:30 2015 +0200
     4.3 @@ -0,0 +1,3 @@
     4.4 +foo = true
     4.5 +bar = false
     4.6 +xyzzy = 42
     5.1 --- a/src/optcfg.c	Fri Nov 13 23:48:31 2015 +0200
     5.2 +++ b/src/optcfg.c	Sat Nov 14 14:12:30 2015 +0200
     5.3 @@ -258,21 +258,26 @@
     5.4  
     5.5  static int get_opt(struct optcfg *oc, const char *arg)
     5.6  {
     5.7 -	int i;
     5.8 +	int i, ndashes = 0;
     5.9  
    5.10 -	if(arg[0] != '-' || !arg[1]) {
    5.11 -		return -1;
    5.12 +	while(*arg && *arg == '-') {
    5.13 +		ndashes++;
    5.14 +		arg++;
    5.15  	}
    5.16  
    5.17 -	if(arg[2]) {	/* match long options */
    5.18 +	if(ndashes > 2) {
    5.19 +		arg -= ndashes;
    5.20 +	}
    5.21 +
    5.22 +	if(arg[1]) {	/* match long options */
    5.23  		for(i=0; oc->optlist[i].opt != -1; i++) {
    5.24 -			if(strcmp(arg + 1, oc->optlist[i].s) == 0) {
    5.25 +			if(strcmp(arg, oc->optlist[i].s) == 0) {
    5.26  				return i;
    5.27  			}
    5.28  		}
    5.29  	} else {
    5.30  		for(i=0; oc->optlist[i].opt != -1; i++) {
    5.31 -			if(arg[1] == oc->optlist[i].c) {
    5.32 +			if(arg[0] == oc->optlist[i].c) {
    5.33  				return i;
    5.34  			}
    5.35  		}