GETHOPT(3)

NAME
gethopt - get option letter or word from argv

SYNOPSIS
#include gethopt.h
    struct h_opt {
        int  option;
        char *optword;
        char optchar;
        int  opthasarg;
        char *optdesc;
    } ;

char*
hoptarg(struct h_context* argctx);
int
hoptind(struct h_context* argctx);
int
hopterr(struct h_context* argctx, int flag);
char
hoptopt(struct h_context* argctx);
void
hoptset(struct h_context* argctx, int argc, char** argv);
struct h_opt*
gethopt(struct h_context* argctx, struct h_opt* optarray, int nropts);

DESCRIPTION
The gethopt() function gets the next known option word or character from argctx. An option is known if it has been specified in the array of accepted options optarray.

The option array optarray contains records with either an option word, character, or both, a flag saying the option needs an argument, a short description of the option, and an option key (which is there for the convenience of the calling program; gethopt() does not use it in any way.). It does not matter to getopt() if a following argument has leading white space.

On return from gethopt(), hoptarg() returns an option argument, if it is anticipated, and the variable hoptind() contains the index to the next argv argument for a subsequent call to gethopt().

gethopt uses a (semi) opaque data blob to hold the current state, which must be initialized by the hoptset() function.

The gethopt() function returns HOPTERR if a non-recognized option is encountered, and NULL when it reaches the end of the options on the command line..

The interpretation of options in the argument list may be cancelled by ‘-’ (single dash) or ‘--’ (double dash) which causes getopt() to signal the end of argument processing and return an NULL. When all options have been processed (i.e., up to the first non-option argument), gethopt() returns NULL.

DIAGNOSTICS
If the gethopt() function encounters a character not found in optarray or detects a missing option argument it returns HOPTERR (and writes an error message to the stderr if hopterr() is used to turn error reporting on.)

EXAMPLE
    struct h_opt opts[] = {
        { 0, "css",    0,  1, "css file" },
        { 1, "header", 0,  1, "header file" },
        { 2, 0,       'a', 0, "option a (no arg)" },
        { 3, 0,       'b', 1, "option B (with arg)" },
        { 4, "help",  '?', 0, "help message" },
    } ;
    #define NROPT (sizeof opts/sizeof opts[0])
    int
    main(argc, argv)
    char **argv;
    {
        struct h_opt *ret;
        struct h_context ctx;
        hoptset(&ctx, argc, argv);
        hopterr(&ctx, 1);
        while (( ret = gethopt(&ctx, opts, NROPT) )) {
    	if ( ret != HOPTERR ) {
    	    if ( ret->optword )
    		printf("%s", ret->optword);
    	    else
    		printf("%c", ret->optchar);
    	    if ( ret->opthasarg ) {
    		if ( hoptarg(&ctx) )
    		    printf(" = %s", hoptarg(&ctx));
    		else
    		    printf(" with no argument?");
    	    }
    	    puts(ret->optdesc ? ret->optdesc : "");
    	}
        }
        argc -= hoptind(&ctx);
        argv += hoptind(&ctx);

HISTORY
The gethopt() function was a quick hack to replace manually parsing full-word arguments in discount.