COHERENT manpages

This page displays the COHERENT manpage for getopt() [Get option letter from argv].

List of available manpages
Index


getopt() -- General Function (libc)

Get option letter from argv
#include <unistd.h>
int getopt(argc, argv, optstring)
int argc;
char **argv;
char *optstring;
extern char *optarg;
extern int optind;

getopt() returns  the next option letter  in argv that matches  a letter in
optstring. optstring is a string of recognized option letters.  If a letter
is followed by a colon, the  option must have an argument, which may or may
not be separated from it by white space.  optarg points to the start of the
option argument on return from getopt().

getopt()  writes into  optind the  argv index  of the  next argument  to be
processed.  Because  optind is external, it is  normally initialized to one
automatically before the first call to getopt().

When  all options  have been  processed (i.e., up  to the  first non-option
argument), getopt() returns EOF.  The  special option ``--'' may be used to
delimit the end of the options: getopt() returns EOF and skip ``--''.

See Also

libc

Diagnostics

getopt()  prints an  error  message and  returns  a question  mark when  it
encounters an option letter not included in optstring.

Notes

It is not  obvious how `-' standing alone should  be treated.  This version
treats it as a non-option argument, which is not always right.

Option arguments  are allowed to  begin with `-'.  This  is reasonable, but
reduces the amount of error checking possible.

getopt() returns the parsed letter option in the external int optopt, which
is overwritten by each call to  getopt(). When getopt() returns `?', it can
be helpful to examine the contents of this variable.