GETOPT(3) 1990 GETOPT(3) NNAAMMEE getopt - get option letter from argv SSYYNNOOPPSSIISS iinntt ggeettoopptt((aarrggcc,, aarrggvv,, ooppttssttrriinngg)) iinntt aarrggcc;; cchhaarr ****aarrggvv;; cchhaarr **ooppttssttrriinngg;; eexxtteerrnn cchhaarr **ooppttaarrgg;; eexxtteerrnn iinntt ooppttiinndd;; eexxtteerrnn iinntt oopptteerrrr;; DDEESSCCRRIIPPTTIIOONN _G_e_t_o_p_t returns the next option letter in _a_r_g_v that matches a letter in _o_p_t_s_t_r_i_n_g. _O_p_t_s_t_r_i_n_g is a string of recognized option letters; if a letter is followed by a colon, the option is expected to have an argument that may or may not be separated from it by white space. On return from _g_e_t_o_p_t, optarg is set to point to the start of any option argument. _O_p_t_i_n_d contains the _a_r_g_v index of the next argument to be processed. _O_p_t_e_r_r and _o_p_t_i_n_d are both initialized to 1. In order to use _g_e_t_o_p_t to evaluate multiple sets of arguments, or to evaluate a single set of arguments multiple times, _o_p_t_i_n_d must be initialized to the number of argv entries to be skipped in each evaluation. When all options have been processed (i.e., up to the first non-option argument), _g_e_t_o_p_t returns EOF. The special option ``--'' may be used to delimit the end of the options; EOF will be returned, and the ``--'' will be skipped. DDIIAAGGNNOOSSTTIICCSS _G_e_t_o_p_t prints an error message on _s_t_d_e_r_r and returns a ques- tion mark (``?'') when it encounters an option letter not included in _o_p_t_s_t_r_i_n_g, or it encounters an option that requires an argument which is not supplied. Setting _o_p_t_e_r_r to a zero will disable these error messages. EEXXAAMMPPLLEE 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; Printed 7/27/90 June 1 GETOPT(3) 1990 GETOPT(3) break; case 'f': if ((fd = open(optarg, O_RDONLY, 0)) < 0) { (void)fprintf(stderr, "myname: unable to read file %s.\n", optarg); exit(1); } break; case '?': default: usage(); } argc -= optind; argv += optind; BBUUGGSS Option arguments are allowed to begin with ``-''; this is reasonable but reduces the amount of error checking possi- ble. A single dash (``-'') may be specified as an character in _o_p_t_s_t_r_i_n_g, however it should nneevveerr have an argument associ- ated with it. This allows _g_e_t_o_p_t 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 provided for backward compatibility oonnllyy. By default, a single dash causes _g_e_t_o_p_t to return EOF. This is, we believe, compatible with System V. It is also possible to handle digits as option letters. This allows _g_e_t_o_p_t 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 oonnllyy. The following code fragment works fairly well. 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 7/27/90 June 2