1.\" $Id: getopt.1,v 1.3 1993/08/02 17:54:28 mycroft Exp $ -*- nroff -*- 2.Dd June 21, 1993 3.Dt GETOPT 1 4.Os 5.Sh NAME 6.Nm getopt 7.Nd parse command options 8.Sh SYNOPSIS 9.Nm set \-\- \`getopt optstring $*\` 10.Sh DESCRIPTION 11.Nm Getopt 12is used to break up options in command lines for easy parsing by 13shell procedures, and to check for legal options. 14.Op Optstring 15is a string of recognized option letters (see 16.Xr getopt 3 17); 18if a letter is followed by a colon, the option 19is expected to have an argument which may or may not be 20separated from it by white space. 21The special option 22.B \-\- 23is used to delimit the end of the options. 24.Nm Getopt 25will place 26.B \-\- 27in the arguments at the end of the options, 28or recognize it if used explicitly. 29The shell arguments 30(\fB$1 $2\fR ...) are reset so that each option is 31preceded by a 32.B \- 33and in its own shell argument; 34each option argument is also in its own shell argument. 35.Sh EXAMPLE 36The following code fragment shows how one might process the arguments 37for a command that can take the options 38.Op a 39and 40.Op b , 41and the option 42.Op o , 43which requires an argument. 44.Pp 45.Bd -literal -offset indent 46set \-\- \`getopt abo: $*\` 47if test $? != 0 48then 49 echo 'Usage: ...' 50 exit 2 51fi 52for i 53do 54 case "$i" 55 in 56 \-a|\-b) 57 flag=$i; shift;; 58 \-o) 59 oarg=$2; shift; shift;; 60 \-\-) 61 shift; break;; 62 esac 63done 64.Ed 65.Pp 66This code will accept any of the following as equivalent: 67.Pp 68.Bd -literal -offset indent 69cmd \-aoarg file file 70cmd \-a \-o arg file file 71cmd \-oarg -a file file 72cmd \-a \-oarg \-\- file file 73.Ed 74.Sh SEE ALSO 75.Xr sh 1 , 76.Xr getopt 3 77.Sh DIAGNOSTICS 78.Nm Getopt 79prints an error message on the standard error output when it 80encounters an option letter not included in 81.Op optstring . 82.Sh HISTORY 83Written by Henry Spencer, working from a Bell Labs manual page. 84Behavior believed identical to the Bell version. 85.Sh BUGS 86Whatever 87.Xr getopt 3 88has. 89.Pp 90Arguments containing white space or imbedded shell metacharacters 91generally will not survive intact; this looks easy to fix but isn't. 92.Pp 93The error message for an invalid option is identified as coming 94from 95.Nm getopt 96rather than from the shell procedure containing the invocation 97of 98.Nm getopt ; 99this again is hard to fix. 100.Pp 101The precise best way to use the 102.Nm set 103command to set the arguments without disrupting the value(s) of 104shell options varies from one shell version to another. 105