149364d3cSMike Pritchard.\" $FreeBSD$ 249364d3cSMike Pritchard.\" 318b3ba26SMartin Cracauer.Dd April 3, 1999 4b37b9a6dSNate Williams.Dt GETOPT 1 5b37b9a6dSNate Williams.Os 6b37b9a6dSNate Williams.Sh NAME 7b37b9a6dSNate Williams.Nm getopt 8b37b9a6dSNate Williams.Nd parse command options 9b37b9a6dSNate Williams.Sh SYNOPSIS 10eec64f74SMartin Cracauer.Nm args=\`getopt Ar optstring $*\` 11eec64f74SMartin Cracauer; errcode=$?; set \-\- $args 12b37b9a6dSNate Williams.Sh DESCRIPTION 133898680cSPhilippe CharnierThe 143898680cSPhilippe Charnier.Nm 153898680cSPhilippe Charnierutility is used to break up options in command lines for easy parsing by 16b37b9a6dSNate Williamsshell procedures, and to check for legal options. 170b6471c9SJoerg Wunsch.Ar Optstring 18b37b9a6dSNate Williamsis a string of recognized option letters (see 1994ba280cSRuslan Ermilov.Xr getopt 3 ) ; 20b37b9a6dSNate Williamsif a letter is followed by a colon, the option 21b37b9a6dSNate Williamsis expected to have an argument which may or may not be 22b37b9a6dSNate Williamsseparated from it by white space. 23b37b9a6dSNate WilliamsThe special option 240b6471c9SJoerg Wunsch.Ql \-\- 25b37b9a6dSNate Williamsis used to delimit the end of the options. 263898680cSPhilippe CharnierThe 273898680cSPhilippe Charnier.Nm 283898680cSPhilippe Charnierutility will place 290b6471c9SJoerg Wunsch.Ql \-\- 30b37b9a6dSNate Williamsin the arguments at the end of the options, 31b37b9a6dSNate Williamsor recognize it if used explicitly. 32b37b9a6dSNate WilliamsThe shell arguments 33b37b9a6dSNate Williams(\fB$1 $2\fR ...) are reset so that each option is 34b37b9a6dSNate Williamspreceded by a 350b6471c9SJoerg Wunsch.Ql \- 36b37b9a6dSNate Williamsand in its own shell argument; 37b37b9a6dSNate Williamseach option argument is also in its own shell argument. 386c7216dfSRuslan Ermilov.Sh EXIT STATUS 396c7216dfSRuslan ErmilovThe 406c7216dfSRuslan Ermilov.Nm 416c7216dfSRuslan Ermilovutility prints an error message on the standard error output and exits with 426c7216dfSRuslan Ermilovstatus > 0 when it encounters an option letter not included in 436c7216dfSRuslan Ermilov.Ar optstring . 44251c176fSRuslan Ermilov.Sh EXAMPLES 45b37b9a6dSNate WilliamsThe following code fragment shows how one might process the arguments 46b37b9a6dSNate Williamsfor a command that can take the options 470b6471c9SJoerg Wunsch.Fl a 48b37b9a6dSNate Williamsand 490b6471c9SJoerg Wunsch.Fl b , 50b37b9a6dSNate Williamsand the option 510b6471c9SJoerg Wunsch.Fl o , 52b37b9a6dSNate Williamswhich requires an argument. 53b37b9a6dSNate Williams.Pp 54b37b9a6dSNate Williams.Bd -literal -offset indent 55eec64f74SMartin Cracauerargs=\`getopt abo: $*\` 56eec64f74SMartin Cracauer# you should not use \`getopt abo: "$@"\` since that would parse 57eec64f74SMartin Cracauer# the arguments differently from what the set command below does. 58b2a75fdfSRuslan Ermilovif [ $? -ne 0 ] 59b37b9a6dSNate Williamsthen 60b37b9a6dSNate Williams echo 'Usage: ...' 61b37b9a6dSNate Williams exit 2 62b37b9a6dSNate Williamsfi 63eec64f74SMartin Cracauerset \-\- $args 64eec64f74SMartin Cracauer# You cannot use the set command with a backquoted getopt directly, 65eec64f74SMartin Cracauer# since the exit code from getopt would be shadowed by those of set, 66eec64f74SMartin Cracauer# which is zero by definition. 67b37b9a6dSNate Williamsfor i 68b37b9a6dSNate Williamsdo 69b37b9a6dSNate Williams case "$i" 70b37b9a6dSNate Williams in 71b37b9a6dSNate Williams \-a|\-b) 720ab2a7aeSMartin Cracauer echo flag $i set; sflags="${i#-}$sflags"; 730ab2a7aeSMartin Cracauer shift;; 74b37b9a6dSNate Williams \-o) 750ab2a7aeSMartin Cracauer echo oarg is "'"$2"'"; oarg="$2"; shift; 760ab2a7aeSMartin Cracauer shift;; 77b37b9a6dSNate Williams \-\-) 78b37b9a6dSNate Williams shift; break;; 79b37b9a6dSNate Williams esac 80b37b9a6dSNate Williamsdone 81eec64f74SMartin Cracauerecho single-char flags: "'"$sflags"'" 8218b3ba26SMartin Cracauerecho oarg is "'"$oarg"'" 83b37b9a6dSNate Williams.Ed 84b37b9a6dSNate Williams.Pp 85b37b9a6dSNate WilliamsThis code will accept any of the following as equivalent: 86b37b9a6dSNate Williams.Pp 87b37b9a6dSNate Williams.Bd -literal -offset indent 88b37b9a6dSNate Williamscmd \-aoarg file file 89b37b9a6dSNate Williamscmd \-a \-o arg file file 90b37b9a6dSNate Williamscmd \-oarg -a file file 91b37b9a6dSNate Williamscmd \-a \-oarg \-\- file file 92b37b9a6dSNate Williams.Ed 93b37b9a6dSNate Williams.Sh SEE ALSO 94b2a75fdfSRuslan Ermilov.Xr getopts 1 , 95b37b9a6dSNate Williams.Xr sh 1 , 96b37b9a6dSNate Williams.Xr getopt 3 97b37b9a6dSNate Williams.Sh HISTORY 980fdf8af9SAlexey ZelkinWritten by 990fdf8af9SAlexey Zelkin.An Henry Spencer , 1000fdf8af9SAlexey Zelkinworking from a Bell Labs manual page. 10187faa07bSSheldon HearnBehavior believed identical to the Bell version. 10287faa07bSSheldon HearnExample changed in 10318b3ba26SMartin Cracauer.Fx 10418b3ba26SMartin Cracauerversion 3.2 and 4.0. 105b37b9a6dSNate Williams.Sh BUGS 106b37b9a6dSNate WilliamsWhatever 107b37b9a6dSNate Williams.Xr getopt 3 108b37b9a6dSNate Williamshas. 109b37b9a6dSNate Williams.Pp 1100ab2a7aeSMartin CracauerArguments containing white space or embedded shell metacharacters 111eec64f74SMartin Cracauergenerally will not survive intact; this looks easy to fix but 1126a3e8b0aSRuslan Ermilovisn't. 1136a3e8b0aSRuslan ErmilovPeople trying to fix 1148fe908efSRuslan Ermilov.Nm 115eec64f74SMartin Cracaueror the example in this manpage should check the history of this file 116eec64f74SMartin Cracauerin 117eec64f74SMartin Cracauer.Fx . 118b37b9a6dSNate Williams.Pp 119b37b9a6dSNate WilliamsThe error message for an invalid option is identified as coming 120b37b9a6dSNate Williamsfrom 1218fe908efSRuslan Ermilov.Nm 122b37b9a6dSNate Williamsrather than from the shell procedure containing the invocation 123b37b9a6dSNate Williamsof 1248fe908efSRuslan Ermilov.Nm ; 1250ab2a7aeSMartin Cracauerthis again is hard to fix. 1260ab2a7aeSMartin Cracauer.Pp 1270ab2a7aeSMartin CracauerThe precise best way to use the 1280ab2a7aeSMartin Cracauer.Nm set 1290ab2a7aeSMartin Cracauercommand to set the arguments without disrupting the value(s) of 1300ab2a7aeSMartin Cracauershell options varies from one shell version to another. 131eec64f74SMartin Cracauer.Pp 132eec64f74SMartin CracauerEach shellscript has to carry complex code to parse arguments halfway 1336a3e8b0aSRuslan Ermilovcorrectly (like the example presented here). 1346a3e8b0aSRuslan ErmilovA better getopt-like tool 135eec64f74SMartin Cracauerwould move much of the complexity into the tool and keep the client 136eec64f74SMartin Cracauershell scripts simpler. 137