Library for parsing command line options.
C Makefile Shell Smarty
Latest commit 819d90e Oct 21, 2015 @danfis Merge branch 'readme'
Failed to load latest commit information.
arch opts v0.2 Oct 5, 2012
debian Decreased required version of debhelper to 7. Oct 5, 2012
.gitattributes make-release.sh removed from tarball. Jun 17, 2012
.gitignore Created manpages. Jun 17, 2012
BSD-LICENSE Fixed typo in license notes. Sep 12, 2012
CHANGELOG
Makefile Improved building of manual page (because of older versions of asciid… Oct 5, 2012
Makefile.include Fixed installation recipies. Jun 21, 2012
Makefile.local.tpl Fixed typo in license notes. Sep 12, 2012
README.md Added README.md. Oct 21, 2015
alloc.h Changed namespace opts. Jun 1, 2012
make-release.sh Introduced archlinux package build recipe. Jun 17, 2012
man-html.conf
optarg.c Added support for number arrays. Jun 17, 2012
opts.3.txt Added to AUTHOR section to manual page. Oct 5, 2012
opts.c
opts.h Created manpages. Jun 17, 2012
parse.c Changed namespace opts. Jun 1, 2012
test.c
test2.c Created manpages. Jun 17, 2012
unittest.out Added support for number arrays. Jun 17, 2012
unittest.sh

README.md

opts

opts is a library for parsing command line options. It has easy-to-use API that allows you to equip your program with command line options faster than with the ordinary getopt library.

How to use Opts see manpage or Example below.

License

opts is distributed under the 3-clause BSD License.

Example

#include <stdio.h>
#include "opts.h"

static void helpcb(const char *l, char s)
{
    fprintf(stderr, "HelpCB: %s %c\n", l, s);
    fprintf(stderr, "\n");
}

int main(int argc, char *argv[])
{
    float opt1;
    float optarr[3];
    int help;
    int i;

    optarr[0] = optarr[1] = optarr[2] = 0.;

    // define options
    optsAdd("opt1", 'o', OPTS_FLOAT, (void *)&opt1, NULL);
    optsAdd("help", 'h', OPTS_NONE, (void *)&help, OPTS_CB(helpcb));
    optsAddDesc("optarr", 'a', OPTS_FLOAT_ARR(3), (void *)optarr, NULL,
                "Array of three ints");
    // parse options
    if (opts(&argc, argv) != 0){
        fprintf(stderr, "Usage: %s\n", argv[0]);
        optsPrint(stderr, "    ");
        return -1;
    }

    // print some info
    fprintf(stdout, "help: %d\n", help);
    fprintf(stdout, "opt1: %f\n", (float)opt1);
    for (i = 0; i < 3; i++){
        fprintf(stdout, "optarr[%d]: %f\n", i, optarr[i]);
    }

    // print the rest of options
    for (i = 0; i < argc; i++){
        fprintf(stdout, "[%02d]: `%s'\n", i, argv[i]);
    }

    return 0;
}

// If compiled as *test* program the outputs should be:
// $ ./test
// > help: 0
// > opt1: 0.000000
// > optarr[0]: 0.000000
// > optarr[1]: 0.000000
// > optarr[2]: 0.000000
// > [00]: `./test'
//
// $ ./test --opt1 1.1 --opt
// > help: 0
// > opt1: 1.100000
// > optarr[0]: 0.000000
// > optarr[1]: 0.000000
// > optarr[2]: 0.000000
// > [00]: `./test'
// > [01]: `--opt'
//
// $ ./test -o 2.2 -h -a 2,3.1,11
// > HelpCB: help h
// >
// > help: 1
// > opt1: 2.200000
// > optarr[0]: 2.000000
// > optarr[1]: 3.100000
// > optarr[2]: 11.000000
// > [00]: `./test'
//
// $ ./test -o 2.2invalid
// > Invalid argument of -o/--opt1 option.
// > Usage: ./test2
// >     -o / --opt1    float
// >     -h / --help
// >     -a / --optarr  float[]   Array of three ints