getopt

Section: C Library Functions (3)
Index Return to Main Contents
 

NAME

getopt - get option character from command line  

SYNTAX

#include <getopt.h>

char *optarg;
int optind;
int optpos;
int opteof;
int optproblem;
char *optprogname;
int opterr;

int getopt(argc,argv,opts);

int argc;
char **argv;
char *opts;  

DESCRIPTION

This is a clone version of the standard getopt library, built on top of subgetopt(3).

See subgetopt(3) for a detailed description of getopt processing. The main difference between getopt and subgetopt is that getopt prints error messages in case of problems. To turn off these error messages, set opterr (default nonzero) to zero.

This clone version of getopt also provides an optprogname variable. There are two uses for this variable:

(1) By default optprogname is null. When getopt sees this, it attempts to initialize optprogname from argv[0], stripping the directory name. The calling program can use optprogname after calling getopt at least once. This is appropriate if the name of the program should be determined from its command line.

(2) getopt prints optprogname at the beginning of any error messages. So the calling program can, before calling getopt, initialize optprogname as desired. This is appropriate if the name of the program should not be determined from its command line.  

COMPATIBILITY

Old versions of getopt do not include opterr. optpos, opteof, optproblem, and optprogname are specific to this clone version of getopt.

Many features of this clone version of getopt are poorly defined, if available at all, in most versions of getopt. For example, the standard getopt interface does not define optind until the end of the option list. And optarg is not defined unless getopt has just returned an option which takes an argument. In this clone version, optind and optpos always indicate the next character to be read, and optarg is null whenever the current option does not take an argument. See subgetopt(3) for precise definitions of the parsing procedure.

When it reaches the end of the option list, this version of getopt always returns opteof, which is the same as subgetoptdone, which is initialized to SUBGETOPTDONE, which is defined as -1. The standard behavior is to return EOF from stdio(3). This is incompatible on any weird machine where EOF is different from -1. The calling program could set opteof to EOF to imitate the standard behavior.

Like most versions of getopt, this clone version allows, but does not demand, that option arguments be separated from the option by whitespace, i.e., be in the next command-line argument.

Some versions of getopt provide an optopt variable. optopt is incompatible across systems: for example, GNU getopt uses it the same way that this clone version uses optproblem, while BSD getopt uses it to indicate the last option character returned by getopt. This clone version does not provide optopt. The use of optopt is strongly discouraged.

Some versions of getopt do not recognize a double hyphen as the end of arguments. This version allows a double hyphen, or in fact any argument beginning with two hyphens.

A lone hyphen is always recognized as the end of arguments. Some versions of getopt allow lone hyphens as options. This practice is wrong and is strongly discouraged.  

SYNTAX NOTE

getopt is actually a macro abbreviation for getoptmine. The external opterr and optprogname variables are macros for getopterr and getoptprogname. All the other opt variables are macros for subgetopt. These macros are defined in <getopt.h>, unless GETOPTNOSHORT is defined. Further macros are defined in <subgetopt.h>, which is included by <getopt.h>, unless SUBGETOPTNOSHORT is defined.  

VERSION

getopt version 1.9, 931129.  

AUTHOR

Placed into the public domain by Daniel J. Bernstein.


 

Index

NAME
SYNTAX
DESCRIPTION
COMPATIBILITY
SYNTAX NOTE
VERSION
AUTHOR

This document was created by man2html, using the manual pages.
Time: 18:00:51 GMT, June 16, 2008