GETOPT(3) UNIX Programmer's Manual GETOPT(3) NAME getopt - get option character from command line argument list SYNOPSIS #include extern char *optarg; extern int optind; extern int optopt; extern int opterr; extern int optreset; _i_n_t getopt(argc, argv, optstring) _i_n_t _a_r_g_c; _c_h_a_r **_a_r_g_v; _c_h_a_r *_o_p_t_s_t_r_i_n_g; DESCRIPTION The getopt() function incrementally parses a command line argument list _a_r_g_v and returns the next _k_n_o_w_n option charac- ter. An option character is _k_n_o_w_n if it has been specified in the string of accepted option characters, _o_p_t_s_t_r_i_n_g. The option string _o_p_t_s_t_r_i_n_g may contain the following ele- ments: individual characters, and characters followed by a colon to indicate an option argument is to follow. For example, an option string """x"" recognizes an option ``-x'', and an option string """x:"" recognizes an option and argument ``-x _a_r_g_u_m_e_n_t''. It does not matter to getopt() if a following argument has leading white space. On return from getopt(), _o_p_t_a_r_g points to an option argu- ment, if it is anticipated, and the variable _o_p_t_i_n_d contains the index to the next _a_r_g_v argument for a subsequent call to getopt(). The variable _o_p_t_o_p_t saves the last _k_n_o_w_n option character returned by getopt(). The variable _o_p_t_e_r_r and _o_p_t_i_n_d are both initialized to 1. The _o_p_t_i_n_d variable may be set to another value before a set of calls to getopt() in order to skip over more or less argv entries. In order to use getopt() to evaluate multiple sets of argu- ments, or to evaluate a single set of arguments multiple times, the variable _o_p_t_r_e_s_e_t must be set to 1 before the second and each additional set of calls to getopt(), and the variable _o_p_t_i_n_d must be reinitialized. The getopt() function returns an EOF when the argument list is exhausted, or a non-recognized option is encountered. Printed 11/26/99 January 12, 1996 1 GETOPT(3) UNIX Programmer's Manual GETOPT(3) The interpretation of options in the argument list may be cancelled by the option `--' (double dash) which causes getopt() to signal the end of argument processing and return an EOF. When all options have been processed (i.e., up to the first non-option argument), getopt() returns EOF. DIAGNOSTICS If the getopt() function encounters a character not found in the string _o_p_t_a_r_g or detects a missing option argument it writes an error message and returns `?' to the _s_t_d_e_r_r. Set- ting _o_p_t_e_r_r to a zero will disable these error messages. If _o_p_t_s_t_r_i_n_g has a leading `:' then a missing option argument causes a `:' to be returned in addition to suppressing any error messages. Option arguments are allowed to begin with `-'; this is rea- sonable but reduces the amount of error checking possible. EXTENSIONS The _o_p_t_r_e_s_e_t variable was added to make it possible to call the getopt() function multiple times. This is an extension to the IEEE Std1003.2 (``POSIX'') specification. EXAMPLE extern char *optarg; extern int optind; int bflag, ch, fd; bflag = 0; while ((ch = getopt(argc, argv, "bf:")) != EOF) switch(ch) { case 'b': bflag = 1; break; case 'f': if ((fd = open(optarg, O_RDONLY, 0)) < 0) { (void)fprintf(stderr, "myname: %s: %s\n", optarg, strerror(errno)); exit(1); } break; case '?': default: usage(); } argc -= optind; argv += optind; HISTORY The getopt() function appeared 4.3BSD. Printed 11/26/99 January 12, 1996 2 GETOPT(3) UNIX Programmer's Manual GETOPT(3) BUGS A single dash ``-'' may be specified as an character in _o_p_t_- _s_t_r_i_n_g , however it should never have an argument associated with it. This allows getopt() to be used with programs that expect ``-'' as an option flag. This practice is wrong, and should not be used in any current development. It is pro- vided for backward compatibility only . By default, a single dash causes getopt() to return EOF. This is, we believe, compatible with System V. It is also possible to handle digits as option letters. This allows getopt() to be used with programs that expect a number (``-3'') as an option. This practice is wrong, and should not be used in any current development. It is pro- vided for backward compatibility only. The following code fragment works in most cases. int length; char *p; while ((c = getopt(argc, argv, "0123456789")) != EOF) switch (c) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': p = argv[optind - 1]; if (p[0] == '-' && p[1] == ch && !p[2]) length = atoi(++p); else length = atoi(argv[optind] + 1); break; } } Printed 11/26/99 January 12, 1996 3