BRL-CAD
Basic Command-line Option Parsing

Special portable re-entrant version of getopt. More...

Collaboration diagram for Basic Command-line Option Parsing:

Files

file  getopt.h
 

Functions

int bu_getopt (int nargc, char *const nargv[], const char *ostr)
 

Variables

int bu_opterr
 
int bu_optind
 
int bu_optopt
 
char * bu_optarg
 

Detailed Description

Special portable re-entrant version of getopt.

Everything is prefixed with bu_, to distinguish it from the various getopt routines found in libc.

Important note - If bu_getopt() is going to be used more than once, it is necessary to reinitialize bu_optind=1 before beginning on the next argument list.

Function Documentation

◆ bu_getopt()

int bu_getopt ( int  nargc,
char *const  nargv[],
const char *  ostr 
)

Get option letter from argument vector.

Parameters
nargcnumber of arguments
nargvarray of argument strings
ostroption string

The 'ostr' option string may contain individual characters (e.g., "abc"), characters followed by a colon to indicate a required argument (e.g., "a:bc", 'a' requires an argument, 'b' and 'c' do not), and characters followed by two colons to indicate an optional argument (e.g., "ab::c", 'b' may be followed by an argument, but none require an argument).

Returns -1 when the argument list is exhausted, otherwise it returns the next known option character. If bu_getopt() encounters a character not found in ostr or if it detects a required missing option argument, it returns ‘?’ (question mark). If ostr has a leading ‘:’ then a missing option argument causes ‘:’ to be returned instead of ‘?’. In either case, the variable bu_optopt is set to the character that caused the error.

Variable Documentation

◆ bu_opterr

int bu_opterr
extern

for bu_getopt(). set to zero to suppress errors.

◆ bu_optind

int bu_optind
extern

for bu_getopt(). current index into parent argv vector.

◆ bu_optopt

int bu_optopt
extern

for bu_getopt(). current option being checked for validity.

◆ bu_optarg

char* bu_optarg
extern

for bu_getopt(). current argument associated with current option.