- Version 3.5 and earlier: getopt() function not available.
- Version 4.0 and later: getopt() function available.
Breaks up command lines for easy parsing and to check for legal options. This function examines a list of arguments for short options.
A short option consists of a dash followed by a single letter and possibly a parameter. For example, in the command command –a –b name –c, –a and –c are short options with no extra parameter, and -b is a short option with the parameter name.
On the first invocation, getopt() examines the first argument. On subsequent invocations, it picks up where it left off and examines the next argument.
result = getopt (argc, argv, short-option-string)
|argc||Required. Number. The number of entries that are in the argument array list argv.|
|argv||Required. List. The argument array to process.|
|short-option-string||Required. A string that contains valid options. This list contains the letters for the short options. Each letter can be followed by a single colon (:) to indicate a required argument if the option is found. Each letter can be followed by two colons (::) to indicate an optional argument to the option. The leading characters of the short option string can modify the search characteristics as follows: A leading + stops parsing as soon as the first non-option parameter is found that is not an option argument. All other parameters are treated as non-option strings. A leading – returns non-option parameters at the place where they are found.|
If a valid option is found, then the function returns that option. If an optional or required argument is associated with the option, then the policy variable optarg contains the value of that argument.
If no valid option is found or if a required argument is missing, then a question mark (?) is returned. The variable optchar is set to the letter of the problem option.
When the end of the argument list is found, an empty string, "", is returned.
The variable optind is set to the subscript of the next string in the argv list.
result = getopt(argc, argv, "ab:c");
This example examines the list of augments in argv looking for –a or –c without a parameter, or –b with a parameter.