Jump to content

getopt

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by 118.107.149.10 (talk) at 04:37, 29 November 2010 (Clarified information about the Unix shell getopt command.). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.


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; 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 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 (BSD 4.3, 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 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)

#include <stdio.h>     /* for printf */
#include <stdlib.h>    /* for exit */
#include <unistd.h>    /* 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)

#include <stdio.h>     /* for printf */
#include <stdlib.h>    /* for exit */
#include <getopt.h>    /* 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);
}

Other languages

Shell

The Unix shell command program called getopt can be used for parsing command line arguments in shell scripts.

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 command that is built into the shell (as opposed to getopt which is an external program). The syntax for using getopts is different to the syntax of getopt, and it does not support long option names.

D

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

Haskell

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

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

Perl

Perl has two separate derivatives of getopt in its standard library: Getopt::Long[2] and Getopt::Std.[3]

PHP

PHP has a getopt() function.[4]

Python

Python contains a module in its standard library based on C's getopt and GNU extensions.[5] 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

.Net Framework has a port of getopt functionality.[6]

See also

References