home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
rtsi.com
/
2014.01.www.rtsi.com.tar
/
www.rtsi.com
/
OS9
/
OSK
/
EFFO
/
forum7.lzh
/
RICO
/
MAN
/
man.getopt
< prev
next >
Wrap
Text File
|
1988-09-26
|
4KB
|
182 lines
GETOPT(3) os9-68000 Programmers Manual GETOPT(3)
_N_A_M_E
getopt - get option letter from argv
_S_Y_N_O_P_S_I_S
int getopt(argc, argv, optstring)
int argc;
char **argv;
char *optstring;
extern char *optarg;
extern int optind;
_D_E_S_C_R_I_P_T_I_O_N
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 is expected to have an argument that may or may not
be separated from it by white space. Optarg is set to
point to the start of the option argument on return from
getopt.
Getopt places in optind the argv index of the next argument
to be processed. Because optind is external, it is
normally initialized to zero 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;
EOF will be returned, and -- will be skipped.
_S_E_E _A_L_S_O
getopt(1)
_D_I_A_G_N_O_S_T_I_C_S
Getopt prints an error message on stderr and returns a
question mark ( ? ) when it encounters an option letter not
included in optstring.
_E_X_A_M_P_L_E
The following code fragment shows how one might process the
arguments for a command that can take the mutually
exclusive options a and b , and the options f and o , both
of which require arguments:
GETOPT(3) unix-library page 1
GETOPT(3) os9-68000 Programmers Manual GETOPT(3)
main(argc, argv)
int argc;
char **argv;
{
int c;
extern int optind;
extern char *optarg;
&.
&.
&.
while ((c = getopt(argc, argv, "abf:o:")) != EOF)
switch (c) {
case 'a':
if (bflg)
errflg++;
else
aflg++;
break;
case 'b':
if (aflg)
errflg++;
else
bproc();
break;
case 'f':
ifile = optarg;
break;
case 'o':
ofile = optarg;
break;
case '?':
default:
errflg++;
break;
}
if (errflg) {
fprintf(stderr, "Usage: ...");
exit(2);
}
for (; optind < argc; optind++) {
&.
&.
&.
}
&.
&.
&.
}
_H_I_S_T_O_R_Y
GETOPT(3) unix-library page 2
GETOPT(3) os9-68000 Programmers Manual GETOPT(3)
Written by Henry Spencer, working from a Bell Labs manual
page. Behavior believed identical to the Bell version.
_B_U_G_S
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 is quite flexible but the obvious price must be
paid: there is much it could do that it doesn't, like
checking mutually exclusive options, checking type of
option arguments, etc.
GETOPT(3) unix-library page 3