OSDN Git Service

0bfcb5a4735448766c0223e82f6bbdf8c84419ee
[linuxjm/LDP_man-pages.git] / release / man3 / getopt.3
1 .\" Copyright (c) 1993 by Thomas Koenig (ig25@rz.uni-karlsruhe.de)
2 .\"
3 .\" Permission is granted to make and distribute verbatim copies of this
4 .\" manual provided the copyright notice and this permission notice are
5 .\" preserved on all copies.
6 .\"
7 .\" Permission is granted to copy and distribute modified versions of this
8 .\" manual under the conditions for verbatim copying, provided that the
9 .\" entire resulting derived work is distributed under the terms of a
10 .\" permission notice identical to this one.
11 .\"
12 .\" Since the Linux kernel and libraries are constantly changing, this
13 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
14 .\" responsibility for errors or omissions, or for damages resulting from
15 .\" the use of the information contained herein.  The author(s) may not
16 .\" have taken the same level of care in the production of this manual,
17 .\" which is licensed free of charge, as they might when working
18 .\" professionally.
19 .\"
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
22 .\" License.
23 .\" Modified Sat Jul 24 19:27:50 1993 by Rik Faith (faith@cs.unc.edu)
24 .\" Modified Mon Aug 30 22:02:34 1995 by Jim Van Zandt <jrv@vanzandt.mv.com>
25 .\"  longindex is a pointer, has_arg can take 3 values, using consistent
26 .\"  names for optstring and longindex, "\n" in formats fixed.  Documenting
27 .\"  opterr and getopt_long_only.  Clarified explanations (borrowing heavily
28 .\"  from the source code).
29 .\" Modified 8 May 1998 by Joseph S. Myers (jsm28@cam.ac.uk)
30 .\" Modified 990715, aeb: changed `EOF' into `-1' since that is what POSIX
31 .\"  says; moreover, EOF is not defined in <unistd.h>.
32 .\" Modified 2002-02-16, joey: added information about nonexisting
33 .\"  option character and colon as first option character
34 .\" Modified 2004-07-28, Michael Kerrisk <mtk.manpages@gmail.com>
35 .\"     Added text to explain how to order both '[-+]' and ':' at
36 .\"             the start of optstring
37 .\" Modified 2006-12-15, mtk, Added getopt() example program.
38 .\"
39 .\" Japanese Version Copyright (c) 1998 NAKANO Takeo all rights reserved.
40 .\" Translated 1998-08-12, NAKANO Takeo <nakano@apm.seikei.ac.jp>
41 .\" Updated & Modified 1999-08-21, NAKANO Takeo <nakano@apm.seikei.ac.jp>
42 .\" Updated & Modified 2001-07-01, Yuichi SATO <ysato@h4.dion.ne.jp>
43 .\" Updated & Modified 2002-03-23, Yuichi SATO
44 .\" Updated & Modified 2003-09-07, Yuichi SATO <ysato444@yahoo.co.jp>
45 .\" Updated & Modified 2005-01-10, Yuichi SATO
46 .\" Updated & Modified 2005-10-28, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
47 .\" Updated & Modified 2006-01-18, Akihiro MOTOKI
48 .\" Updated & Modified 2007-05-01, Akihiro MOTOKI, LDP v2.46
49 .\" Updated 2008-08-11, Akihiro MOTOKI, LDP v3.05
50 .\"
51 .\"WORD:        argv-element            argv 要素
52 .\"
53 .TH GETOPT 3  2010-11-01 "GNU" "Linux Programmer's Manual"
54 .SH 名前
55 getopt, getopt_long, getopt_long_only,
56 optarg, optind, opterr, optopt \- コマンドラインオプションを解釈する
57 .SH 書式
58 .nf
59 .B #include <unistd.h>
60 .sp
61 .BI "int getopt(int " argc ", char * const " argv[] ,
62 .BI "           const char *" optstring );
63 .sp
64 .BI "extern char *" optarg ;
65 .BI "extern int " optind ", " opterr ", " optopt ;
66 .sp
67 .B #include <getopt.h>
68 .sp
69 .BI "int getopt_long(int " argc ", char * const " argv[] ,
70 .BI "           const char *" optstring ,
71 .BI "           const struct option *" longopts ", int *" longindex );
72 .sp
73 .BI "int getopt_long_only(int " argc ", char * const " argv[] ,
74 .BI "           const char *" optstring ,
75 .BI "           const struct option *" longopts ", int *" longindex );
76 .fi
77 .sp
78 .in -4n
79 glibc 向けの機能検査マクロの要件
80 .RB ( feature_test_macros (7)
81 参照):
82 .ad l
83 .in
84 .sp
85 .BR getopt ():
86 _POSIX_C_SOURCE\ >=\ 2 || _XOPEN_SOURCE
87 .br
88 .BR getopt_long (),
89 .BR getopt_long_only ():
90 _GNU_SOURCE
91 .ad b
92 .SH 説明
93 .BR getopt ()
94 関数はコマンドライン引き数を解釈する。
95 .BR getopt ()
96 がとる引き数
97 .I argc
98
99 .I argv
100 は、それぞれプログラムの起動時に
101 .IR main ()
102 関数に渡された引き数の個数と配列である。
103 \fIargv\fP の要素のうち \(aq\-\(aq で始まるもの
104 (かつ "\-" 単独や "\-\-" 単独ではないもの) は
105 オプション要素 (option element) とみなされる。
106 この要素から先頭の \(aq\-\(aq を除いた文字は
107 オプション文字 (option character) とされる。
108 .BR getopt ()
109 は、繰り返し呼び出されるごとに、次のオプション文字を返す。
110 .PP
111 変数
112 .I optind
113 は、
114 .I argv
115 の次に処理される要素のインデックスである。
116 システムによりこの変数の値は 1 に初期化される。
117 呼び出し側でこの値を 1 にリセットすることで、同じ
118 .I argv
119 のスキャンをやり直したり、新しい引き数ベクトルをスキャンすることができる。
120 .PP
121 新たなオプション文字を見つけると、
122 .BR getopt ()
123 はその文字を返し、
124 外部変数 \fIoptind\fP とスタティックな変数 \fInextchar\fP を更新する。
125 これらによって、
126 .BR getopt ()
127 は次回の呼び出しの際に、
128 以降のオプション文字や \fIargv\fP 要素のスキャンを継続できる。
129 .PP
130 オプション文字がそれ以上見つからなくなると、
131 .BR getopt ()
132
133 \-1 を返す。そして \fIoptind\fP は、\fIargv\fP の要素のうち、
134 オプションでない最初の要素を示すようになる。
135 .PP
136 .I optstring
137 は受け付けるオプション文字からなる文字列である。
138 文字のあとにコロン (:) が置かれている場合は、
139 オプションには引き数が必要であることを示す。
140 このとき
141 .BR getopt ()
142 は、現在注目している
143 \fIargv\fP 要素で、オプション文字に引き続くテキストへのポインタか、
144 あるいは次の \fIargv\fP 要素のテキストへのポインタを
145 .I optarg
146 に代入する。
147 2 個連続してコロンが置かれている場合は、
148 そのオプションは引き数をとってもとらなくてもよい。
149 現在の \fIargv\fP 要素にテキストがあれば
150 (つまり、"\-oarg" のように、オプション名自身と同じワード内に
151 テキストがある場合)、それが \fIoptarg\fP に返される。
152 なければ \fIoptarg\fP は 0 に設定される。
153 これは GNU による拡張である。
154 .I optstring
155
156 .B W
157 とそれに続くセミコロンが入っていると、
158 .B \-W foo
159 は長いオプション
160 .B \-\-foo
161 と同じように扱われる
162 (POSIX.2 は
163 .B \-W
164 オプションを実装依存の拡張として予約している)。
165 この動作は GNU による拡張であり、glibc 2 以前のライブラリでは
166 利用できない。
167 .PP
168 デフォルトでは
169 .BR getopt ()
170 は \fIargv\fP をスキャンする際に順序を変更し、
171 オプション以外の要素を最後に移動する。
172 他にも 2 つのモードが実装されている。
173 \fIoptstring\fP の先頭文字が \(aq+\(aq であるか、環境変数
174 .B POSIXLY_CORRECT
175 が設定されている場合には、オプションを対象とする動作は、
176 非オプションの引き数が現れた段階で終了する。
177 \fIoptstring\fP の先頭文字が \(aq\-\(aq である場合には、
178 オプションでない \fIargv\fP 要素は、
179 文字コード 1 のオプションであるかのように扱われる (これを用いるプログラムは、
180 オプションや \fIargv\fP 要素を任意の順序で受け入れ、かつそれらの順序が
181 意味を持つように書かれている必要がある)。
182 "\-\-" は特殊な引き数で、スキャンのモードによらず、
183 オプションのスキャンを強制的に終了させる。
184 .PP
185 認識できないオプション文字があると、
186 .BR getopt ()
187 はエラーメッセージを標準エラー出力 \fIstderr\fP に表示し、
188 その文字を \fIoptopt\fP に保存して \(aq?\(aq を返す。
189 呼び出したプログラムで \fIopterr\fP を 0 にしておけば、
190 エラーメッセージの表示を抑制できる。
191 .PP
192 .BR getopt ()
193 は \fIargv\fP の中に
194 \fIoptstring\fP にないオプション文字を見つけた場合、
195 またはオプション引き数が足りないことが分かった場合、
196 \&\(aq?\(aq を返して外部変数 \fIoptopt\fP をそのオプション文字に設定する。
197 \fIoptstring\fP の (上で説明したオプションで指定できる
198 \&\(aq+\(aq または \(aq\-\(aq 後に続く) 最初の文字が
199 コロン (\(aq:\(aq) のとき、
200 .BR getopt ()
201 はオプション引き数が足りない場合に \(aq?\(aq ではなく \(aq:\(aq を返す。
202 エラーを見つけた場合で、かつ \fIoptstring\fP の最初の文字がコロンでなく、
203 かつ外部変数 \fIopterr\fP が 0 でない場合 (これがデフォルト)、
204 .BR getopt ()
205 はエラーメッセージを表示する。
206 .SS getopt_long() と getopt_long_only()
207 .BR getopt_long ()
208 関数は、長いオプション (2 つのダッシュ "\-\-" で始まるオプション) を
209 受け入れることを除いて
210 .BR getopt ()
211 と同じように動作する
212 (プログラムに長いオプションだけが渡された場合、
213 .I optstring
214 は NULL ではなく空文字列 ("") となる)。
215 長いオプションの名前は、他と重ならない範囲において短縮できる。
216 あるいは定義されたオプションに正確にマッチするものでも (当然) かまわない。
217 長いオプションは引き数を取ることができ、
218 .B \-\-arg=param
219 または
220 .B "\-\-arg param"
221 と言う形式で指定する。
222 .PP
223 .I longopts
224
225 .I struct option
226 の要素からなる配列の、先頭要素へのポインタである。
227 .I struct option
228
229 .I <getopt.h>
230 で以下のように定義されている。
231 .in +4n
232 .nf
233 .sp
234 struct option {
235     const char *name;
236     int         has_arg;
237     int        *flag;
238     int         val;
239 };
240 .fi
241 .in
242 .PP
243 それぞれのフィールドの意味は以下の通り。
244 .TP
245 .I name
246 長いオプションの名前。
247 .TP
248 .I has_arg
249 \fBno_argument\fP (または 0) なら、オプションは引き数をとらない。
250 \fBrequired_argument\fP (または 1) なら、オプションは引き数を必要とする。
251 \fBoptional_argument\fP (または 2) なら、オプションは引き数をとっても
252 とらなくても良い。
253 .TP
254 .I flag
255 長いオプションに対する結果の返し方を指定する。\fIflag\fP が
256 NULL なら
257 .BR getopt_long ()
258 は \fIval\fP を返す
259 (例えば呼び出し元のプログラムは、
260 \fIval\fP に等価なオプション文字を代入することができる)。
261 NULL 以外の場合には、
262 .BR getopt_long ()
263 は 0 を返す。
264 このときオプションが見つかると \fIflag\fP がポイントする変数に
265 \fIval\fP が代入される。見つからないとこの変数は変更されない。
266 .TP
267 \fIval\fP
268 返り値、または \fIflag\fP がポイントする変数へロードされる値。
269 .PP
270 配列の最後の要素は、全て 0 で埋められていなければならない。
271 .PP
272 \fIlongindex\fP は、NULL でなければ、
273 長いオプションのインデックスを
274 .I longopts
275 からの相対位置として保持している変数へのポインタとなる。
276 .PP
277 .BR getopt_long_only ()
278
279 .BR getopt_long ()
280 と同様の動作をするが、 \(aq\-\(aq も "\-\-" と同様に、
281 長いオプションとして扱われる。\(aq\-\(aq で始まる
282 ("\-\-" 以外の) オプションが、長いものにはマッチしないが短いものに
283 マッチする場合においては、それは短いオプションとして解釈される。
284 .SH 返り値
285 オプションが正常に見つかれば
286 .BR getopt ()
287 はそのオプション文字を返す。
288 すべてのコマンドラインオプションの解析が終わったら、
289 .BR getopt ()
290 は \-1 を返す。
291 .I optstring
292 に含まれないオプション文字が見つかると、\(aq?\(aq を返す。
293 引き数が足りないオプションが見つかった場合、
294 返り値は
295 .I optstring
296 の最初の文字による異なる: 最初の文字が \(aq:\(aq であれば \(aq:\(aq を返し、
297 それ以外の場合は \(aq?\(aq を返す。
298 .PP
299 .BR getopt_long ()
300
301 .BR getopt_long_only ()
302 も、
303 短いオプション文字を認識した場合にはその文字を返す。
304 長いオプションに対しては、
305 \fIflag\fP が NULL なら \fIval\fP を返し、
306 \fIflag\fP が NULL 以外なら 0 を返す。
307 エラーと \-1 の返り値は
308 .BR getopt ()
309 と同じである。
310 さらに \(aq?\(aq は、マッチが確定できない場合や余分なパラメーターがある場合にも返る。
311 .SH 環境
312 .TP
313 .B POSIXLY_CORRECT
314 これが設定されていると、非オプションの引き数に到達した時点でオプション
315 に対する操作が停止される。
316 .TP
317 .B _<PID>_GNU_nonoption_argv_flags_
318 この変数は
319 .BR bash (1)
320 2.0 が glibc と通信するために用いられた。
321 どの引き数がワイルドカードを展開した結果で、
322 したがってオプションとみなすべきでないかを知らせるものである。
323 この機能は
324 .BR bash (1)
325 のバージョン 2.01 で削除されたが、glibc にはまだ残っている。
326 .SH 準拠
327 .TP
328 .BR getopt ():
329 環境変数
330 .B POSIXLY_CORRECT
331 が設定されている場合は POSIX.2 と POSIX.1-2001 に準拠する。
332 他の場合は \fIargv\fP の要素は本当の意味での定数にはならない。
333 なぜなら順序が変更されてしまうからである。
334 ただしそれらは、プロトタイプでは定数であるかのようにしてある。
335 これは他のシステムとの互換性のためである。
336
337 .I optstring
338 で \(aq+\(aq や \(aq\-\(aq を使うのは GNU による拡張である.
339
340 古い実装のいくつかでは、
341 .BR getopt ()
342
343 .I <stdio.h>
344 で宣言されていた。
345 SUSv1 では、
346 .I <unistd.h>
347
348 .I <stdio.h>
349 のどちらかで
350 宣言してもよかった。
351 POSIX.1-2001 では、
352 .B getopt
353 の宣言を
354 .I <stdio.h>
355 で行うのは「過去の名残」であるとされた。
356 POSIX.1-2001 では
357 .I <stdio.h>
358 で宣言を行うことを認めていない。
359 .TP
360 .BR getopt_long "(), " getopt_long_only ():
361 これらの関数は GNU による拡張である。
362 .SH 注意
363 複数の引き数ベクトルをスキャンしたり、同じ引き数ベクトルを二回以上
364 スキャンするようなプログラムで、
365 .I optstring
366 の先頭で \(aq+\(aq や \(aq\-\(aq といった GNU による拡張機能を使用したり、
367 引き数ベクトルの切り替え時に
368 .B POSIXLY_CORRECT
369 の値を変更したりする場合には、
370 .I optind
371 を伝統的な 1 ではなく 0 にリセットすることで
372 .BR getopt ()
373 を再初期化しなければならない
374 (0 にリセットすることで、
375 .B POSIXLY_CORRECT
376
377 .I optstring
378 の GNU 拡張機能のチェックを行う内部初期化ルーチンが起動される)。
379 .SH バグ
380 POSIX.2 における
381 .BR getopt ()
382 の仕様には技術的な問題があり、
383 その内容は POSIX.2 Interpretation 150 に記されている。
384 GNU による実装では (おそらく他のすべての実装でも)、
385 仕様と異なる正しい動作をするように実装されている。
386 .SH 例
387 以下に示す簡単なサンプルプログラムでは、
388 二種類のプログラムオプションを扱うのに
389 .BR getopt ()
390 を使用している。一つは値を伴わない
391 .I \-n
392 で、もう一つは対応する値が必要な
393 .I "\-t val"
394 である。
395 .nf
396 .sp
397 #include <unistd.h>
398 #include <stdlib.h>
399 #include <stdio.h>
400
401 int
402 main(int argc, char *argv[])
403 {
404     int flags, opt;
405     int nsecs, tfnd;
406
407     nsecs = 0;
408     tfnd = 0;
409     flags = 0;
410     while ((opt = getopt(argc, argv, "nt:")) != \-1) {
411         switch (opt) {
412         case \(aqn\(aq:
413             flags = 1;
414             break;
415         case \(aqt\(aq:
416             nsecs = atoi(optarg);
417             tfnd = 1;
418             break;
419         default: /* \(aq?\(aq */
420             fprintf(stderr, "Usage: %s [\-t nsecs] [\-n] name\\n",
421                     argv[0]);
422             exit(EXIT_FAILURE);
423         }
424     }
425
426     printf("flags=%d; tfnd=%d; optind=%d\\n", flags, tfnd, optind);
427
428     if (optind >= argc) {
429         fprintf(stderr, "Expected argument after options\\n");
430         exit(EXIT_FAILURE);
431     }
432
433     printf("name argument = %s\\n", argv[optind]);
434
435     /* Other code omitted */
436
437     exit(EXIT_SUCCESS);
438 }
439 .fi
440 .PP
441 以下は、
442 .BR getopt_long ()
443 の使用法を、ほぼすべての機能について示したプログラムの例である。
444 .nf
445 .sp
446 #include <stdio.h>     /* for printf */
447 #include <stdlib.h>    /* for exit */
448 #include <getopt.h>
449
450 int
451 main(int argc, char **argv) {
452     int c;
453     int digit_optind = 0;
454
455     while (1) {
456         int this_option_optind = optind ? optind : 1;
457         int option_index = 0;
458         static struct option long_options[] = {
459             {"add",     required_argument, 0,  0 },
460             {"append",  no_argument,       0,  0 },
461             {"delete",  required_argument, 0,  0 },
462             {"verbose", no_argument,       0,  0 },
463             {"create",  required_argument, 0, \(aqc\(aq},
464             {"file",    required_argument, 0,  0 },
465             {0,         0,                 0,  0 }
466         };
467
468         c = getopt_long(argc, argv, "abc:d:012",
469                  long_options, &option_index);
470         if (c == \-1)
471             break;
472
473         switch (c) {
474         case 0:
475             printf("option %s", long_options[option_index].name);
476             if (optarg)
477                 printf(" with arg %s", optarg);
478             printf("\\n");
479             break;
480
481         case \(aq0\(aq:
482         case \(aq1\(aq:
483         case \(aq2\(aq:
484             if (digit_optind != 0 && digit_optind != this_option_optind)
485               printf("digits occur in two different argv\-elements.\\n");
486             digit_optind = this_option_optind;
487             printf("option %c\\n", c);
488             break;
489
490         case \(aqa\(aq:
491             printf("option a\\n");
492             break;
493
494         case \(aqb\(aq:
495             printf("option b\\n");
496             break;
497
498         case \(aqc\(aq:
499             printf("option c with value \(aq%s\(aq\\n", optarg);
500             break;
501
502         case \(aqd\(aq:
503             printf("option d with value \(aq%s\(aq\\n", optarg);
504             break;
505
506         case \(aq?\(aq:
507             break;
508
509         default:
510             printf("?? getopt returned character code 0%o ??\\n", c);
511         }
512     }
513
514     if (optind < argc) {
515         printf("non-option ARGV\-elements: ");
516         while (optind < argc)
517             printf("%s ", argv[optind++]);
518         printf("\\n");
519     }
520
521     exit(EXIT_SUCCESS);
522 }
523 .fi
524 .SH 関連項目
525 .BR getsubopt (3)