1 | .TH GETOPT 1 "May 31, 1997" Linux "" |
1 | .TH GETOPT "1" "June 2012" "Linux" "User Commands" |
2 | .SH NAME |
2 | .SH NAME |
3 | getopt \- parse command options (enhanced) |
3 | getopt \- parse command options (enhanced) |
4 | .SH SYNOPSIS |
4 | .SH SYNOPSIS |
5 | .B getopt |
5 | .BI getopt |
6 | .I optstring parameters |
6 | .I optstring parameters |
7 | .br |
7 | .br |
8 | .B getopt |
8 | .B getopt |
9 | .RI [ options ] |
9 | .RI [ options ] |
10 | .RB [ \-\- ] |
10 | .RB [ \-\- ] |
… | |
… | |
19 | .I parameters |
19 | .I parameters |
20 | .SH DESCRIPTION |
20 | .SH DESCRIPTION |
21 | .B getopt |
21 | .B getopt |
22 | is used to break up |
22 | is used to break up |
23 | .RI ( parse ) |
23 | .RI ( parse ) |
24 | options in command lines for easy parsing by |
24 | options in command lines for easy parsing by shell procedures, and to |
25 | shell procedures, and to check for legal options. |
25 | check for legal options. It uses the |
26 | It uses the |
|
|
27 | .SM GNU |
26 | .SM GNU |
28 | .BR getopt (3) |
27 | .BR getopt (3) |
29 | routines to do this. |
28 | routines to do this. |
30 | |
29 | .PP |
31 | The parameters |
30 | The parameters |
32 | .B getopt |
31 | .B getopt |
33 | is called with can be divided into two parts: options |
32 | is called with can be divided into two parts: options which modify |
34 | which modify the way getopt will parse |
33 | the way |
|
|
34 | .B getopt |
|
|
35 | will do the parsing |
35 | .RI ( options |
36 | .RI "(the " options |
36 | and |
37 | and the |
37 | .BR \-o | \-\-options |
|
|
38 | .I optstring |
38 | .I optstring |
39 | in the |
39 | in the |
40 | .BR SYNOPSIS ), |
40 | .BR SYNOPSIS ), |
41 | and the parameters which are to be |
41 | and the parameters which are to be parsed |
42 | parsed |
|
|
43 | .RI ( parameters |
42 | .RI ( parameters |
44 | in the |
43 | in the |
45 | .BR SYNOPSIS ). |
44 | .BR SYNOPSIS ). |
46 | The second part will start at the first non\-option parameter |
45 | The second part will start at the first non\-option parameter that is |
47 | that is not an option argument, or after the first occurrence of |
46 | not an option argument, or after the first occurrence of |
48 | .RB ` \-\- '. |
47 | .RB ' \-\- '. |
49 | If no |
48 | If no |
50 | .RB ` \-o ' |
49 | .RB ' \-o ' |
51 | or |
50 | or |
52 | .RB ` \-\-options ' |
51 | .RB ' \-\-options ' |
53 | option is found in the first part, the first |
52 | option is found in the first part, the first parameter of the second |
54 | parameter of the second part is used as the short options string. |
53 | part is used as the short options string. |
55 | |
54 | .PP |
56 | If the environment variable |
55 | If the environment variable |
57 | .B GETOPT_COMPATIBLE |
56 | .B GETOPT_COMPATIBLE |
58 | is set, or if its first parameter |
57 | is set, or if the first \fIparameter\fR is not an option (does not start |
59 | is not an option (does not start with a |
58 | with a |
60 | .RB ` \- ', |
59 | .RB ' \- ', |
61 | this is the first format in the |
60 | the first format in the |
62 | .BR SYNOPSIS ), |
61 | .BR SYNOPSIS ), |
63 | .B getopt |
62 | .B getopt |
64 | will generate output that is compatible with that of other versions of |
63 | will generate output that is compatible with that of other versions of |
65 | .BR getopt (1). |
64 | .BR getopt (1). |
66 | It will still do parameter shuffling and recognize optional |
65 | It will still do parameter shuffling and recognize optional arguments |
67 | arguments (see section |
66 | (see section |
68 | .B COMPATIBILITY |
67 | .B COMPATIBILITY |
69 | for more information). |
68 | for more information). |
70 | |
69 | .PP |
71 | Traditional implementations of |
70 | Traditional implementations of |
72 | .BR getopt (1) |
71 | .BR getopt (1) |
73 | are unable to cope with whitespace and other (shell\-specific) special characters |
72 | are unable to cope with whitespace and other (shell\-specific) |
74 | in arguments and non\-option parameters. To solve this problem, this |
73 | special characters in arguments and non\-option parameters. To solve |
75 | implementation can generate |
74 | this problem, this implementation can generate quoted output which |
76 | quoted output which must once again be interpreted by the shell (usually |
75 | must once again be interpreted by the shell (usually by using the |
77 | by using the |
|
|
78 | .B eval |
76 | .B eval |
79 | command). This has the effect of preserving those characters, but |
77 | command). This has the effect of preserving those characters, but |
80 | you must call |
78 | you must call |
81 | .B getopt |
79 | .B getopt |
82 | in a way that is no longer compatible with other versions (the second |
80 | in a way that is no longer compatible with other versions (the second |
83 | or third format in the |
81 | or third format in the |
84 | .BR SYNOPSIS ). |
82 | .BR SYNOPSIS ). |
… | |
… | |
89 | can be used. |
87 | can be used. |
90 | .SH OPTIONS |
88 | .SH OPTIONS |
91 | .TP |
89 | .TP |
92 | .BR \-a , " \-\-alternative" |
90 | .BR \-a , " \-\-alternative" |
93 | Allow long options to start with a single |
91 | Allow long options to start with a single |
94 | .RB ` \- '. |
92 | .RB ' \- '. |
95 | .TP |
93 | .TP |
96 | .BR \-h , " \-\-help" |
94 | .BR \-h , " \-\-help" |
97 | Output a small usage guide and exit successfully. No other output is generated. |
95 | Display help text and exit. No other output is generated. |
98 | .TP |
96 | .TP |
99 | .BR \-l , " \-\-longoptions \fIlongopts\fP" |
97 | .BR \-l , " \-\-longoptions \fIlongopts\fP" |
100 | The long (multi\-character) options to be recognized. |
98 | The long (multi\-character) options to be recognized. More than one |
101 | More than one option name |
|
|
102 | may be specified at once, by separating the names with commas. This option |
99 | option name may be specified at once, by separating the names with |
103 | may be given more than once, the |
100 | commas. This option may be given more than once, the |
104 | .I longopts |
101 | .I longopts |
105 | are cumulative. |
102 | are cumulative. Each long option name in |
106 | Each long option name |
|
|
107 | in |
|
|
108 | .I longopts |
103 | .I longopts |
109 | may be followed by one colon to indicate it has a required argument, and by two colons to indicate it has an optional argument. |
104 | may be followed by one colon to indicate it has a required argument, |
|
|
105 | and by two colons to indicate it has an optional argument. |
110 | .TP |
106 | .TP |
111 | .BR \-n , " \-\-name \fIprogname\fP" |
107 | .BR \-n , " \-\-name \fIprogname\fP" |
112 | The name that will be used by the |
108 | The name that will be used by the |
113 | .BR getopt (3) |
109 | .BR getopt (3) |
114 | routines when it reports errors. Note that errors of |
110 | routines when it reports errors. Note that errors of |
115 | .BR getopt (1) |
111 | .BR getopt (1) |
116 | are still reported as coming from getopt. |
112 | are still reported as coming from getopt. |
117 | .TP |
113 | .TP |
118 | .BR \-o , " \-\-options \fIshortopts\fP" |
114 | .BR \-o , " \-\-options \fIshortopts\fP" |
119 | The short (one\-character) options to be recognized. If this option is not |
115 | The short (one\-character) options to be recognized. If this option |
120 | found, the first parameter of |
116 | is not found, the first parameter of |
121 | .B getopt |
117 | .B getopt |
122 | that does not start with |
118 | that does not start with a |
123 | a |
|
|
124 | .RB ` \- ' |
119 | .RB ' \- ' |
125 | (and is not an option argument) is used as the short options string. |
120 | (and is not an option argument) is used as the short options string. |
126 | Each short option character |
121 | Each short option character in |
127 | in |
|
|
128 | .I shortopts |
122 | .I shortopts |
129 | may be followed by one colon to indicate it has a required argument, |
123 | may be followed by one colon to indicate it has a required argument, |
130 | and by two colons to indicate it has an optional argument. |
124 | and by two colons to indicate it has an optional argument. The first |
131 | The first character of shortopts may be |
125 | character of shortopts may be |
132 | .RB ` + ' |
126 | .RB ' + ' |
133 | or |
127 | or |
134 | .RB ` \- ' |
128 | .RB ' \- ' |
135 | to influence the way |
|
|
136 | options are parsed and output is generated (see section |
129 | to influence the way options are parsed and output is generated (see |
|
|
130 | section |
137 | .B SCANNING MODES |
131 | .B SCANNING MODES |
138 | for details). |
132 | for details). |
139 | .TP |
133 | .TP |
140 | .BR \-q , " \-\-quiet" |
134 | .BR \-q , " \-\-quiet" |
141 | Disable error reporting by getopt(3). |
135 | Disable error reporting by getopt(3). |
142 | .TP |
136 | .TP |
143 | .BR \-Q , " \-\-quiet\-output" |
137 | .BR \-Q , " \-\-quiet\-output" |
144 | Do not generate normal output. Errors are still reported by |
138 | Do not generate normal output. Errors are still reported by |
145 | .BR getopt (3), |
139 | .BR getopt (3), |
146 | unless you also use |
140 | unless you also use |
147 | .IR \-q . |
141 | .BR \-q . |
148 | .TP |
142 | .TP |
149 | .BR \-s , " \-\-shell \fIshell\fP" |
143 | .BR \-s , " \-\-shell \fIshell\fP" |
150 | Set quoting conventions to those of shell. If no \-s argument is found, |
144 | Set quoting conventions to those of \fIshell\fR. |
151 | the |
145 | If the \fB\-s\fR option is not given, the |
152 | .SM BASH |
146 | .SM BASH |
153 | conventions are used. Valid arguments are currently |
147 | conventions are used. Valid arguments are currently |
154 | .RB ` sh ' |
148 | .RB ' sh ' |
155 | .RB ` bash ', |
149 | .RB ' bash ', |
156 | .RB ` csh ', |
150 | .RB ' csh ', |
157 | and |
151 | and |
158 | .RB ` tcsh '. |
152 | .RB ' tcsh '. |
159 | .TP |
153 | .TP |
160 | .BR \-u , " \-\-unquoted" |
154 | .BR \-u , " \-\-unquoted" |
161 | Do not quote the output. Note that whitespace and special (shell\-dependent) |
155 | Do not quote the output. Note that whitespace and special |
162 | characters can cause havoc in this mode (like they do with other |
156 | (shell\-dependent) characters can cause havoc in this mode (like they |
|
|
157 | do with other |
163 | .BR getopt (1) |
158 | .BR getopt (1) |
164 | implementations). |
159 | implementations). |
165 | .TP |
160 | .TP |
166 | .BR \-T , " \-\-test" |
161 | .BR \-T , " \-\-test" |
167 | Test if your |
162 | Test if your |
168 | .BR getopt (1) |
163 | .BR getopt (1) |
169 | is this enhanced version or an old version. This generates no output, |
164 | is this enhanced version or an old version. This generates no |
170 | and sets the error status to 4. Other implementations of |
165 | output, and sets the error status to 4. Other implementations of |
171 | .BR getopt (1), |
166 | .BR getopt (1), |
172 | and this version if the environment variable |
167 | and this version if the environment variable |
173 | .B GETOPT_COMPATIBLE |
168 | .B GETOPT_COMPATIBLE |
174 | is set, |
169 | is set, will return |
175 | will return |
|
|
176 | .RB ` \-\- ' |
170 | .RB ' \-\- ' |
177 | and error status 0. |
171 | and error status 0. |
178 | .TP |
172 | .TP |
179 | .BR \-V , " \-\-version" |
173 | .BR \-V , " \-\-version" |
180 | Output version information and exit successfully. No other output is generated. |
174 | Display version information and exit. No other output is generated. |
181 | .SH PARSING |
175 | .SH PARSING |
182 | This section specifies the format of the second part of the parameters of |
176 | This section specifies the format of the second part of the |
|
|
177 | parameters of |
183 | .B getopt |
178 | .B getopt |
184 | (the |
179 | (the |
185 | .I parameters |
180 | .I parameters |
186 | in the |
181 | in the |
187 | .BR SYNOPSIS ). |
182 | .BR SYNOPSIS ). |
188 | The next section |
183 | The next section |
189 | .RB ( OUTPUT ) |
184 | .RB ( OUTPUT ) |
190 | describes the output that is |
185 | describes the output that is generated. These parameters were |
191 | generated. These parameters were typically the parameters a shell function |
186 | typically the parameters a shell function was called with. Care must |
192 | was called with. |
|
|
193 | Care must be taken that each parameter the shell function was |
187 | be taken that each parameter the shell function was called with |
194 | called with corresponds to exactly one parameter in the parameter list of |
188 | corresponds to exactly one parameter in the parameter list of |
195 | .B getopt |
189 | .B getopt |
196 | (see the |
190 | (see the |
197 | .BR EXAMPLES ). |
191 | .BR EXAMPLES ). |
198 | All parsing is done by the GNU |
192 | All parsing is done by the GNU |
199 | .BR getopt (3) |
193 | .BR getopt (3) |
200 | routines. |
194 | routines. |
201 | |
195 | .PP |
202 | The parameters are parsed from left to right. Each parameter is classified as a |
196 | The parameters are parsed from left to right. Each parameter is |
203 | short option, a long option, an argument to an option, |
197 | classified as a short option, a long option, an argument to an |
204 | or a non\-option parameter. |
198 | option, or a non\-option parameter. |
205 | |
199 | .PP |
206 | A simple short option is a |
200 | A simple short option is a |
207 | .RB ` \- ' |
201 | .RB ' \- ' |
208 | followed by a short option character. If |
202 | followed by a short option character. If the option has a required |
209 | the option has a required argument, it may be written directly after the option |
203 | argument, it may be written directly after the option character or as |
210 | character or as the next parameter (ie. separated by whitespace on the |
204 | the next parameter (i.e. separated by whitespace on the command |
211 | command line). If the |
|
|
212 | option has an optional argument, it must be written directly after the |
205 | line). If the option has an optional argument, it must be written |
213 | option character if present. |
206 | directly after the option character if present. |
214 | |
207 | .PP |
215 | It is possible to specify several short options after one |
208 | It is possible to specify several short options after one |
216 | .RB ` \- ', |
209 | .RB ' \- ', |
217 | as long as all (except possibly the last) do not have required or optional |
210 | as long as all (except possibly the last) do not have required or |
218 | arguments. |
211 | optional arguments. |
219 | |
212 | .PP |
220 | A long option normally begins with |
213 | A long option normally begins with |
221 | .RB ` \-\- ' |
214 | .RB ' \-\- ' |
222 | followed by the long option name. |
215 | followed by the long option name. If the option has a required |
223 | If the option has a required argument, it may be written directly after |
216 | argument, it may be written directly after the long option name, |
224 | the long option name, separated by |
217 | separated by |
225 | .RB ` = ', |
218 | .RB ' = ', |
226 | or as the next argument (ie. separated by whitespace on the command line). |
219 | or as the next argument (i.e. separated by whitespace on the command |
227 | If the option has an optional argument, it must |
220 | line). If the option has an optional argument, it must be written |
228 | be written directly after the long option name, separated by |
221 | directly after the long option name, separated by |
229 | .RB ` = ', |
222 | .RB ' = ', |
230 | if present (if you add the |
223 | if present (if you add the |
231 | .RB ` = ' |
224 | .RB ' = ' |
232 | but nothing behind it, it is interpreted |
225 | but nothing behind it, it is interpreted as if no argument was |
233 | as if no argument was present; this is a slight bug, see the |
226 | present; this is a slight bug, see the |
234 | .BR BUGS ). |
227 | .BR BUGS ). |
235 | Long options may be abbreviated, as long as the abbreviation is not |
228 | Long options may be abbreviated, as long as the abbreviation is not |
236 | ambiguous. |
229 | ambiguous. |
237 | |
230 | .PP |
238 | Each parameter not starting with a |
231 | Each parameter not starting with a |
239 | .RB ` \- ', |
232 | .RB ' \- ', |
240 | and not a required argument of |
233 | and not a required argument of a previous option, is a non\-option |
241 | a previous option, is a non\-option parameter. Each parameter after |
234 | parameter. Each parameter after a |
242 | a |
|
|
243 | .RB ` \-\- ' |
235 | .RB ' \-\- ' |
244 | parameter is always interpreted as a non\-option parameter. |
236 | parameter is always interpreted as a non\-option parameter. If the |
245 | If the environment variable |
237 | environment variable |
246 | .B POSIXLY_CORRECT |
238 | .B POSIXLY_CORRECT |
247 | is set, or if the short |
239 | is set, or if the short option string started with a |
248 | option string started with a |
|
|
249 | .RB ` + ', |
240 | .RB ' + ', |
250 | all remaining parameters are interpreted |
241 | all remaining parameters are interpreted as non\-option parameters as |
251 | as non\-option parameters as soon as the first non\-option parameter is |
242 | soon as the first non\-option parameter is found. |
252 | found. |
|
|
253 | .SH OUTPUT |
243 | .SH OUTPUT |
254 | Output is generated for each element described in the previous section. |
244 | Output is generated for each element described in the previous |
255 | Output is done |
245 | section. Output is done in the same order as the elements are |
256 | in the same order as the elements are specified in the input, except |
246 | specified in the input, except for non\-option parameters. Output |
257 | for non\-option parameters. Output can be done in |
247 | can be done in |
258 | .I compatible |
248 | .I compatible |
259 | .RI ( unquoted ) |
249 | .RI ( unquoted ) |
260 | mode, or in such way that whitespace and other special characters within |
250 | mode, or in such way that whitespace and other special characters |
261 | arguments and non\-option parameters are preserved (see |
251 | within arguments and non\-option parameters are preserved (see |
262 | .BR QUOTING ). |
252 | .BR QUOTING ). |
263 | When the output is processed in the shell script, it will seem to be |
253 | When the output is processed in the shell script, it will seem to be |
264 | composed of distinct elements that can be processed one by one (by using the |
254 | composed of distinct elements that can be processed one by one (by |
265 | shift command in most shell languages). This is imperfect in unquoted mode, |
255 | using the shift command in most shell languages). This is imperfect |
266 | as elements can be split at unexpected places if they contain whitespace |
256 | in unquoted mode, as elements can be split at unexpected places if |
267 | or special characters. |
257 | they contain whitespace or special characters. |
268 | |
258 | .PP |
269 | If there are problems parsing the parameters, for example because a |
259 | If there are problems parsing the parameters, for example because a |
270 | required argument is not found or an option is not recognized, an error |
260 | required argument is not found or an option is not recognized, an |
271 | will be reported on stderr, there will be no output for the offending |
261 | error will be reported on stderr, there will be no output for the |
272 | element, and a non\-zero error status is returned. |
262 | offending element, and a non\-zero error status is returned. |
273 | |
263 | .PP |
274 | For a short option, a single |
264 | For a short option, a single |
275 | .RB ` \- ' |
265 | .RB ' \- ' |
276 | and the option character are generated |
266 | and the option character are generated as one parameter. If the |
277 | as one parameter. If the option has an argument, the next |
267 | option has an argument, the next parameter will be the argument. If |
278 | parameter will be the argument. If the option takes an optional argument, |
268 | the option takes an optional argument, but none was found, the next |
279 | but none was found, the next parameter will be generated but be empty in |
269 | parameter will be generated but be empty in quoting mode, but no |
280 | quoting mode, |
|
|
281 | but no second parameter will be generated in unquoted (compatible) mode. |
270 | second parameter will be generated in unquoted (compatible) mode. |
282 | Note that many other |
271 | Note that many other |
283 | .BR getopt (1) |
272 | .BR getopt (1) |
284 | implementations do not support optional arguments. |
273 | implementations do not support optional arguments. |
285 | |
274 | .PP |
286 | If several short options were specified after a single |
275 | If several short options were specified after a single |
287 | .RB ` \- ', |
276 | .RB ' \- ', |
288 | each will be present in the output as a separate parameter. |
277 | each will be present in the output as a separate parameter. |
289 | |
278 | .PP |
290 | For a long option, |
279 | For a long option, |
291 | .RB ` \-\- ' |
280 | .RB ' \-\- ' |
292 | and the full option name are generated as one |
281 | and the full option name are generated as one parameter. This is |
293 | parameter. This is done regardless whether the option was abbreviated or |
282 | done regardless whether the option was abbreviated or specified with |
294 | specified with a single |
283 | a single |
295 | .RB ` \- ' |
284 | .RB ' \- ' |
296 | in the input. Arguments are handled as with short options. |
285 | in the input. Arguments are handled as with short options. |
297 | |
286 | .PP |
298 | Normally, no non\-option parameters output is generated until all options |
287 | Normally, no non\-option parameters output is generated until all |
299 | and their arguments have been generated. Then |
288 | options and their arguments have been generated. Then |
300 | .RB ` \-\- ' |
289 | .RB ' \-\- ' |
301 | is generated as a |
290 | is generated as a single parameter, and after it the non\-option |
302 | single parameter, and after it the non\-option parameters in the order |
291 | parameters in the order they were found, each as a separate |
303 | they were found, each as a separate parameter. |
|
|
304 | Only if the first character of the short options string was a |
292 | parameter. Only if the first character of the short options string |
|
|
293 | was a |
305 | .RB ` \- ', |
294 | .RB ' \- ', |
306 | non\-option parameter output is generated at the place they are found in the |
295 | non\-option parameter output is generated at the place they are found |
307 | input (this is not supported if the first format of the |
296 | in the input (this is not supported if the first format of the |
308 | .B SYNOPSIS |
297 | .B SYNOPSIS |
309 | is used; in that case all preceding occurrences of |
298 | is used; in that case all preceding occurrences of |
310 | .RB ` \- ' |
299 | .RB ' \- ' |
311 | and |
300 | and |
312 | .RB ` + ' |
301 | .RB ' + ' |
313 | are ignored). |
302 | are ignored). |
314 | .SH QUOTING |
303 | .SH QUOTING |
315 | In compatible mode, whitespace or 'special' characters in arguments or |
304 | In compatible mode, whitespace or 'special' characters in arguments |
316 | non\-option parameters are not handled correctly. As the output is |
305 | or non\-option parameters are not handled correctly. As the output |
317 | fed to the shell script, the script does not know how it is supposed to break |
306 | is fed to the shell script, the script does not know how it is |
318 | the output into separate parameters. To circumvent this |
307 | supposed to break the output into separate parameters. To circumvent |
319 | problem, this implementation offers quoting. The idea is that output |
308 | this problem, this implementation offers quoting. The idea is that |
320 | is generated with quotes around each parameter. When this output is once |
309 | output is generated with quotes around each parameter. When this |
321 | again fed to the shell (usually by a shell |
310 | output is once again fed to the shell (usually by a shell |
322 | .B eval |
311 | .B eval |
323 | command), it is split correctly into separate parameters. |
312 | command), it is split correctly into separate parameters. |
324 | |
313 | .PP |
325 | Quoting is not enabled if the environment variable |
314 | Quoting is not enabled if the environment variable |
326 | .B GETOPT_COMPATIBLE |
315 | .B GETOPT_COMPATIBLE |
327 | is set, if the first form of the |
316 | is set, if the first form of the |
328 | .B SYNOPSIS |
317 | .B SYNOPSIS |
329 | is used, or if the option |
318 | is used, or if the option |
330 | .RB ` \-u ' |
319 | .RB ' \-u ' |
331 | is found. |
320 | is found. |
332 | |
321 | .PP |
333 | Different shells use different quoting conventions. You can use the |
322 | Different shells use different quoting conventions. You can use the |
334 | .RB ` \-s ' |
323 | .RB ' \-s ' |
335 | option to select the shell you are using. The following shells are |
324 | option to select the shell you are using. The following shells are |
336 | currently supported: |
325 | currently supported: |
337 | .RB ` sh ', |
326 | .RB ' sh ', |
338 | .RB ` bash ', |
327 | .RB ' bash ', |
339 | .RB ` csh ' |
328 | .RB ' csh ' |
340 | and |
329 | and |
341 | .RB ` tcsh '. |
330 | .RB ' tcsh '. |
342 | Actually, only two `flavors' are distinguished: sh\-like quoting conventions |
331 | Actually, only two 'flavors' are distinguished: sh\-like quoting |
343 | and csh\-like quoting conventions. Chances are that if you use another shell |
332 | conventions and csh\-like quoting conventions. Chances are that if |
344 | script language, one of these flavors can still be used. |
333 | you use another shell script language, one of these flavors can still |
345 | |
334 | be used. |
346 | .SH "SCANNING MODES" |
335 | .SH "SCANNING MODES" |
347 | The first character of the short options string may be a |
336 | The first character of the short options string may be a |
348 | .RB ` \- ' |
337 | .RB ' \- ' |
349 | or a |
338 | or a |
350 | .RB ` + ' |
339 | .RB ' + ' |
351 | to indicate a special scanning mode. If the first calling form |
340 | to indicate a special scanning mode. If the first calling form in |
352 | in the |
341 | the |
353 | .B SYNOPSIS |
342 | .B SYNOPSIS |
354 | is used they are ignored; the environment variable |
343 | is used they are ignored; the environment variable |
355 | .B POSIXLY_CORRECT |
344 | .B POSIXLY_CORRECT |
356 | is still examined, though. |
345 | is still examined, though. |
357 | |
346 | .PP |
358 | If the first character is |
347 | If the first character is |
359 | .RB ` + ', |
348 | .RB ' + ', |
360 | or if the environment variable |
349 | or if the environment variable |
361 | .B POSIXLY_CORRECT |
350 | .B POSIXLY_CORRECT |
362 | is set, parsing stops as soon as the first non\-option parameter |
351 | is set, parsing stops as soon as the first non\-option parameter |
363 | (ie. a parameter that does not start with a |
352 | (i.e. a parameter that does not start with a |
364 | .RB ` \- ') |
353 | .RB ' \- ') |
365 | is found that |
354 | is found that is not an option argument. The remaining parameters |
366 | is not an option argument. The remaining parameters are all interpreted as |
355 | are all interpreted as non\-option parameters. |
367 | non\-option parameters. |
356 | .PP |
368 | |
|
|
369 | If the first character is a |
357 | If the first character is a |
370 | .RB ` \- ', |
358 | .RB ' \- ', |
371 | non\-option parameters are outputted at the place where they are found; in normal |
359 | non\-option parameters are outputted at the place where they are |
372 | operation, they are all collected at the end of output after a |
360 | found; in normal operation, they are all collected at the end of |
|
|
361 | output after a |
373 | .RB ` \-\- ' |
362 | .RB ' \-\- ' |
374 | parameter has been generated. Note that this |
363 | parameter has been generated. Note that this |
375 | .RB ` \-\- ' |
364 | .RB ' \-\- ' |
376 | parameter is still generated, but it will always be the last parameter in |
365 | parameter is still generated, but it will always be the last |
377 | this mode. |
366 | parameter in this mode. |
378 | .SH COMPATIBILITY |
367 | .SH COMPATIBILITY |
379 | This version of |
368 | This version of |
380 | .BR getopt (1) |
369 | .BR getopt (1) |
381 | is written to be as compatible as possible to |
370 | is written to be as compatible as possible to other versions. |
382 | other versions. Usually you can just replace them with this version |
371 | Usually you can just replace them with this version without any |
383 | without any modifications, and with some advantages. |
372 | modifications, and with some advantages. |
384 | |
373 | .PP |
385 | If the first character of the first parameter of getopt is not a |
374 | If the first character of the first parameter of getopt is not a |
386 | .RB ` \- ', |
375 | .RB ' \- ', |
|
|
376 | .B getopt |
387 | getopt goes into compatibility mode. It will interpret its first parameter as |
377 | goes into compatibility mode. It will interpret its first |
388 | the string of short options, and all other arguments will be parsed. It |
378 | parameter as the string of short options, and all other arguments |
389 | will still do parameter shuffling (ie. all non\-option parameters are outputted |
379 | will be parsed. It will still do parameter shuffling (i.e. all |
390 | at the end), unless the environment variable |
380 | non\-option parameters are output at the end), unless the |
|
|
381 | environment variable |
391 | .B POSIXLY_CORRECT |
382 | .B POSIXLY_CORRECT |
392 | is set. |
383 | is set. |
393 | |
384 | .PP |
394 | The environment variable |
385 | The environment variable |
395 | .B GETOPT_COMPATIBLE |
386 | .B GETOPT_COMPATIBLE |
396 | forces |
387 | forces |
397 | .B getopt |
388 | .B getopt |
398 | into compatibility mode. Setting both this environment variable and |
389 | into compatibility mode. Setting both this environment variable and |
399 | .B POSIXLY_CORRECT |
390 | .B POSIXLY_CORRECT |
400 | offers 100% compatibility for `difficult' programs. Usually, though, |
391 | offers 100% compatibility for 'difficult' programs. Usually, though, |
401 | neither is needed. |
392 | neither is needed. |
402 | |
393 | .PP |
403 | In compatibility mode, leading |
394 | In compatibility mode, leading |
404 | .RB ` \- ' |
395 | .RB ' \- ' |
405 | and |
396 | and |
406 | .RB ` + ' |
397 | .RB ' + ' |
407 | characters in the short options string are ignored. |
398 | characters in the short options string are ignored. |
408 | .SH RETURN CODES |
399 | .SH RETURN CODES |
409 | .B getopt |
400 | .B getopt |
410 | returns error code |
401 | returns error code |
411 | .B 0 |
402 | .B 0 |
… | |
… | |
423 | .BR \-T . |
414 | .BR \-T . |
424 | .SH EXAMPLES |
415 | .SH EXAMPLES |
425 | Example scripts for (ba)sh and (t)csh are provided with the |
416 | Example scripts for (ba)sh and (t)csh are provided with the |
426 | .BR getopt (1) |
417 | .BR getopt (1) |
427 | distribution, and are optionally installed in |
418 | distribution, and are optionally installed in |
428 | .B /usr/local/share/getopt |
419 | .BR /usr/local/share/getopt/ |
429 | or |
420 | or |
430 | .BR /usr/share/getopt . |
421 | .BR /usr/share/getopt/ . |
431 | |
|
|
432 | .SH ENVIRONMENT |
422 | .SH ENVIRONMENT |
433 | .IP POSIXLY_CORRECT |
423 | .IP POSIXLY_CORRECT |
434 | This environment variable is examined by the |
424 | This environment variable is examined by the |
435 | .BR getopt (3) |
425 | .BR getopt (3) |
436 | routines. |
|
|
437 | If it is set, parsing stops as soon as a parameter |
426 | routines. If it is set, parsing stops as soon as a parameter is |
438 | is found that is not an option or an option argument. All remaining |
427 | found that is not an option or an option argument. All remaining |
439 | parameters are also interpreted as non\-option parameters, regardless |
428 | parameters are also interpreted as non\-option parameters, regardless |
440 | whether they start with a |
429 | whether they start with a |
441 | .RB ` \- '. |
430 | .RB ' \- '. |
442 | .IP GETOPT_COMPATIBLE |
431 | .IP GETOPT_COMPATIBLE |
443 | Forces |
432 | Forces |
444 | .B getopt |
433 | .B getopt |
445 | to use the first calling format as specified in the |
434 | to use the first calling format as specified in the |
446 | .BR SYNOPSIS . |
435 | .BR SYNOPSIS . |
447 | .SH BUGS |
436 | .SH BUGS |
448 | .BR getopt (3) |
437 | .BR getopt (3) |
449 | can parse long options with optional arguments that are given an empty optional |
438 | can parse long options with optional arguments that are given an |
450 | argument (but can not do this for short options). This |
439 | empty optional argument (but can not do this for short options). |
|
|
440 | This |
451 | .BR getopt (1) |
441 | .BR getopt (1) |
452 | treats optional arguments that are empty as if they were not present. |
442 | treats optional arguments that are empty as if they were not present. |
453 | |
443 | .PP |
454 | The syntax if you do not want any short option variables at all is |
444 | The syntax if you do not want any short option variables at all is |
455 | not very intuitive (you have to set them explicitly to the empty |
445 | not very intuitive (you have to set them explicitly to the empty |
456 | string). |
446 | string). |
457 | |
|
|
458 | .SH AUTHOR |
447 | .SH AUTHOR |
459 | Frodo Looijaard <frodo@frodo.looijaard.name> |
448 | .MT frodo@frodo.looijaard.name |
|
|
449 | Frodo Looijaard |
|
|
450 | .ME |
460 | .SH "SEE ALSO" |
451 | .SH "SEE ALSO" |
461 | .BR getopt (3), |
452 | .BR getopt (3), |
462 | .BR bash (1), |
453 | .BR bash (1), |
463 | .BR tcsh (1). |
454 | .BR tcsh (1). |
464 | |
455 | .SH AVAILABILITY |
|
|
456 | You can download the latest getopt from |
|
|
457 | .UR http://frodo.looijaard.name/project/getopt |
|
|
458 | the author's home page |
|
|
459 | .UE . |
|
|
460 | It is also part of the util-linux package which is available from |
|
|
461 | .UR ftp://\:ftp.kernel.org\:/pub\:/linux\:/utils\:/util-linux/ |
|
|
462 | Linux Kernel Archive |
|
|
463 | .UE . |