# HG changeset patch # User John Tsiombikas # Date 1447503150 -7200 # Node ID 8fd2858c6a29d40db7b485ca936ab7c1b901315b # Parent 10b89befcaa9367d114ac94d923f5d35106a1256 example (test) program diff -r 10b89befcaa9 -r 8fd2858c6a29 Makefile --- a/Makefile Fri Nov 13 23:48:31 2015 +0200 +++ b/Makefile Sat Nov 14 14:12:30 2015 +0200 @@ -1,3 +1,7 @@ +# installation prefix, change it to install elsewhere +PREFIX = /usr/local +# ----------------------------- + src = $(wildcard src/*.c) obj = $(src:.c=.o) name = optcfg @@ -41,6 +45,7 @@ cp $(alib) $(DESTDIR)$(PREFIX)/lib/$(alib) cp $(solib) $(DESTDIR)$(PREFIX)/lib/$(solib) [ -n "$(soname)" ] && cd $(DESTDIR)$(PREFIX)/lib && \ + rm -f $(soname) $(ldname) && \ ln -s $(solib) $(soname) && \ ln -s $(soname) $(ldname) || true diff -r 10b89befcaa9 -r 8fd2858c6a29 example/Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/example/Makefile Sat Nov 14 14:12:30 2015 +0200 @@ -0,0 +1,12 @@ +obj = example.o +bin = example + +CFLAGS = -pedantic -Wall -g +LDFLAGS = -loptcfg + +$(bin): $(obj) + $(CC) -o $@ $(obj) $(LDFLAGS) + +.PHONY: clean +clean: + rm -f $(obj) $(bin) diff -r 10b89befcaa9 -r 8fd2858c6a29 example/example.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/example/example.c Sat Nov 14 14:12:30 2015 +0200 @@ -0,0 +1,77 @@ +#include +#include +#include + +enum { + OPT_FOO, + OPT_BAR, + OPT_XYZZY, + OPT_HELP +}; + +struct optcfg_option options[] = { + /* short, long, id, description */ + {'f', "foo", OPT_FOO, "foo does nothing really"}, + {'b', "bar", OPT_BAR, "bar also does nothing"}, + {0, "xyzzy", OPT_XYZZY, "xyzzy doesn't have a short option"}, + {'h', "help", OPT_HELP, "print usage and exit"}, + {0, 0, -1, 0} /* terminate with id=-1 */ +}; + +int foo, bar; +int xyzzy; + +int opt_handler(struct optcfg *oc, int opt, void *cls); + +int main(int argc, char **argv) +{ + /* pass the options array to initialize the optcfg object */ + struct optcfg *oc = optcfg_init(options); + /* set the option callback function */ + optcfg_set_opt_callback(oc, opt_handler, 0); + /* first let's load options from a config file */ + printf("parsing config file: example.conf\n"); + if(optcfg_parse_config_file(oc, "example.conf") == -1) { + return 1; + } + /* then let's override those with commandline options */ + printf("parsing commandline arguments\n"); + if(optcfg_parse_args(oc, argc, argv) == -1) { + return 1; + } + + printf("\nfoo: %s\n", foo ? "true" : "false"); + printf("bar: %s\n", bar ? "true" : "false"); + printf("xyzzy: %d\n", xyzzy); + + optcfg_destroy(oc); + return 0; +} + +int opt_handler(struct optcfg *oc, int opt, void *cls) +{ + char *val; + + switch(opt) { + case OPT_FOO: + foo = 1; + break; + case OPT_BAR: + bar = 1; + break; + case OPT_XYZZY: + /* this option needs an integer value */ + if(!(val = optcfg_next_value(oc)) || optcfg_int_value(val, &xyzzy) == -1) { + fprintf(stderr, "xyzzy must be followed by a number\n"); + return -1; + } + break; + case OPT_HELP: + /* print usage and exit */ + printf("Usage: example [options]\n"); + printf("Options:\n"); + optcfg_print_options(oc); + exit(0); + } + return 0; +} diff -r 10b89befcaa9 -r 8fd2858c6a29 example/example.conf --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/example/example.conf Sat Nov 14 14:12:30 2015 +0200 @@ -0,0 +1,3 @@ +foo = true +bar = false +xyzzy = 42 diff -r 10b89befcaa9 -r 8fd2858c6a29 src/optcfg.c --- a/src/optcfg.c Fri Nov 13 23:48:31 2015 +0200 +++ b/src/optcfg.c Sat Nov 14 14:12:30 2015 +0200 @@ -258,21 +258,26 @@ static int get_opt(struct optcfg *oc, const char *arg) { - int i; + int i, ndashes = 0; - if(arg[0] != '-' || !arg[1]) { - return -1; + while(*arg && *arg == '-') { + ndashes++; + arg++; } - if(arg[2]) { /* match long options */ + if(ndashes > 2) { + arg -= ndashes; + } + + if(arg[1]) { /* match long options */ for(i=0; oc->optlist[i].opt != -1; i++) { - if(strcmp(arg + 1, oc->optlist[i].s) == 0) { + if(strcmp(arg, oc->optlist[i].s) == 0) { return i; } } } else { for(i=0; oc->optlist[i].opt != -1; i++) { - if(arg[1] == oc->optlist[i].c) { + if(arg[0] == oc->optlist[i].c) { return i; } }