Category : OS/2 Files
Archive   : KSH48.ZIP
Filename : GETOPTS.1

 
Output of file : GETOPTS.1 contained in archive : KSH48.ZIP
.TH GETOPTS 1 "January 1988" "Korn shell"
.SH NAME
getopts \- parse command options
.SH SYNOPSIS
.B getopts
optstring name [arg ...]
.SH DESCRIPTION
.I getopts
is used by shell procedures
to parse positional parameters and to check for legal options.
It supports all applicable rules of the command syntax standard
(see Rules 3-10,
.IR intro (1)).
It should be used in place of the
.IR getopt (1)
command.
(See the
.BR \s-1WARNING\s0 ,
below.)
.PP
.I optstring
must contain the option letters the command using
.I getopts
will recognize;
if a letter is followed by a colon,
the option is expected to have an argument
which should be separated from it by white space.
.PP
Each time it is invoked,
.I getopts
will place the next option in the shell variable
.I name
and the index of the next argument to be processed in the shell variable
.BR \s-1OPTIND\s0 .
Whenever the shell or a shell procedure is invoked,
.B \s-1OPTIND\s0
is initialized to
.BR 1 .
.PP
When an option requires an option-argument,
.I getopts
places it in the shell variable
.BR \s-1OPTARG\s0 .
.P
If an illegal option is encountered,
.B ?\&
will be placed in
.IR name .
.P
When the end of the options is encountered,
.I getopts
exits with a non-zero exit status.
The special option
.RB `` \-\- ''
may be used to delimit the end of the options.
.P
By default,
.I getopts
parses the positional parameters.
If extra arguments
.RI ( arg
\&...) are given on the
.I getopts
command line,
.I getopts
will parse them instead.
.PP
So all new commands will adhere to the command syntax standard described in
.IR intro (1),
they should use
.IR getopts (1)
or
.IR getopt (3C)
to parse positional parameters
and check for options that are legal for that command
(see
.BR \s-1WARNINGS\s0 ,
below).
.SH EXAMPLE
The following fragment of a shell program
shows how one might process the arguments
for a command that can take the options
.B a
or
.BR b ,
as well as the option
.BR o ,
which requires an option-argument:
.PP
.RS
.nf
.ta +.5i +1i
\fBwhile getopts abo: c
do
case $c in
a\(bvb) FLAGS=$FLAGS$c;;
o) OARG=$OPTARG;;
\e?) echo $USAGE 1>&2
exit 2;;
esac
done
shift OPTIND\-1\fP
.fi
.ta
.RE
.PP
This code will accept any of the following as equivalent:
.PP
.RS
.nf
\fBcmd \-a \-b \-o "xxx z yy" file
cmd \-a \-b \-o "xxx z yy" \-\- file
cmd \-ab \-o "xxx z yy" file
cmd \-ab \-o "xxx z yy" \-\- file\fP
.fi
.RE
.SH SEE ALSO
intro(1),
sh(1).
.br
getopt(3C)
in the
.IR "Programmer's Reference Manual" .
.br
.IR "UNIX System V Release 3.0 Release Notes" .
.SH WARNING
Although the following command syntax rule (see
.IR intro (1))
relaxations are permitted under the current implementation,
they should not be used because they may not be supported
in future releases of the system.
As in the
.B \s-1EXAMPLE\s0
section above,
.B a
and
.B b
are options,
and the option
.B o
requires an option-argument:
.PP
.RS
.nf
.ta +1i +1.5i
\fBcmd \-aboxxx file\fP (Rule 5 violation: options with
option-arguments must not be grouped with other options)
\fBcmd \-ab \-oxxx file\fP (Rule 6 violation: there must be
white space after an option that takes an option-argument)
.fi
.ta
.RE
.PP
Changing the value of the shell variable
.B \s-1OPTIND\s0
or parsing different sets of arguments
may lead to unexpected results.
.SH DIAGNOSTICS
.I getopts
prints an error message on the standard error output
when it encounters an option letter not included in
.IR optstring .


  3 Responses to “Category : OS/2 Files
Archive   : KSH48.ZIP
Filename : GETOPTS.1

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/