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