getopt

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 on which everyone could depend.

getopt dates back to at least 1980[1] 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.

[6] The conventional (POSIX and BSD) handling is that the options end when the first non-option argument is encountered, and that getopt would return -1 to signal that.

In the glibc extension, however, options are allowed anywhere for ease of use; getopt implicitly permutes the argument vector so it still leaves the non-options in the end.

[6] This interface proved popular, and has been taken up (sans the permutation) by many BSD distributions including FreeBSD as well as Solaris.

[7] An alternative way to support long options is seen in Solaris and Korn Shell (extending optstring), but it was not as popular.

If a short name is not defined, getopt will put an index referring to the option structure in the longindex pointer instead.

[13] In 1986, USL decided that being unsafe around metacharacters and whitespace was no longer acceptable, and they created the builtin getopts command for Unix SVR3 Bourne Shell instead.

getopt is a concise description of the common POSIX command argument structure, and it is replicated widely by programmers seeking to provide a similar interface, both to themselves and to the user on the command-line.