1 .\" Copyright (c) 1993 by Thomas Koenig (ig25@rz.uni-karlsruhe.de)
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.
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.
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
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
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.
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
51 .\"WORD: argv-element argv 要素
53 .TH GETOPT 3 2010-11-01 "GNU" "Linux Programmer's Manual"
55 getopt, getopt_long, getopt_long_only,
56 optarg, optind, opterr, optopt \- コマンドラインオプションを解釈する
59 .B #include <unistd.h>
61 .BI "int getopt(int " argc ", char * const " argv[] ,
62 .BI " const char *" optstring );
64 .BI "extern char *" optarg ;
65 .BI "extern int " optind ", " opterr ", " optopt ;
67 .B #include <getopt.h>
69 .BI "int getopt_long(int " argc ", char * const " argv[] ,
70 .BI " const char *" optstring ,
71 .BI " const struct option *" longopts ", int *" longindex );
73 .BI "int getopt_long_only(int " argc ", char * const " argv[] ,
74 .BI " const char *" optstring ,
75 .BI " const struct option *" longopts ", int *" longindex );
80 .RB ( feature_test_macros (7)
86 _POSIX_C_SOURCE\ >=\ 2 || _XOPEN_SOURCE
89 .BR getopt_long_only ():
103 \fIargv\fP の要素のうち \(aq\-\(aq で始まるもの
104 (かつ "\-" 単独や "\-\-" 単独ではないもの) は
105 オプション要素 (option element) とみなされる。
106 この要素から先頭の \(aq\-\(aq を除いた文字は
107 オプション文字 (option character) とされる。
109 は、繰り返し呼び出されるごとに、次のオプション文字を返す。
115 の次に処理される要素のインデックスである。
116 システムによりこの変数の値は 1 に初期化される。
117 呼び出し側でこの値を 1 にリセットすることで、同じ
119 のスキャンをやり直したり、新しい引き数ベクトルをスキャンすることができる。
124 外部変数 \fIoptind\fP とスタティックな変数 \fInextchar\fP を更新する。
128 以降のオプション文字や \fIargv\fP 要素のスキャンを継続できる。
130 オプション文字がそれ以上見つからなくなると、
133 \-1 を返す。そして \fIoptind\fP は、\fIargv\fP の要素のうち、
134 オプションでない最初の要素を示すようになる。
137 は受け付けるオプション文字からなる文字列である。
138 文字のあとにコロン (:) が置かれている場合は、
139 オプションには引き数が必要であることを示す。
143 \fIargv\fP 要素で、オプション文字に引き続くテキストへのポインタか、
144 あるいは次の \fIargv\fP 要素のテキストへのポインタを
147 2 個連続してコロンが置かれている場合は、
148 そのオプションは引き数をとってもとらなくてもよい。
149 現在の \fIargv\fP 要素にテキストがあれば
150 (つまり、"\-oarg" のように、オプション名自身と同じワード内に
151 テキストがある場合)、それが \fIoptarg\fP に返される。
152 なければ \fIoptarg\fP は 0 に設定される。
164 オプションを実装依存の拡張として予約している)。
165 この動作は GNU による拡張であり、glibc 2 以前のライブラリでは
170 は \fIargv\fP をスキャンする際に順序を変更し、
173 \fIoptstring\fP の先頭文字が \(aq+\(aq であるか、環境変数
175 が設定されている場合には、オプションを対象とする動作は、
176 非オプションの引き数が現れた段階で終了する。
177 \fIoptstring\fP の先頭文字が \(aq\-\(aq である場合には、
178 オプションでない \fIargv\fP 要素は、
179 文字コード 1 のオプションであるかのように扱われる (これを用いるプログラムは、
180 オプションや \fIargv\fP 要素を任意の順序で受け入れ、かつそれらの順序が
181 意味を持つように書かれている必要がある)。
182 "\-\-" は特殊な引き数で、スキャンのモードによらず、
183 オプションのスキャンを強制的に終了させる。
187 はエラーメッセージを標準エラー出力 \fIstderr\fP に表示し、
188 その文字を \fIoptopt\fP に保存して \(aq?\(aq を返す。
189 呼び出したプログラムで \fIopterr\fP を 0 にしておけば、
194 \fIoptstring\fP にないオプション文字を見つけた場合、
195 またはオプション引き数が足りないことが分かった場合、
196 \&\(aq?\(aq を返して外部変数 \fIoptopt\fP をそのオプション文字に設定する。
197 \fIoptstring\fP の (上で説明したオプションで指定できる
198 \&\(aq+\(aq または \(aq\-\(aq 後に続く) 最初の文字が
201 はオプション引き数が足りない場合に \(aq?\(aq ではなく \(aq:\(aq を返す。
202 エラーを見つけた場合で、かつ \fIoptstring\fP の最初の文字がコロンでなく、
203 かつ外部変数 \fIopterr\fP が 0 でない場合 (これがデフォルト)、
206 .SS getopt_long() と getopt_long_only()
208 関数は、長いオプション (2 つのダッシュ "\-\-" で始まるオプション) を
212 (プログラムに長いオプションだけが渡された場合、
214 は NULL ではなく空文字列 ("") となる)。
215 長いオプションの名前は、他と重ならない範囲において短縮できる。
216 あるいは定義されたオプションに正確にマッチするものでも (当然) かまわない。
226 の要素からなる配列の、先頭要素へのポインタである。
249 \fBno_argument\fP (または 0) なら、オプションは引き数をとらない。
250 \fBrequired_argument\fP (または 1) なら、オプションは引き数を必要とする。
251 \fBoptional_argument\fP (または 2) なら、オプションは引き数をとっても
255 長いオプションに対する結果の返し方を指定する。\fIflag\fP が
260 \fIval\fP に等価なオプション文字を代入することができる)。
264 このときオプションが見つかると \fIflag\fP がポイントする変数に
265 \fIval\fP が代入される。見つからないとこの変数は変更されない。
268 返り値、または \fIflag\fP がポイントする変数へロードされる値。
270 配列の最後の要素は、全て 0 で埋められていなければならない。
272 \fIlongindex\fP は、NULL でなければ、
275 からの相対位置として保持している変数へのポインタとなる。
277 .BR getopt_long_only ()
280 と同様の動作をするが、 \(aq\-\(aq も "\-\-" と同様に、
281 長いオプションとして扱われる。\(aq\-\(aq で始まる
282 ("\-\-" 以外の) オプションが、長いものにはマッチしないが短いものに
283 マッチする場合においては、それは短いオプションとして解釈される。
288 すべてのコマンドラインオプションの解析が終わったら、
292 に含まれないオプション文字が見つかると、\(aq?\(aq を返す。
293 引き数が足りないオプションが見つかった場合、
296 の最初の文字による異なる: 最初の文字が \(aq:\(aq であれば \(aq:\(aq を返し、
297 それ以外の場合は \(aq?\(aq を返す。
301 .BR getopt_long_only ()
303 短いオプション文字を認識した場合にはその文字を返す。
305 \fIflag\fP が NULL なら \fIval\fP を返し、
306 \fIflag\fP が NULL 以外なら 0 を返す。
310 さらに \(aq?\(aq は、マッチが確定できない場合や余分なパラメーターがある場合にも返る。
314 これが設定されていると、非オプションの引き数に到達した時点でオプション
317 .B _<PID>_GNU_nonoption_argv_flags_
320 2.0 が glibc と通信するために用いられた。
321 どの引き数がワイルドカードを展開した結果で、
322 したがってオプションとみなすべきでないかを知らせるものである。
325 のバージョン 2.01 で削除されたが、glibc にはまだ残っている。
331 が設定されている場合は POSIX.2 と POSIX.1-2001 に準拠する。
332 他の場合は \fIargv\fP の要素は本当の意味での定数にはならない。
333 なぜなら順序が変更されてしまうからである。
334 ただしそれらは、プロトタイプでは定数であるかのようにしてある。
335 これは他のシステムとの互換性のためである。
338 で \(aq+\(aq や \(aq\-\(aq を使うのは GNU による拡張である.
360 .BR getopt_long "(), " getopt_long_only ():
361 これらの関数は GNU による拡張である。
363 複数の引き数ベクトルをスキャンしたり、同じ引き数ベクトルを二回以上
366 の先頭で \(aq+\(aq や \(aq\-\(aq といった GNU による拡張機能を使用したり、
371 を伝統的な 1 ではなく 0 にリセットすることで
378 の GNU 拡張機能のチェックを行う内部初期化ルーチンが起動される)。
383 その内容は POSIX.2 Interpretation 150 に記されている。
384 GNU による実装では (おそらく他のすべての実装でも)、
385 仕様と異なる正しい動作をするように実装されている。
402 main(int argc, char *argv[])
410 while ((opt = getopt(argc, argv, "nt:")) != \-1) {
416 nsecs = atoi(optarg);
419 default: /* \(aq?\(aq */
420 fprintf(stderr, "Usage: %s [\-t nsecs] [\-n] name\\n",
426 printf("flags=%d; tfnd=%d; optind=%d\\n", flags, tfnd, optind);
428 if (optind >= argc) {
429 fprintf(stderr, "Expected argument after options\\n");
433 printf("name argument = %s\\n", argv[optind]);
435 /* Other code omitted */
443 の使用法を、ほぼすべての機能について示したプログラムの例である。
446 #include <stdio.h> /* for printf */
447 #include <stdlib.h> /* for exit */
451 main(int argc, char **argv) {
453 int digit_optind = 0;
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 },
468 c = getopt_long(argc, argv, "abc:d:012",
469 long_options, &option_index);
475 printf("option %s", long_options[option_index].name);
477 printf(" with arg %s", optarg);
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);
491 printf("option a\\n");
495 printf("option b\\n");
499 printf("option c with value \(aq%s\(aq\\n", optarg);
503 printf("option d with value \(aq%s\(aq\\n", optarg);
510 printf("?? getopt returned character code 0%o ??\\n", c);
515 printf("non-option ARGV\-elements: ");
516 while (optind < argc)
517 printf("%s ", argv[optind++]);