<s>
Getopt	B-Library
is	O
a	O
C	B-Language
library	B-Library
function	I-Library
used	O
to	O
parse	O
command-line	B-Application
options	O
of	O
the	O
Unix/POSIX	O
style	O
.	O
</s>
<s>
It	O
is	O
a	O
part	O
of	O
the	O
POSIX	O
specification	O
,	O
and	O
is	O
universal	O
to	O
Unix-like	B-Operating_System
systems	I-Operating_System
.	O
</s>
<s>
It	O
is	O
also	O
the	O
name	O
of	O
a	O
Unix	O
program	O
for	O
parsing	O
command	B-Application
line	I-Application
arguments	O
in	O
shell	O
scripts	O
.	O
</s>
<s>
A	O
long-standing	O
issue	O
with	O
command	B-Application
line	I-Application
programs	I-Application
was	O
how	O
to	O
specify	O
options	O
;	O
early	O
programs	O
used	O
many	O
ways	O
of	O
doing	O
so	O
,	O
including	O
single	O
character	O
options	O
(	O
-a	O
)	O
,	O
multiple	O
options	O
specified	O
together	O
(	O
-abc	O
is	O
equivalent	O
to	O
-a	O
-b	O
-c	O
)	O
,	O
multicharacter	O
options	O
(	O
-inum	O
)	O
,	O
options	O
with	O
arguments	O
(	O
-a	O
arg	O
,	O
-inum	O
3	O
,	O
-a	O
=	O
arg	O
)	O
,	O
and	O
different	O
prefix	O
characters	O
(	O
-a	O
,	O
+b	O
,	O
/c	O
)	O
.	O
</s>
<s>
The	O
function	O
was	O
written	O
to	O
be	O
a	O
standard	O
mechanism	O
that	O
all	O
programs	O
could	O
use	O
to	O
parse	O
command-line	B-Application
options	O
so	O
that	O
there	O
would	O
be	O
a	O
common	O
interface	O
on	O
which	O
everyone	O
could	O
depend	O
.	O
</s>
<s>
Versions	O
of	O
it	O
were	O
subsequently	O
picked	O
up	O
by	O
other	O
flavors	O
of	O
Unix	O
(	O
4.3BSD	B-Operating_System
,	O
Linux	B-Application
,	O
etc	O
.	O
)	O
.	O
</s>
<s>
It	O
is	O
specified	O
in	O
the	O
POSIX.2	O
standard	O
as	O
part	O
of	O
the	O
unistd.h	B-Language
header	B-Language
file	I-Language
.	O
</s>
<s>
Derivatives	O
of	O
have	O
been	O
created	O
for	O
many	O
programming	O
languages	O
to	O
parse	O
command-line	B-Application
options	O
.	O
</s>
<s>
is	O
a	O
system	O
dependent	O
function	O
,	O
and	O
its	O
behavior	O
depends	O
on	O
the	O
implementation	O
in	O
the	O
C	B-Language
library	B-Library
.	O
</s>
<s>
Some	O
custom	O
implementations	O
like	O
gnulib	B-Application
are	O
available	O
,	O
however	O
.	O
</s>
<s>
In	O
the	O
glibc	B-Language
extension	O
,	O
however	O
,	O
options	O
are	O
allowed	O
anywhere	O
for	O
ease	O
of	O
use	O
;	O
implicitly	O
permutes	O
the	O
argument	O
vector	O
so	O
it	O
still	O
leaves	O
the	O
non-options	O
in	O
the	O
end	O
.	O
</s>
<s>
A	O
GNU	B-Operating_System
extension	O
,	O
getopt_long	O
,	O
allows	O
parsing	O
of	O
more	O
readable	O
,	O
multicharacter	O
options	O
,	O
which	O
are	O
introduced	O
by	O
two	O
dashes	O
instead	O
of	O
one	O
.	O
</s>
<s>
The	O
GNU	B-Operating_System
extension	O
also	O
allows	O
an	O
alternative	O
format	O
for	O
options	O
with	O
arguments	O
:	O
--name	O
=	O
arg	O
.	O
</s>
<s>
This	O
interface	O
proved	O
popular	O
,	O
and	O
has	O
been	O
taken	O
up	O
(	O
sans	O
the	O
permution	O
)	O
by	O
many	O
BSD	O
distributions	O
including	O
FreeBSD	B-Operating_System
as	O
well	O
as	O
Solaris	B-Application
.	O
</s>
<s>
An	O
alternative	O
way	O
to	O
support	O
long	O
options	O
is	O
seen	O
in	O
Solaris	B-Application
and	O
Korn	B-Language
Shell	I-Language
(	O
extending	O
optstring	O
)	O
,	O
but	O
it	O
was	O
not	O
as	O
popular	O
.	O
</s>
<s>
Another	O
common	O
advanced	O
extension	O
of	O
getopt	B-Library
is	O
resetting	O
the	O
state	O
of	O
argument	O
parsing	O
;	O
this	O
is	O
useful	O
as	O
a	O
replacement	O
of	O
the	O
options-anyware	O
GNU	B-Operating_System
extension	O
,	O
or	O
as	O
a	O
way	O
to	O
"	O
layer	O
"	O
a	O
set	O
of	O
command-line	B-Application
interface	I-Application
with	O
different	O
options	O
at	O
different	O
levels	O
.	O
</s>
<s>
This	O
is	O
achieved	O
in	O
BSD	O
systems	O
using	O
an	O
variable	O
,	O
and	O
on	O
GNU	B-Operating_System
systems	I-Operating_System
by	O
setting	O
to	O
0	O
.	O
</s>
<s>
The	O
command-line	B-Application
syntaxes	O
for	O
getopt-based	O
programs	O
is	O
the	O
POSIX-recommended	O
Utility	O
Argument	O
Syntax	O
.	O
</s>
<s>
All	O
options	O
precede	O
non-option	O
arguments	O
(	O
except	O
for	O
in	O
the	O
GNU	B-Operating_System
extension	O
)	O
.	O
</s>
<s>
Extensions	O
on	O
the	O
syntax	O
include	O
the	O
GNU	B-Operating_System
convention	O
and	O
Sun	O
's	O
specification	O
.	O
</s>
<s>
The	O
getopt	B-Library
manual	O
from	O
GNU	B-Operating_System
specifies	O
such	O
a	O
usage	O
for	O
getopt	B-Library
:	O
</s>
<s>
Here	O
the	O
and	O
are	O
defined	O
exactly	O
like	O
they	O
are	O
in	O
the	O
C	B-Language
function	O
prototype	O
;	O
i.e.	O
,	O
argc	O
indicates	O
the	O
length	O
of	O
the	O
argv	O
array-of-strings	O
.	O
</s>
<s>
GNU	B-Operating_System
here	O
implements	O
a	O
extension	O
for	O
long	O
option	O
synonyms	O
.	O
</s>
<s>
The	O
idiom	O
is	O
to	O
use	O
a	O
while-loop	O
to	O
go	B-Application
through	O
options	O
,	O
and	O
to	O
use	O
a	O
switch-case	O
statement	O
to	O
pick	O
and	O
act	O
on	O
options	O
.	O
</s>
<s>
optind	O
Where	O
getopt	B-Library
is	O
currently	O
looking	O
at	O
in	O
.	O
</s>
<s>
opterr	O
A	O
boolean	O
switch	O
controlling	O
whether	O
getopt	B-Library
should	O
print	O
error	O
messages	O
.	O
</s>
<s>
The	O
GNU	B-Operating_System
extension	O
interface	O
is	O
similar	O
,	O
although	O
it	O
belongs	O
to	O
a	O
different	O
header	B-Language
file	I-Language
and	O
takes	O
an	O
extra	O
option	O
for	O
defining	O
the	O
"	O
short	O
"	O
names	O
of	O
long	O
options	O
and	O
some	O
extra	O
controls	O
.	O
</s>
<s>
If	O
a	O
short	O
name	O
is	O
not	O
defined	O
,	O
getopt	B-Library
will	O
put	O
an	O
index	O
referring	O
to	O
the	O
option	O
structure	O
in	O
the	O
pointer	O
instead	O
.	O
</s>
<s>
To	O
achieve	O
this	O
goal	O
,	O
they	O
turn	O
to	O
getopts	B-Device
and	O
seek	O
to	O
port	O
it	O
to	O
their	O
own	O
language	O
.	O
</s>
<s>
The	O
first	O
attempt	O
at	O
porting	O
was	O
the	O
program	O
getopt	B-Library
,	O
implemented	O
by	O
Unix	O
System	O
Laboratories	O
(	O
USL	O
)	O
.	O
</s>
<s>
It	O
has	O
been	O
inherited	O
to	O
FreeBSD	B-Operating_System
.	O
</s>
<s>
In	O
1986	O
,	O
USL	O
decided	O
that	O
being	O
unsafe	O
around	O
metacharacters	O
and	O
whitespace	O
was	O
no	O
longer	O
acceptable	O
,	O
and	O
they	O
created	O
the	O
builtin	O
getopts	B-Device
command	O
for	O
Unix	O
SVR3	O
Bourne	O
Shell	O
instead	O
.	O
</s>
<s>
In	O
1995	O
,	O
getopts	B-Device
was	O
included	O
in	O
the	O
Single	O
UNIX	O
Specification	O
version	O
1	O
/	O
X/Open	O
Portability	O
Guidelines	O
Issue	O
4	O
.	O
</s>
<s>
Now	O
a	O
part	O
of	O
the	O
POSIX	O
Shell	O
standard	O
,	O
getopts	B-Device
have	O
spread	O
far	O
and	O
wide	O
in	O
many	O
other	O
shells	O
trying	O
to	O
be	O
POSIX-compliant	O
.	O
</s>
<s>
getopt	B-Library
was	O
basically	O
forgotten	O
until	O
util-linux	B-Application
came	O
out	O
with	O
an	O
enhanced	O
version	O
that	O
fixed	O
all	O
of	O
old	O
getopt	B-Library
's	O
problems	O
by	O
escaping	O
.	O
</s>
<s>
It	O
also	O
supports	O
GNU	B-Operating_System
's	O
long	O
option	O
names	O
.	O
</s>
<s>
On	O
the	O
other	O
hand	O
,	O
long	O
options	O
have	O
been	O
implemented	O
rarely	O
in	O
the	O
command	O
in	O
other	O
shells	O
,	O
ksh93	B-Language
being	O
an	O
exception	O
.	O
</s>
<s>
getopt	B-Library
is	O
a	O
concise	O
description	O
of	O
the	O
common	O
POSIX	O
command	O
argument	O
structure	O
,	O
and	O
it	O
is	O
replicated	O
widely	O
by	O
programmers	O
seeking	O
to	O
provide	O
a	O
similar	O
interface	O
,	O
both	O
to	O
themselves	O
and	O
to	O
the	O
user	O
on	O
the	O
command-line	B-Application
.	O
</s>
<s>
C	B-Language
:	O
non-POSIX	O
systems	O
do	O
not	O
ship	O
in	O
the	O
C	B-Language
library	B-Library
,	O
but	O
gnulib	B-Application
and	O
MinGW	B-Application
(	O
both	O
accept	O
GNU-style	O
)	O
,	O
as	O
well	O
as	O
some	O
more	O
minimal	O
libraries	O
,	O
can	O
be	O
used	O
to	O
provide	O
the	O
functionality	O
.	O
</s>
<s>
The	O
library	B-Library
,	O
used	O
by	O
RPM	B-Application
package	I-Application
manager	I-Application
,	O
has	O
the	O
additional	O
advantage	O
of	O
being	O
reentrant	B-Operating_System
.	O
</s>
<s>
The	O
family	O
of	O
functions	O
in	O
glibc	B-Language
and	O
gnulib	B-Application
provides	O
some	O
more	O
convenience	O
and	O
modularity	O
.	O
</s>
<s>
D	B-Application
programming	I-Application
language	I-Application
:	O
has	O
getopt	B-Library
module	O
in	O
the	O
D	B-Application
standard	B-Library
library	I-Library
.	O
</s>
<s>
Go	B-Application
:	O
comes	O
with	O
the	O
package	O
,	O
which	O
allows	O
long	O
flag	O
names	O
.	O
</s>
<s>
The	O
package	O
supports	O
processing	O
closer	O
to	O
the	O
C	B-Language
function	O
.	O
</s>
<s>
Haskell	B-Language
:	O
comes	O
with	O
System.Console.GetOpt	O
,	O
which	O
is	O
essentially	O
a	O
Haskell	B-Language
port	O
of	O
the	O
GNU	B-Operating_System
getopt	B-Library
library	B-Library
.	O
</s>
<s>
Java	B-Language
:	O
There	O
is	O
no	O
implementation	O
of	O
getopt	B-Library
in	O
the	O
Java	B-Language
standard	B-Library
library	I-Library
.	O
</s>
<s>
Several	O
open	O
source	O
modules	O
exist	O
,	O
including	O
gnu.getopt.Getopt	O
,	O
which	O
is	O
ported	O
from	O
GNU	B-Operating_System
getopt	B-Library
,	O
and	O
Apache	B-Language
Commons	I-Language
CLI	O
.	O
</s>
<s>
Lisp	B-Language
:	O
has	O
many	O
different	O
dialects	O
with	O
no	O
common	O
standard	B-Library
library	I-Library
.	O
</s>
<s>
There	O
are	O
some	O
third	O
party	O
implementations	O
of	O
getopt	B-Library
for	O
some	O
dialects	O
of	O
Lisp	B-Language
.	O
</s>
<s>
Common	B-Language
Lisp	I-Language
has	O
a	O
prominent	O
third	O
party	O
implementation	O
.	O
</s>
<s>
Free	B-Operating_System
Pascal	I-Operating_System
:	O
has	O
its	O
own	O
implementation	O
as	O
one	O
of	O
its	O
standard	O
units	O
named	O
GetOpts	B-Device
.	O
</s>
<s>
Perl	B-Language
programming	I-Language
language	I-Language
:	O
has	O
two	O
separate	O
derivatives	O
of	O
getopt	B-Library
in	O
its	O
standard	B-Library
library	I-Library
:	O
Getopt::Long	O
and	O
Getopt::Std	O
.	O
</s>
<s>
PHP	B-Application
:	O
has	O
a	O
getopt	B-Library
function	O
.	O
</s>
<s>
Python	B-Language
:	O
contains	O
a	O
module	O
in	O
its	O
standard	B-Library
library	I-Library
based	O
on	O
C	B-Language
's	O
getopt	B-Library
and	O
GNU	B-Operating_System
extensions	O
.	O
</s>
<s>
Python	B-Language
's	O
standard	B-Library
library	I-Library
also	O
contains	O
other	O
modules	O
to	O
parse	O
options	O
that	O
are	O
more	O
convenient	O
to	O
use	O
.	O
</s>
<s>
Ruby	B-Language
:	O
has	O
an	O
implementation	O
of	O
getopt_long	O
in	O
its	O
standard	B-Library
library	I-Library
,	O
GetoptLong	O
.	O
</s>
<s>
Ruby	B-Language
also	O
has	O
modules	O
in	O
its	O
standard	B-Library
library	I-Library
with	O
a	O
more	O
sophisticated	O
and	O
convenient	O
interface	O
.	O
</s>
<s>
A	O
third	O
party	O
implementation	O
of	O
the	O
original	O
getopt	B-Library
interface	O
is	O
available	O
.	O
</s>
<s>
.NET	B-Application
Framework	I-Application
:	O
does	O
not	O
have	O
getopt	B-Library
functionality	O
in	O
its	O
standard	B-Library
library	I-Library
.	O
</s>
