Harman Patil (Editor)

Getopt

Updated on
Edit
Like
Comment
Share on FacebookTweet on TwitterShare on LinkedInShare on Reddit

getopt is a C library function used to parse command-line options.

Contents

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; 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 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 dates back to at least 1980 and was first published by AT&T 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 (4.3BSD, Linux, etc.). It is specified in the POSIX.2 standard as part of the unistd.h header file. Derivatives of getopt have been created for many programming languages to parse command-line options.

A GNU 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 a system dependent 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.

Shell

The Unix shell command program called getopt 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. Most Linux based operating systems come with a version, getopt(1), that supports enhancements (such as long option names).

Command line parsing in shell scripts can also be performed using the getopts command, also designed and implemented by Unix System Laboratories as a replacement for getopt. This version is built into many shells and standardized by POSIX (contrary to getopt). The syntax for using getopts is very different from the syntax of getopt. Although getopts has more features than the original getopt program, its implementation as specified by POSIX does not have further enhancments (though some implementations like ksh93's do). For example, implementations of getopts other than ksh93's usually do not support long option names. The getopts command is a built-in Unix shell command, unlike getopt, which is an external command line program. Being a built-in Unix shell command allows it to interact more seamlessly with other built-in shell options and variables.

D

The D programming language has a getopt module in the standard library.

Haskell

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

Java

There is no implementation of getopt in the Java standard library. Several open source modules exist, including gnu.getopt.Getopt, which is ported from GNU getopt, and Apache Commons CLI.

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 has a prominent third party implementation.

Pascal

Free Pascal has its own implementation as one of its standard units named GetOpts. It is supported on all platforms.

Perl

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

PHP

PHP has a getopt() function.

Python

Python contains a module in its standard library based on C's getopt and GNU extensions. Python's standard library also contains other 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 does not have getopt functionality in its standard library. Third-party implementations are available.

References

Getopt Wikipedia