OSDN Git Service

(split) LDP: Update the version to 3.53 in PO files
[linuxjm/LDP_man-pages.git] / release / man3 / getopt.3
1 .\" Copyright (c) 1993 by Thomas Koenig (ig25@rz.uni-karlsruhe.de)
2 .\"
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
7 .\"
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
12 .\"
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein.  The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" %%%LICENSE_END
24 .\"
25 .\" Modified Sat Jul 24 19:27:50 1993 by Rik Faith (faith@cs.unc.edu)
26 .\" Modified Mon Aug 30 22:02:34 1995 by Jim Van Zandt <jrv@vanzandt.mv.com>
27 .\"  longindex is a pointer, has_arg can take 3 values, using consistent
28 .\"  names for optstring and longindex, "\n" in formats fixed.  Documenting
29 .\"  opterr and getopt_long_only.  Clarified explanations (borrowing heavily
30 .\"  from the source code).
31 .\" Modified 8 May 1998 by Joseph S. Myers (jsm28@cam.ac.uk)
32 .\" Modified 990715, aeb: changed `EOF' into `-1' since that is what POSIX
33 .\"  says; moreover, EOF is not defined in <unistd.h>.
34 .\" Modified 2002-02-16, joey: added information about nonexistent
35 .\"  option character and colon as first option character
36 .\" Modified 2004-07-28, Michael Kerrisk <mtk.manpages@gmail.com>
37 .\"     Added text to explain how to order both '[-+]' and ':' at
38 .\"             the start of optstring
39 .\" Modified 2006-12-15, mtk, Added getopt() example program.
40 .\"
41 .\"*******************************************************************
42 .\"
43 .\" This file was generated with po4a. Translate the source file.
44 .\"
45 .\"*******************************************************************
46 .TH GETOPT 3 2010\-11\-01 GNU "Linux Programmer's Manual"
47 .SH 名前
48 getopt, getopt_long, getopt_long_only, optarg, optind, opterr, optopt \-
49 コマンドラインオプションを解釈する
50 .SH 書式
51 .nf
52 \fB#include <unistd.h>\fP
53 .sp
54 \fBint getopt(int \fP\fIargc\fP\fB, char * const \fP\fIargv[]\fP\fB,\fP
55 \fB           const char *\fP\fIoptstring\fP\fB);\fP
56 .sp
57 \fBextern char *\fP\fIoptarg\fP\fB;\fP
58 \fBextern int \fP\fIoptind\fP\fB, \fP\fIopterr\fP\fB, \fP\fIoptopt\fP\fB;\fP
59 .sp
60 \fB#include <getopt.h>\fP
61 .sp
62 \fBint getopt_long(int \fP\fIargc\fP\fB, char * const \fP\fIargv[]\fP\fB,\fP
63 \fB           const char *\fP\fIoptstring\fP\fB,\fP
64 \fB           const struct option *\fP\fIlongopts\fP\fB, int *\fP\fIlongindex\fP\fB);\fP
65 .sp
66 \fBint getopt_long_only(int \fP\fIargc\fP\fB, char * const \fP\fIargv[]\fP\fB,\fP
67 \fB           const char *\fP\fIoptstring\fP\fB,\fP
68 \fB           const struct option *\fP\fIlongopts\fP\fB, int *\fP\fIlongindex\fP\fB);\fP
69 .fi
70 .sp
71 .in -4n
72 glibc 向けの機能検査マクロの要件 (\fBfeature_test_macros\fP(7)  参照):
73 .ad l
74 .in
75 .sp
76 \fBgetopt\fP(): _POSIX_C_SOURCE\ >=\ 2 || _XOPEN_SOURCE
77 .br
78 \fBgetopt_long\fP(), \fBgetopt_long_only\fP(): _GNU_SOURCE
79 .ad b
80 .SH 説明
81 \fBgetopt\fP()  関数はコマンドライン引き数を解釈する。 \fBgetopt\fP()  がとる引き数 \fIargc\fP と \fIargv\fP
82 は、それぞれプログラムの起動時に \fImain\fP()  関数に渡された引き数の個数と配列である。 \fIargv\fP の要素のうち \(aq\-\(aq
83 で始まるもの (かつ "\-" 単独や "\-\-" 単独ではないもの) は オプション要素 (option element) とみなされる。
84 この要素から先頭の \(aq\-\(aq を除いた文字は オプション文字 (option character) とされる。 \fBgetopt\fP()
85 は、繰り返し呼び出されるごとに、次のオプション文字を返す。
86 .PP
87 変数 \fIoptind\fP は、 \fIargv\fP の次に処理される要素のインデックスである。 システムによりこの変数の値は 1 に初期化される。
88 呼び出し側でこの値を 1 にリセットすることで、同じ \fIargv\fP のスキャンをやり直したり、新しい引き数ベクトルをスキャンすることができる。
89 .PP
90 新たなオプション文字を見つけると、 \fBgetopt\fP()  はその文字を返し、 外部変数 \fIoptind\fP とスタティックな変数
91 \fInextchar\fP を更新する。 これらによって、 \fBgetopt\fP()  は次回の呼び出しの際に、 以降のオプション文字や \fIargv\fP
92 要素のスキャンを継続できる。
93 .PP
94 オプション文字がそれ以上見つからなくなると、 \fBgetopt\fP()  は \-1 を返す。そして \fIoptind\fP は、\fIargv\fP の要素のうち、
95 オプションでない最初の要素を示すようになる。
96 .PP
97 \fIoptstring\fP は受け付けるオプション文字からなる文字列である。 文字のあとにコロン (:) が置かれている場合は、
98 オプションには引き数が必要であることを示す。 このとき \fBgetopt\fP()  は、現在注目している \fIargv\fP
99 要素で、オプション文字に引き続くテキストへのポインタか、 あるいは次の \fIargv\fP 要素のテキストへのポインタを \fIoptarg\fP に代入する。
100 2 個連続してコロンが置かれている場合は、 そのオプションは引き数をとってもとらなくてもよい。 現在の \fIargv\fP 要素にテキストがあれば
101 (つまり、"\-oarg" のように、オプション名自身と同じワード内に テキストがある場合)、それが \fIoptarg\fP に返される。 なければ
102 \fIoptarg\fP は 0 に設定される。 これは GNU による拡張である。 \fIoptstring\fP に \fBW\fP
103 とそれに続くセミコロンが入っていると、 \fB\-W foo\fP は長いオプション \fB\-\-foo\fP と同じように扱われる (POSIX.2 は \fB\-W\fP
104 オプションを実装依存の拡張として予約している)。 この動作は GNU による拡張であり、glibc 2 以前のライブラリでは 利用できない。
105 .PP
106 デフォルトでは \fBgetopt\fP()  は \fIargv\fP をスキャンする際に順序を変更し、 オプション以外の要素を最後に移動する。 他にも 2
107 つのモードが実装されている。 \fIoptstring\fP の先頭文字が \(aq+\(aq であるか、環境変数 \fBPOSIXLY_CORRECT\fP
108 が設定されている場合には、オプションを対象とする動作は、 非オプションの引き数が現れた段階で終了する。 \fIoptstring\fP の先頭文字が
109 \(aq\-\(aq である場合には、 オプションでない \fIargv\fP 要素は、 文字コード 1 のオプションであるかのように扱われる
110 (これを用いるプログラムは、 オプションや \fIargv\fP 要素を任意の順序で受け入れ、かつそれらの順序が 意味を持つように書かれている必要がある)。
111 "\-\-" は特殊な引き数で、スキャンのモードによらず、 オプションのスキャンを強制的に終了させる。
112 .PP
113 認識できないオプション文字があると、 \fBgetopt\fP()  はエラーメッセージを標準エラー出力 \fIstderr\fP に表示し、 その文字を
114 \fIoptopt\fP に保存して \(aq?\(aq を返す。 呼び出したプログラムで \fIopterr\fP を 0 にしておけば、
115 エラーメッセージの表示を抑制できる。
116 .PP
117 \fBgetopt\fP()  は \fIargv\fP の中に \fIoptstring\fP にないオプション文字を見つけた場合、
118 またはオプション引き数が足りないことが分かった場合、 \&\(aq?\(aq を返して外部変数 \fIoptopt\fP をそのオプション文字に設定する。
119 \fIoptstring\fP の (上で説明したオプションで指定できる \&\(aq+\(aq または \(aq\-\(aq 後に続く) 最初の文字が コロン
120 (\(aq:\(aq) のとき、 \fBgetopt\fP()  はオプション引き数が足りない場合に \(aq?\(aq ではなく \(aq:\(aq
121 を返す。 エラーを見つけた場合で、かつ \fIoptstring\fP の最初の文字がコロンでなく、 かつ外部変数 \fIopterr\fP が 0 でない場合
122 (これがデフォルト)、 \fBgetopt\fP()  はエラーメッセージを表示する。
123 .SS "getopt_long() と getopt_long_only()"
124 \fBgetopt_long\fP()  関数は、長いオプション (2 つのダッシュ "\-\-" で始まるオプション) を 受け入れることを除いて
125 \fBgetopt\fP()  と同じように動作する (プログラムに長いオプションだけが渡された場合、 \fIoptstring\fP は NULL
126 ではなく空文字列 ("") となる)。 長いオプションの名前は、他と重ならない範囲において短縮できる。
127 あるいは定義されたオプションに正確にマッチするものでも (当然) かまわない。 長いオプションは引き数を取ることができ、 \fB\-\-arg=param\fP
128 または \fB\-\-arg param\fP と言う形式で指定する。
129 .PP
130 \fIlongopts\fP は \fIstruct option\fP の要素からなる配列の、先頭要素へのポインタである。 \fIstruct option\fP は
131 \fI<getopt.h>\fP で以下のように定義されている。
132 .in +4n
133 .nf
134 .sp
135 struct option {
136     const char *name;
137     int         has_arg;
138     int        *flag;
139     int         val;
140 };
141 .fi
142 .in
143 .PP
144 それぞれのフィールドの意味は以下の通り。
145 .TP 
146 \fIname\fP
147 長いオプションの名前。
148 .TP 
149 \fIhas_arg\fP
150 \fBno_argument\fP (または 0) なら、オプションは引き数をとらない。 \fBrequired_argument\fP (または 1)
151 なら、オプションは引き数を必要とする。 \fBoptional_argument\fP (または 2) なら、オプションは引き数をとっても とらなくても良い。
152 .TP 
153 \fIflag\fP
154 長いオプションに対する結果の返し方を指定する。\fIflag\fP が NULL なら \fBgetopt_long\fP()  は \fIval\fP を返す
155 (例えば呼び出し元のプログラムは、 \fIval\fP に等価なオプション文字を代入することができる)。 NULL 以外の場合には、
156 \fBgetopt_long\fP()  は 0 を返す。 このときオプションが見つかると \fIflag\fP がポイントする変数に \fIval\fP
157 が代入される。見つからないとこの変数は変更されない。
158 .TP 
159 \fIval\fP
160 返り値、または \fIflag\fP がポイントする変数へロードされる値。
161 .PP
162 配列の最後の要素は、全て 0 で埋められていなければならない。
163 .PP
164 \fIlongindex\fP は、NULL でなければ、 長いオプションのインデックスを \fIlongopts\fP
165 からの相対位置として保持している変数へのポインタとなる。
166 .PP
167 \fBgetopt_long_only\fP()  は \fBgetopt_long\fP()  と同様の動作をするが、 \(aq\-\(aq も "\-\-"
168 と同様に、 長いオプションとして扱われる。\(aq\-\(aq で始まる ("\-\-" 以外の) オプションが、長いものにはマッチしないが短いものに
169 マッチする場合においては、それは短いオプションとして解釈される。
170 .SH 返り値
171 オプションが正常に見つかれば \fBgetopt\fP()  はそのオプション文字を返す。 すべてのコマンドラインオプションの解析が終わったら、
172 \fBgetopt\fP()  は \-1 を返す。 \fIoptstring\fP に含まれないオプション文字が見つかると、\(aq?\(aq を返す。
173 引き数が足りないオプションが見つかった場合、 返り値は \fIoptstring\fP の最初の文字による異なる: 最初の文字が \(aq:\(aq であれば
174 \(aq:\(aq を返し、 それ以外の場合は \(aq?\(aq を返す。
175 .PP
176 \fBgetopt_long\fP()  と \fBgetopt_long_only\fP()  も、 短いオプション文字を認識した場合にはその文字を返す。
177 長いオプションに対しては、 \fIflag\fP が NULL なら \fIval\fP を返し、 \fIflag\fP が NULL 以外なら 0 を返す。 エラーと
178 \-1 の返り値は \fBgetopt\fP()  と同じである。 さらに \(aq?\(aq
179 は、マッチが確定できない場合や余分なパラメーターがある場合にも返る。
180 .SH 環境
181 .TP 
182 \fBPOSIXLY_CORRECT\fP
183 これが設定されていると、非オプションの引き数に到達した時点でオプション に対する操作が停止される。
184 .TP 
185 \fB_<PID>_GNU_nonoption_argv_flags_\fP
186 この変数は \fBbash\fP(1)  2.0 が glibc と通信するために用いられた。 どの引き数がワイルドカードを展開した結果で、
187 したがってオプションとみなすべきでないかを知らせるものである。 この機能は \fBbash\fP(1)  のバージョン 2.01 で削除されたが、glibc
188 にはまだ残っている。
189 .SH 準拠
190 .TP 
191 \fBgetopt\fP():
192 環境変数 \fBPOSIXLY_CORRECT\fP が設定されている場合は POSIX.2 と POSIX.1\-2001 に準拠する。 他の場合は
193 \fIargv\fP の要素は本当の意味での定数にはならない。 なぜなら順序が変更されてしまうからである。
194 ただしそれらは、プロトタイプでは定数であるかのようにしてある。 これは他のシステムとの互換性のためである。
195
196 \fIoptstring\fP で \(aq+\(aq や \(aq\-\(aq を使うのは GNU による拡張である.
197
198 古い実装のいくつかでは、 \fBgetopt\fP()  は \fI<stdio.h>\fP で宣言されていた。 SUSv1 では、
199 \fI<unistd.h>\fP か \fI<stdio.h>\fP のどちらかで 宣言してもよかった。 POSIX.1\-2001
200 では、 \fBgetopt\fP の宣言を \fI<stdio.h>\fP で行うのは「過去の名残」であるとされた。 POSIX.1\-2001 では
201 \fI<stdio.h>\fP で宣言を行うことを認めていない。
202 .TP 
203 \fBgetopt_long\fP(), \fBgetopt_long_only\fP():
204 これらの関数は GNU による拡張である。
205 .SH 注意
206 複数の引き数ベクトルをスキャンしたり、同じ引き数ベクトルを二回以上 スキャンするようなプログラムで、 \fIoptstring\fP の先頭で
207 \(aq+\(aq や \(aq\-\(aq といった GNU による拡張機能を使用したり、 引き数ベクトルの切り替え時に
208 \fBPOSIXLY_CORRECT\fP の値を変更したりする場合には、 \fIoptind\fP を伝統的な 1 ではなく 0 にリセットすることで
209 \fBgetopt\fP()  を再初期化しなければならない (0 にリセットすることで、 \fBPOSIXLY_CORRECT\fP や \fIoptstring\fP
210 の GNU 拡張機能のチェックを行う内部初期化ルーチンが起動される)。
211 .SH バグ
212 POSIX.2 における \fBgetopt\fP()  の仕様には技術的な問題があり、 その内容は POSIX.2 Interpretation 150
213 に記されている。 GNU による実装では (おそらく他のすべての実装でも)、 仕様と異なる正しい動作をするように実装されている。
214 .SH 例
215 以下に示す簡単なサンプルプログラムでは、 二種類のプログラムオプションを扱うのに \fBgetopt\fP()  を使用している。一つは値を伴わない
216 \fI\-n\fP で、もう一つは対応する値が必要な \fI\-t val\fP である。
217 .nf
218 .sp
219 #include <unistd.h>
220 #include <stdlib.h>
221 #include <stdio.h>
222
223 int
224 main(int argc, char *argv[])
225 {
226     int flags, opt;
227     int nsecs, tfnd;
228
229     nsecs = 0;
230     tfnd = 0;
231     flags = 0;
232     while ((opt = getopt(argc, argv, "nt:")) != \-1) {
233         switch (opt) {
234         case \(aqn\(aq:
235             flags = 1;
236             break;
237         case \(aqt\(aq:
238             nsecs = atoi(optarg);
239             tfnd = 1;
240             break;
241         default: /* \(aq?\(aq */
242             fprintf(stderr, "Usage: %s [\-t nsecs] [\-n] name\en",
243                     argv[0]);
244             exit(EXIT_FAILURE);
245         }
246     }
247
248     printf("flags=%d; tfnd=%d; optind=%d\en", flags, tfnd, optind);
249
250     if (optind >= argc) {
251         fprintf(stderr, "Expected argument after options\en");
252         exit(EXIT_FAILURE);
253     }
254
255     printf("name argument = %s\en", argv[optind]);
256
257     /* Other code omitted */
258
259     exit(EXIT_SUCCESS);
260 }
261 .fi
262 .PP
263 以下は、 \fBgetopt_long\fP()  の使用法を、ほぼすべての機能について示したプログラムの例である。
264 .nf
265 .sp
266 #include <stdio.h>     /* for printf */
267 #include <stdlib.h>    /* for exit */
268 #include <getopt.h>
269
270 int
271 main(int argc, char **argv) {
272     int c;
273     int digit_optind = 0;
274
275     while (1) {
276         int this_option_optind = optind ? optind : 1;
277         int option_index = 0;
278         static struct option long_options[] = {
279             {"add",     required_argument, 0,  0 },
280             {"append",  no_argument,       0,  0 },
281             {"delete",  required_argument, 0,  0 },
282             {"verbose", no_argument,       0,  0 },
283             {"create",  required_argument, 0, \(aqc\(aq},
284             {"file",    required_argument, 0,  0 },
285             {0,         0,                 0,  0 }
286         };
287
288         c = getopt_long(argc, argv, "abc:d:012",
289                  long_options, &option_index);
290         if (c == \-1)
291             break;
292
293         switch (c) {
294         case 0:
295             printf("option %s", long_options[option_index].name);
296             if (optarg)
297                 printf(" with arg %s", optarg);
298             printf("\en");
299             break;
300
301         case \(aq0\(aq:
302         case \(aq1\(aq:
303         case \(aq2\(aq:
304             if (digit_optind != 0 && digit_optind != this_option_optind)
305               printf("digits occur in two different argv\-elements.\en");
306             digit_optind = this_option_optind;
307             printf("option %c\en", c);
308             break;
309
310         case \(aqa\(aq:
311             printf("option a\en");
312             break;
313
314         case \(aqb\(aq:
315             printf("option b\en");
316             break;
317
318         case \(aqc\(aq:
319             printf("option c with value \(aq%s\(aq\en", optarg);
320             break;
321
322         case \(aqd\(aq:
323             printf("option d with value \(aq%s\(aq\en", optarg);
324             break;
325
326         case \(aq?\(aq:
327             break;
328
329         default:
330             printf("?? getopt returned character code 0%o ??\en", c);
331         }
332     }
333
334     if (optind < argc) {
335         printf("non\-option ARGV\-elements: ");
336         while (optind < argc)
337             printf("%s ", argv[optind++]);
338         printf("\en");
339     }
340
341     exit(EXIT_SUCCESS);
342 }
343 .fi
344 .SH 関連項目
345 \fBgetsubopt\fP(3)
346 .SH この文書について
347 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.53 の一部
348 である。プロジェクトの説明とバグ報告に関する情報は
349 http://www.kernel.org/doc/man\-pages/ に書かれている。