getopt_long

Name

getopt_long -- parse command line options

Synopsis

#define _GNU_SOURCE
#include <getopt.h>

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

Description

getopt_long 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

getopt_long returns the option character if the option was found successfully, or ":" if there was a missing parameter for one of the options, or "?" 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.