Getopt
Encyclopedia
getopt is a C
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....

 library
Library (computer science)
In computer science, a library is a collection of resources used to develop software. These may include pre-written code and subroutines, classes, values or type specifications....

 function
Subroutine
In computer science, a subroutine is a portion of code within a larger program that performs a specific task and is relatively independent of the remaining code....

 used to parse command-line options.

It is also the name of a Unix program for parsing command line arguments in shell scripts.

History

A long standing issue with command line programs
Command-line interface
A command-line interface is a mechanism for interacting with a computer operating system or software by typing commands to perform specific tasks...

 was how to specify options;
early programs used many ways of doing so, including single character options (-a),
multiple options specified together (-abc is equivalent to -a -b -c),
multicharacter options (-inum),
options with arguments (-a arg, -inum 3, -a=arg), and
different prefix characters (-a, +b, /c).
The getopt function
Subroutine
In computer science, a subroutine is a portion of code within a larger program that performs a specific task and is relatively independent of the remaining code....

 was written to be a standard mechanism
that all programs could use to parse command-line options
so that there would be a common interface that everyone could depend on.
As such, the original authors picked out of the variations support for single character options,
multiple options specified together,
and options with arguments (-a arg or -aarg), all controllable by an option string.

getopt was first published by AT&T
AT&T
AT&T Inc. is an American multinational telecommunications corporation headquartered in Whitacre Tower, Dallas, Texas, United States. It is the largest provider of mobile telephony and fixed telephony in the United States, and is also a provider of broadband and subscription television services...

 at the 1985 UNIFORUM conference in Dallas, Texas, with the
intent for it to be available in the public domain.
Versions of it were subsequently picked up by other flavors of Unix (BSD 4.3, Linux
Linux
Linux is a Unix-like computer operating system assembled under the model of free and open source software development and distribution. The defining component of any Linux system is the Linux kernel, an operating system kernel first released October 5, 1991 by Linus Torvalds...

, etc.).
It is specified in the POSIX.2
POSIX
POSIX , an acronym for "Portable Operating System Interface", is a family of standards specified by the IEEE for maintaining compatibility between operating systems...

 standard as part of the unistd.h
Unistd.h
In the C and C++ programming languages, unistd.h is the name of the header file that provides access to the POSIX operating system API. It is defined by the POSIX.1 standard, the base of the Single Unix Specification, and should therefore be available in any conforming operating system/compiler .On...

 header file
Header file
Some programming languages use header files. These files allow programmers to separate certain elements of a program's source code into reusable files. Header files commonly contain forward declarations of classes, subroutines, variables, and other identifiers...

.
Derivatives of getopt have been created for many programming language
Programming language
A programming language is an artificial language designed to communicate instructions to a machine, particularly a computer. Programming languages can be used to create programs that control the behavior of a machine and/or to express algorithms precisely....

s to parse command-line options.

A GNU
GNU
GNU is a Unix-like computer operating system developed by the GNU project, ultimately aiming to be a "complete Unix-compatible software system"...

 extension, getopt_long, allows parsing of more readable, multicharacter options, which are introduced by two dashes instead of one.
The choice of two dashes allows multicharacter options (--inum) to be differentiated from single character options specified together (-abc).
The GNU extension also allows an alternative format for options with arguments: --name=arg.

getopt is not a system independent function. The implementation of getopt in GNU C Library does permute the contents of the argument vector as it scans, so that eventually all the non-option arguments are at the end. On the contrary, the implementation of getopt in BSD C Library does not permute the argument vector and returns -1 if it encounters a non-option argument.

Example 1 (using getopt)

  1. include /* for printf */
  2. include /* for exit */
  3. include /* for getopt */

int main (int argc, char **argv) {
int c;
int digit_optind = 0;
int aopt = 0, bopt = 0;
char *copt = 0, *dopt = 0;
while ( (c = getopt(argc, argv, "abc:d:012")) != -1) {
int this_option_optind = optind ? optind : 1;
switch (c) {
case '0':
case '1':
case '2':
if (digit_optind != 0 && digit_optind != this_option_optind)
printf ("digits occur in two different argv-elements.\n");
digit_optind = this_option_optind;
printf ("option %c\n", c);
break;
case 'a':
printf ("option a\n");
aopt = 1;
break;
case 'b':
printf ("option b\n");
bopt = 1;
break;
case 'c':
printf ("option c with value '%s'\n", optarg);
copt = optarg;
break;
case 'd':
printf ("option d with value '%s'\n", optarg);
dopt = optarg;
break;
case '?':
break;
default:
printf ("?? getopt returned character code 0%o ??\n", c);
}
}
if (optind < argc) {
printf ("non-option ARGV-elements: ");
while (optind < argc)
printf ("%s ", argv[optind++]);
printf ("\n");
}
exit (0);
}

Example 2 (using GNU long option variation)

  1. include /* for printf */
  2. include /* for exit */
  3. include /* for getopt_long; standard getopt is in unistd.h */

