getopt.1 (0227791b40a6936310e1dd376b0883b540fec0c0) | getopt.1 (b37b9a6d2d8f98fdefaae729547cc6681bdbfcc7) |
---|---|
1.\" $FreeBSD$ 2.\" 3.Dd April 3, 1999 | 1.Dd June 21, 1993 |
4.Dt GETOPT 1 5.Os 6.Sh NAME 7.Nm getopt 8.Nd parse command options 9.Sh SYNOPSIS | 2.Dt GETOPT 1 3.Os 4.Sh NAME 5.Nm getopt 6.Nd parse command options 7.Sh SYNOPSIS |
10.Nm args=\`getopt Ar optstring $*\` 11; errcode=$?; set \-\- $args | 8.Nm set \-\- \`getopt optstring $*\` |
12.Sh DESCRIPTION | 9.Sh DESCRIPTION |
13The 14.Nm 15utility is used to break up options in command lines for easy parsing by | 10.Nm Getopt 11is used to break up options in command lines for easy parsing by |
16shell procedures, and to check for legal options. | 12shell procedures, and to check for legal options. |
17.Ar Optstring | 13.Op Optstring |
18is a string of recognized option letters (see | 14is a string of recognized option letters (see |
19.Xr getopt 3 ) ; | 15.Xr getopt 3 16); |
20if a letter is followed by a colon, the option 21is expected to have an argument which may or may not be 22separated from it by white space. 23The special option | 17if a letter is followed by a colon, the option 18is expected to have an argument which may or may not be 19separated from it by white space. 20The special option |
24.Ql \-\- | 21.B \-\- |
25is used to delimit the end of the options. | 22is used to delimit the end of the options. |
26The 27.Nm 28utility will place 29.Ql \-\- | 23.Nm Getopt 24will place 25.B \-\- |
30in the arguments at the end of the options, 31or recognize it if used explicitly. 32The shell arguments 33(\fB$1 $2\fR ...) are reset so that each option is 34preceded by a | 26in the arguments at the end of the options, 27or recognize it if used explicitly. 28The shell arguments 29(\fB$1 $2\fR ...) are reset so that each option is 30preceded by a |
35.Ql \- | 31.B \- |
36and in its own shell argument; 37each option argument is also in its own shell argument. | 32and in its own shell argument; 33each option argument is also in its own shell argument. |
38.Sh EXIT STATUS 39The 40.Nm 41utility prints an error message on the standard error output and exits with 42status > 0 when it encounters an option letter not included in 43.Ar optstring . 44.Sh EXAMPLES | 34.Sh EXAMPLE |
45The following code fragment shows how one might process the arguments 46for a command that can take the options | 35The following code fragment shows how one might process the arguments 36for a command that can take the options |
47.Fl a | 37.Op a |
48and | 38and |
49.Fl b , | 39.Op b , |
50and the option | 40and the option |
51.Fl o , | 41.Op o , |
52which requires an argument. 53.Pp 54.Bd -literal -offset indent | 42which requires an argument. 43.Pp 44.Bd -literal -offset indent |
55args=\`getopt abo: $*\` 56# you should not use \`getopt abo: "$@"\` since that would parse 57# the arguments differently from what the set command below does. 58if [ $? -ne 0 ] | 45set \-\- \`getopt abo: $*\` 46if test $? != 0 |
59then 60 echo 'Usage: ...' 61 exit 2 62fi | 47then 48 echo 'Usage: ...' 49 exit 2 50fi |
63set \-\- $args 64# You cannot use the set command with a backquoted getopt directly, 65# since the exit code from getopt would be shadowed by those of set, 66# which is zero by definition. | |
67for i 68do 69 case "$i" 70 in 71 \-a|\-b) | 51for i 52do 53 case "$i" 54 in 55 \-a|\-b) |
72 echo flag $i set; sflags="${i#-}$sflags"; 73 shift;; | 56 flag=$i; shift;; |
74 \-o) | 57 \-o) |
75 echo oarg is "'"$2"'"; oarg="$2"; shift; 76 shift;; | 58 oarg=$2; shift; shift;; |
77 \-\-) 78 shift; break;; 79 esac 80done | 59 \-\-) 60 shift; break;; 61 esac 62done |
81echo single-char flags: "'"$sflags"'" 82echo oarg is "'"$oarg"'" | |
83.Ed 84.Pp 85This code will accept any of the following as equivalent: 86.Pp 87.Bd -literal -offset indent 88cmd \-aoarg file file 89cmd \-a \-o arg file file 90cmd \-oarg -a file file 91cmd \-a \-oarg \-\- file file 92.Ed 93.Sh SEE ALSO | 63.Ed 64.Pp 65This code will accept any of the following as equivalent: 66.Pp 67.Bd -literal -offset indent 68cmd \-aoarg file file 69cmd \-a \-o arg file file 70cmd \-oarg -a file file 71cmd \-a \-oarg \-\- file file 72.Ed 73.Sh SEE ALSO |
94.Xr getopts 1 , | |
95.Xr sh 1 , 96.Xr getopt 3 | 74.Xr sh 1 , 75.Xr getopt 3 |
76.Sh DIAGNOSTICS 77.Nm Getopt 78prints an error message on the standard error output when it 79encounters an option letter not included in 80.Op optstring . |
|
97.Sh HISTORY | 81.Sh HISTORY |
98Written by 99.An Henry Spencer , 100working from a Bell Labs manual page. | 82Written by Henry Spencer, working from a Bell Labs manual page. |
101Behavior believed identical to the Bell version. | 83Behavior believed identical to the Bell version. |
102Example changed in 103.Fx 104version 3.2 and 4.0. | |
105.Sh BUGS 106Whatever 107.Xr getopt 3 108has. 109.Pp | 84.Sh BUGS 85Whatever 86.Xr getopt 3 87has. 88.Pp |
110Arguments containing white space or embedded shell metacharacters 111generally will not survive intact; this looks easy to fix but 112is not. 113People trying to fix 114.Nm 115or the example in this manpage should check the history of this file 116in 117.Fx . | 89Arguments containing white space or imbedded shell metacharacters 90generally will not survive intact; this looks easy to fix but isn't. |
118.Pp 119The error message for an invalid option is identified as coming 120from | 91.Pp 92The error message for an invalid option is identified as coming 93from |
121.Nm | 94.Nm getopt |
122rather than from the shell procedure containing the invocation 123of | 95rather than from the shell procedure containing the invocation 96of |
124.Nm ; | 97.Nm getopt ; |
125this again is hard to fix. 126.Pp 127The precise best way to use the 128.Nm set 129command to set the arguments without disrupting the value(s) of 130shell options varies from one shell version to another. | 98this again is hard to fix. 99.Pp 100The precise best way to use the 101.Nm set 102command to set the arguments without disrupting the value(s) of 103shell options varies from one shell version to another. |
131.Pp 132Each shellscript has to carry complex code to parse arguments halfway 133correctly (like the example presented here). 134A better getopt-like tool 135would move much of the complexity into the tool and keep the client 136shell scripts simpler. | 104varies from one shell version to another. |