1*fe7b9c06SThomas Cort.\" $NetBSD: getopt.1,v 1.19 2010/01/24 20:13:28 dholland Exp $ 2*fe7b9c06SThomas Cort.Dd November 28, 2009 3*fe7b9c06SThomas Cort.Dt GETOPT 1 4*fe7b9c06SThomas Cort.Os 5*fe7b9c06SThomas Cort.Sh NAME 6*fe7b9c06SThomas Cort.Nm getopt 7*fe7b9c06SThomas Cort.Nd parse command options 8*fe7b9c06SThomas Cort.Sh SYNOPSIS 9*fe7b9c06SThomas Cort.Li args=\`getopt optstring $*\` 10*fe7b9c06SThomas Cort.Pp 11*fe7b9c06SThomas Cort.Li set \-\- \`getopt optstring $*\` 12*fe7b9c06SThomas Cort.Sh DESCRIPTION 13*fe7b9c06SThomas Cort.Nm 14*fe7b9c06SThomas Cortis used to break up options in command lines for easy parsing by 15*fe7b9c06SThomas Cortshell procedures, and to check for legal options. 16*fe7b9c06SThomas Cort.Op Optstring 17*fe7b9c06SThomas Cortis a string of recognized option letters (see 18*fe7b9c06SThomas Cort.Xr getopt 3 ) ; 19*fe7b9c06SThomas Cortif a letter is followed by a colon, the option 20*fe7b9c06SThomas Cortis expected to have an argument which may or may not be 21*fe7b9c06SThomas Cortseparated from it by white space. 22*fe7b9c06SThomas CortThe special option 23*fe7b9c06SThomas Cort.Dq \-\- 24*fe7b9c06SThomas Cortis used to delimit the end of the options. 25*fe7b9c06SThomas Cort.Nm 26*fe7b9c06SThomas Cortwill place 27*fe7b9c06SThomas Cort.Dq \-\- 28*fe7b9c06SThomas Cortin the arguments at the end of the options, 29*fe7b9c06SThomas Cortor recognize it if used explicitly. 30*fe7b9c06SThomas CortThe shell arguments 31*fe7b9c06SThomas Cort.Pq Ev $1 , Ev $2 , ... 32*fe7b9c06SThomas Cortare reset so that each option is 33*fe7b9c06SThomas Cortpreceded by a 34*fe7b9c06SThomas Cort.Dq \- 35*fe7b9c06SThomas Cortand in its own shell argument; 36*fe7b9c06SThomas Corteach option argument is also in its own shell argument. 37*fe7b9c06SThomas Cort.Pp 38*fe7b9c06SThomas Cort.Nm 39*fe7b9c06SThomas Cortshould not be used in new scripts; use the shell builtin 40*fe7b9c06SThomas Cort.Nm getopts 41*fe7b9c06SThomas Cortinstead. 42*fe7b9c06SThomas Cort.Sh EXAMPLES 43*fe7b9c06SThomas CortThe following code fragment shows how one might process the arguments 44*fe7b9c06SThomas Cortfor a command that can take the options 45*fe7b9c06SThomas Cort.Op a 46*fe7b9c06SThomas Cortand 47*fe7b9c06SThomas Cort.Op b , 48*fe7b9c06SThomas Cortand the option 49*fe7b9c06SThomas Cort.Op c , 50*fe7b9c06SThomas Cortwhich requires an argument. 51*fe7b9c06SThomas Cort.Pp 52*fe7b9c06SThomas Cort.Bd -literal -offset indent 53*fe7b9c06SThomas Cortargs=\`getopt abc: $*\` 54*fe7b9c06SThomas Cortif [ $? \-ne 0 ]; then 55*fe7b9c06SThomas Cort echo 'Usage: ...' 56*fe7b9c06SThomas Cort exit 2 57*fe7b9c06SThomas Cortfi 58*fe7b9c06SThomas Cortset \-\- $args 59*fe7b9c06SThomas Cortwhile [ $# \-gt 0 ]; do 60*fe7b9c06SThomas Cort case "$1" in 61*fe7b9c06SThomas Cort \-a|\-b) 62*fe7b9c06SThomas Cort flag=$1 63*fe7b9c06SThomas Cort ;; 64*fe7b9c06SThomas Cort \-c) 65*fe7b9c06SThomas Cort carg=$2; shift 66*fe7b9c06SThomas Cort ;; 67*fe7b9c06SThomas Cort \-\-) 68*fe7b9c06SThomas Cort shift; break 69*fe7b9c06SThomas Cort ;; 70*fe7b9c06SThomas Cort esac 71*fe7b9c06SThomas Cort shift 72*fe7b9c06SThomas Cortdone 73*fe7b9c06SThomas Cort.Ed 74*fe7b9c06SThomas Cort.Pp 75*fe7b9c06SThomas CortThis code will accept any of the following as equivalent: 76*fe7b9c06SThomas Cort.Pp 77*fe7b9c06SThomas Cort.Bd -literal -offset indent 78*fe7b9c06SThomas Cortcmd \-acarg file file 79*fe7b9c06SThomas Cortcmd \-a \-c arg file file 80*fe7b9c06SThomas Cortcmd \-carg -a file file 81*fe7b9c06SThomas Cortcmd \-a \-carg \-\- file file 82*fe7b9c06SThomas Cort.Ed 83*fe7b9c06SThomas Cort.Pp 84*fe7b9c06SThomas Cort.St -p1003.2 85*fe7b9c06SThomas Cortmandates that the 86*fe7b9c06SThomas Cort.Xr sh 1 87*fe7b9c06SThomas Cortset command return the value of 0 for the exit status. 88*fe7b9c06SThomas CortTherefore, the exit status of the 89*fe7b9c06SThomas Cort.Nm 90*fe7b9c06SThomas Cortcommand is lost when 91*fe7b9c06SThomas Cort.Nm 92*fe7b9c06SThomas Cortand the 93*fe7b9c06SThomas Cort.Xr sh 1 94*fe7b9c06SThomas Cortset command are used on the same line. 95*fe7b9c06SThomas CortThe example given is one way to detect errors found by 96*fe7b9c06SThomas Cort.Nm . 97*fe7b9c06SThomas Cort.Sh DIAGNOSTICS 98*fe7b9c06SThomas Cort.Nm 99*fe7b9c06SThomas Cortprints an error message on the standard error output when it 100*fe7b9c06SThomas Cortencounters an option letter not included in 101*fe7b9c06SThomas Cort.Op optstring . 102*fe7b9c06SThomas Cort.Sh SEE ALSO 103*fe7b9c06SThomas Cort.Xr sh 1 , 104*fe7b9c06SThomas Cort.Xr getopt 3 105*fe7b9c06SThomas Cort.Sh HISTORY 106*fe7b9c06SThomas CortWritten by Henry Spencer, working from a Bell Labs manual page. 107*fe7b9c06SThomas CortBehavior believed identical to the Bell version. 108*fe7b9c06SThomas Cort.Sh BUGS 109*fe7b9c06SThomas CortWhatever 110*fe7b9c06SThomas Cort.Xr getopt 3 111*fe7b9c06SThomas Corthas. 112*fe7b9c06SThomas Cort.Pp 113*fe7b9c06SThomas CortArguments containing white space or embedded shell metacharacters 114*fe7b9c06SThomas Cortgenerally will not survive intact; this looks easy to fix but isn't. 115*fe7b9c06SThomas Cort.Pp 116*fe7b9c06SThomas CortThe error message for an invalid option is identified as coming 117*fe7b9c06SThomas Cortfrom 118*fe7b9c06SThomas Cort.Nm 119*fe7b9c06SThomas Cortrather than from the shell procedure containing the invocation 120*fe7b9c06SThomas Cortof 121*fe7b9c06SThomas Cort.Nm ; 122*fe7b9c06SThomas Cortthis again is hard to fix. 123*fe7b9c06SThomas Cort.Pp 124*fe7b9c06SThomas CortThe precise best way to use the 125*fe7b9c06SThomas Cort.Ic set 126*fe7b9c06SThomas Cortcommand to set the arguments without disrupting the value(s) of 127*fe7b9c06SThomas Cortshell options varies from one shell version to another. 128