int main (int argc, char **argv) {
int c;
int digit_optind = 0;
int aopt = 0, bopt = 0;
char *copt = 0, *dopt = 0;
static struct option long_options[] = {
{"add", 1, 0, 0},
{"append", 0, 0, 0},
{"delete", 1, 0, 0},
{"verbose", 0, 0, 0},
{"create", 1, 0, 'c'},
{"file", 1, 0, 0},
{NULL, 0, NULL, 0}
};
int option_index = 0;
while ((c = getopt_long(argc, argv, "abc:d:012",
long_options, &option_index)) != -1) {
int this_option_optind = optind ? optind : 1;
switch (c) {
case 0:
printf ("option %s", long_options[option_index].name);
if (optarg)
printf (" with arg %s", optarg);
printf ("\n");
break;
case '0':
case '1':
case '2':
if (digit_optind != 0 && digit_optind != this_option_optind)
printf ("digits occur in two different argv-elements.\n");
digit_optind = this_option_optind;
printf ("option %c\n", c);
break;
case 'a':
printf ("option a\n");
aopt = 1;
break;
case 'b':
printf ("option b\n");
bopt = 1;
break;
case 'c':
printf ("option c with value '%s'\n", optarg);
copt = optarg;
break;
case 'd':
printf ("option d with value '%s'\n", optarg);
dopt = optarg;
break;
case '?':
break;
default:
printf ("?? getopt returned character code 0%o ??\n", c);
}
}
if (optind < argc) {
printf ("non-option ARGV-elements: ");
while (optind < argc)
printf ("%s ", argv[optind++]);
printf ("\n");
}
exit (0);
}

Shell

The Unix shell command program called getopt
Getopt
getopt is a C library function used to parse command-line options.It is also the name of a Unix program for parsing command line arguments in shell scripts.- History :A long standing issue with command line programs was how to specify options;...

 can be used for parsing command line arguments in shell scripts.

There are two major versions of the getopt shell command program. The original version was implemented by UNIX System Laboratories. There is also a GNU enhanced version that supports additional features (such as long option names and whitespace in parameters).

Command line parsing in shell scripts can also be performed using the getopts
Getopts
getopts is a built-in Unix shell command for parsing command-line arguments.It is designed to process command line arguments that follow the .-History:...

 built-in shell command. The syntax for using getopts is very different to the syntax of getopt. Although getopts has more features than the original getopt program, it does not have some of the features that the GNU enhanced version of getopt does. For example, getopts does not support long option names. The getopts is a built-in Unix shell command, unlike getopt (either the original version or the GNU enhanced version) which is an external command line program.

Haskell

Haskell
Haskell (programming language)
Haskell is a standardized, general-purpose purely functional programming language, with non-strict semantics and strong static typing. It is named after logician Haskell Curry. In Haskell, "a function is a first-class citizen" of the programming language. As a functional programming language, the...

 comes with System.Console.GetOpt in the base library which is essentially a Haskell port of the GNU getopt library.

Java

The Java standard library does not have an implementation of getopt in its standard library. Several third party modules exist, including one that uses the GNU extensions.

Lisp

Lisp has many different dialects with no common standard library. There are some third party implementations of getopt for some dialects of Lisp. Common Lisp
Common Lisp
Common Lisp, commonly abbreviated CL, is a dialect of the Lisp programming language, published in ANSI standard document ANSI INCITS 226-1994 , . From the ANSI Common Lisp standard the Common Lisp HyperSpec has been derived for use with web browsers...

 has a prominent third party implementation.

Pascal

Free Pascal
Free Pascal
Free Pascal Compiler is a free Pascal and Object Pascal compiler.In addition to its own Object Pascal dialect, Free Pascal supports, to varying degrees, the dialects of several other compilers, including those of Turbo Pascal, Delphi, and some historical Macintosh compilers...

 has its own implementation as one of its standard units named GetOpts. It's supported on all platforms.

Perl

Perl
Perl
Perl is a high-level, general-purpose, interpreted, dynamic programming language. Perl was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier. Since then, it has undergone many changes and revisions and become widely popular...

 has two separate derivatives of getopt in its standard library: Getopt::Long and Getopt::Std.

Python

Python
Python (programming language)
Python is a general-purpose, high-level programming language whose design philosophy emphasizes code readability. Python claims to "[combine] remarkable power with very clear syntax", and its standard library is large and comprehensive...

 contains a module in its standard library
Standard library
A standard library for a programming language is the library that is conventionally made available in every implementation of that language. In some cases, the library is described directly in the programming language specification; in other cases, the contents of the standard library are...

 based on C's getopt and GNU extensions. Python's standard library also contains modules to parse options that are more convenient to use.

Ruby

Ruby has an implementation of getopt_long in its standard library, GetoptLong. Ruby also has modules in its standard library with a more sophisticated and convenient interface. A third party implementation of the original getopt interface is available.

.NET Framework

The .NET Framework
.NET Framework
The .NET Framework is a software framework that runs primarily on Microsoft Windows. It includes a large library and supports several programming languages which allows language interoperability...

does not have getopt functionality in its standard library. Third-party implementations are available.

External links

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK