subgetopt(3) subgetopt(3) NNAAMMEE subgetopt - get option character from command line SSYYNNTTAAXX ##iinncclluuddee <> char *ssggooppttaarrgg; int ssggooppttiinndd; int ssggooppttppooss; int ssggooppttddoonnee; int ssggooppttpprroobblleemm; int ssggoopptt((_a_r_g_c_,_a_r_g_v_,_o_p_t_s)); int _a_r_g_c; char **_a_r_g_v; char *_o_p_t_s; DDEESSCCRRIIPPTTIIOONN ssggoopptt returns the next valid command-line option character from _a_r_g_v. Valid option characters are listed in the _o_p_t_s string. _o_p_t_s may be empty. A character in _o_p_t_s may be followed by a colon, in which case it takes an _o_p_t_i_o_n _a_r_g_u_m_e_n_t. Avoid using the characters ?, :, and - as option characters. Below _o_p_t_i_o_n _a_r_g_u_m_e_n_t is abbreviated as _o_p_t_a_r_g and _c_o_m_m_a_n_d_-_l_i_n_e _a_r_g_u_- _m_e_n_t is abbreviated as _c_m_d_a_r_g. Options are listed in cmdargs which begin with a minus sign. Several options which do not take optargs may be combined into one cmdarg. An option which takes an optarg may be handled in two ways. If it appears at the very end of a cmdarg, then the entire next cmdarg is the optarg. But if there are any characters in the cmdarg after the option character, then those characters form the optarg. The optarg is returned in ssggooppttaarrgg. Next time ssggoopptt looks at the cmdarg which fol- lows the optarg. If a cmdarg does not begin with a hyphen, or if it is a lone hyphen not followed by any characters, or if it begins with two hyphens, then it terminates option processing, and ssggoopptt returns an appropriate code. If there are two hyphens, ssggoopptt will advance attention to the next cmdarg, so it can be called again to read further options. PPRROOPPEERR UUSSAAGGEE ssggooppttpprroobblleemm should be used only when ssggoopptt returns ?. ssggooppttiinndd and ssggooppttppooss are defined all the time. ssggooppttaarrgg is defined all the time; it is null unless ssggoopptt has just returned an option with optarg. ssggoopptt is typically used as follows. #include main(argc,argv) int argc; char **argv; { int opt; while ((opt = sgopt(argc,argv,"a:s")) != sgoptdone) switch(opt) { case ’a’: printf("opt a with optarg %s\n",sgoptarg); break; case ’s’: printf("opt s with no optarg\n"); break; case ’?’: if (argv[sgoptind] && (sgoptind < argc)) printf("illegal opt %c\n",sgoptproblem); else printf("missing arg, opt %c\n",sgoptproblem); exit(1); } argv += sgoptind; while (*argv) printf("argument %s\n",*argv++); exit(0); } The end of the command line is marked by either _a_r_g_c, or a null pointer in _a_r_g_v, whichever comes first. Normally these two markers coincide, so it is redundant to test for both _a_r_g_v[[ssggooppttiinndd]] and ssggooppttiinndd << _a_r_g_c. The above code shows both tests as an illustration. MMuullttiippllee ooppttiioonn sseettss:: One useful technique is to call ssggoopptt with a pri- mary _o_p_t_s until it returns EOF, then call ssggoopptt with a secondary _o_p_t_s until it returns EOF. The user can provide primary options, then a double hyphen, and then secondary options. No special handling is needed if some or all of the options are omitted. The same technique can be used for any number of option sets in series. MMuullttiippllee ccoommmmaanndd lliinneess:: Before parsing a new aarrggvv, make sure to set ssggooppttiinndd and ssggooppttppooss back to 1 and 0. PPAARRSSIINNGG SSTTAAGGEESS ssggoopptt keeps track of its position in _a_r_g_v with ssggooppttiinndd and ssggooppttppooss, which are initialized to 1 and 0. It looks at _a_r_g_v[[ssggooppttiinndd]][[ssggooppttppooss]] and following characters. ssggoopptt indicates that no more options are available by returning ssggoopptt-- ddoonnee, which is initialized to SSUUBBGGEETTOOPPTTDDOONNEE, which is defined as -1. ssggoopptt begins by setting ooppttaarrgg to null. EEnnddiinngg ccoonnddiittiioonnss:: If _a_r_g_v is null, or ssggooppttiinndd is larger than _a_r_g_c, or the current cmdarg _a_r_g_v[[ssggooppttiinndd]] is null, then ssggoopptt returns ooppttddoonnee. SSttaaggee oonnee:: If the current character is zero, ssggoopptt moves to the begin- ning of the next cmdarg. It then checks the ending conditions again. SSttaaggee ttwwoo:: If the current position is the begining of the cmdarg, ssggoopptt checks whether the current character is a minus sign. If not it returns ooppttddoonnee. It then moves to the next character. If that charac- ter is zero, ssggoopptt moves back to the beginning of the cmdarg, and returns ssggooppttddoonnee. If the character is a minus sign, ssggoopptt moves to the beginning of the next cmdarg, and returns ssggooppttddoonnee. SSttaaggee tthhrreeee:: ssggoopptt records the current character, _c, and moves to the next character. There are three possibilities: (1) _c is an option character without optarg in _o_p_t_s, or (2) _c is an option character with optarg in _o_p_t_s, or (3) _c does not appear in _o_p_t_s. (1) If _c appears as an option character without optarg in _o_p_t_s, ssggoopptt returns _c. (2) If _c appears as an option character with optarg in _o_p_t_s, ssggoopptt sets ssggooppttaarrgg to the current position, and moves to the next cmdarg. If ssggooppttaarrgg is nonempty, ssggoopptt returns _c. Then ssggoopptt sets ssggooppttaarrgg to the current cmdarg. If the current cmdarg is null, or past _a_r_g_c, ssggoopptt sets ssggooppttpprroobblleemm to _c and returns ?. Otherwise ssggoopptt moves to the next argument and returns _c. (2) If _c does not appear in _o_p_t_s, ssggoopptt sets ssggooppttpprroobblleemm to _c and returns ?. SSYYNNTTAAXX NNOOTTEE ssggoopptt is actually a macro abbreviation for ssuubbggeettoopptt. The external ssgg variables are also macros for ssuubbggeett. These macros are defined in <>, unless SSUUBBGGEETTOOPPTTNNOOSSHHOORRTT is defined when <> is included. VVEERRSSIIOONN subgetopt version 0.9, 931129. AAUUTTHHOORR Placed into the public domain by Daniel J. Bernstein. subgetopt(3)