getopt_long -- parse command line options


#define _GNU_SOURCE
#include <getopt.h>

int getopt_long(int argc, char * const argv[],
           const char *optstring,
           const struct option *longopts, int *longindex);


The getopt_long() function works like getopt() except that it also accepts long options, started out by two dashes. Long option names may be abbreviated if the abbreviation is unique or is an exact match for some defined option. A long option may take a parameter, of the form --arg=param or --arg param.

longopts is a pointer to the first element of an array of struct option declared in <getopt.h> as

struct option {

const char *name;

int *flag;

int has_arg;

int val;


Return Value

The getopt_long() function returns the option character if the option was found successfully, : if there was a missing parameter for one of the options, ? for an unknown option character, or -1 for the end of the option list.

getopt_long() also returns the option character when a short option is recognized. For a long option, they return val if flag is NULL, and 0 otherwise. Error and -1 returns are the same as for getopt(), plus ? for an ambiguous match or an extraneous parameter.