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"
56 .\"O getopt, getopt_long, getopt_long_only,
57 .\"O optarg, optind, opterr, optopt \- Parse command-line options
58 getopt, getopt_long, getopt_long_only,
59 optarg, optind, opterr, optopt \- コマンドラインオプションを解釈する
63 .B #include <unistd.h>
65 .BI "int getopt(int " argc ", char * const " argv[] ,
66 .BI " const char *" optstring );
68 .BI "extern char *" optarg ;
69 .BI "extern int " optind ", " opterr ", " optopt ;
71 .B #include <getopt.h>
73 .BI "int getopt_long(int " argc ", char * const " argv[] ,
74 .BI " const char *" optstring ,
75 .BI " const struct option *" longopts ", int *" longindex );
77 .BI "int getopt_long_only(int " argc ", char * const " argv[] ,
78 .BI " const char *" optstring ,
79 .BI " const struct option *" longopts ", int *" longindex );
83 .\"O Feature Test Macro Requirements for glibc (see
84 .\"O .BR feature_test_macros (7)):
86 .RB ( feature_test_macros (7)
92 _POSIX_C_SOURCE\ >=\ 2 || _XOPEN_SOURCE
95 .BR getopt_long_only ():
102 .\"O function parses the command-line arguments.
107 .\"O are the argument count and array as passed to the
109 .\"O function on program invocation.
120 .\"O An element of \fIargv\fP that starts with \(aq\-\(aq
121 .\"O (and is not exactly "\-" or "\-\-")
122 .\"O is an option element.
123 .\"O The characters of this element
124 .\"O (aside from the initial \(aq\-\(aq) are option characters.
127 .\"O is called repeatedly, it returns successively each of the option characters
128 .\"O from each of the option elements.
129 \fIargv\fP の要素のうち \(aq\-\(aq で始まるもの
130 (かつ "\-" 単独や "\-\-" 単独ではないもの) は
131 オプション要素 (option element) とみなされる。
132 この要素から先頭の \(aq\-\(aq を除いた文字は
133 オプション文字 (option character) とされる。
135 は、繰り返し呼び出されるごとに、次のオプション文字を返す。
139 .\"O is the index of the next element to be processed in
141 .\"O The system initializes this value to 1.
142 .\"O The caller can reset it to 1 to restart scanning of the same
144 .\"O or when scanning a new argument vector.
149 の次に処理される要素のインデックスである。
150 システムによりこの変数の値は 1 に初期化される。
151 呼び出し側でこの値を 1 にリセットすることで、同じ
153 のスキャンをやり直したり、新しい引き数ベクトルをスキャンすることができる。
157 .\"O finds another option character, it returns that
158 .\"O character, updating the external variable \fIoptind\fP and a static
159 .\"O variable \fInextchar\fP so that the next call to
162 .\"O resume the scan with the following option character or
163 .\"O \fIargv\fP-element.
167 外部変数 \fIoptind\fP とスタティックな変数 \fInextchar\fP を更新する。
171 以降のオプション文字や \fIargv\fP 要素のスキャンを継続できる。
173 .\"O If there are no more option characters,
176 .\"O Then \fIoptind\fP is the index in \fIargv\fP of the first
177 .\"O \fIargv\fP-element that is not an option.
178 オプション文字がそれ以上見つからなくなると、
181 \-1 を返す。そして \fIoptind\fP は、\fIargv\fP の要素のうち、
182 オプションでない最初の要素を示すようになる。
185 .\"O is a string containing the legitimate option characters.
187 .\"O character is followed by a colon, the option requires an argument, so
189 .\"O places a pointer to the following text in the same
190 .\"O \fIargv\fP-element, or the text of the following \fIargv\fP-element, in
193 は受け付けるオプション文字からなる文字列である。
194 文字のあとにコロン (:) が置かれている場合は、
195 オプションには引き数が必要であることを示す。
199 \fIargv\fP 要素で、オプション文字に引き続くテキストへのポインタか、
200 あるいは次の \fIargv\fP 要素のテキストへのポインタを
203 .\"O Two colons mean an option takes
204 .\"O an optional arg; if there is text in the current \fIargv\fP-element
205 .\"O (i.e., in the same word as the option name itself, for example, "\-oarg"),
206 .\"O then it is returned in \fIoptarg\fP, otherwise \fIoptarg\fP is set to zero.
207 .\"O This is a GNU extension.
212 .\"O followed by a semicolon, then
214 .\"O is treated as the long option
216 2 個連続してコロンが置かれている場合は、
217 そのオプションは引き数をとってもとらなくてもよい。
218 現在の \fIargv\fP 要素にテキストがあれば
219 (つまり、"\-oarg" のように、オプション名自身と同じワード内に
220 テキストがある場合)、それが \fIoptarg\fP に返される。
221 なければ \fIoptarg\fP は 0 に設定される。
233 .\"O option is reserved by POSIX.2 for implementation extensions.)
234 .\"O This behavior is a GNU extension, not available with libraries before
238 オプションを実装依存の拡張として予約している)。
239 この動作は GNU による拡張であり、glibc 2 以前のライブラリでは
244 .\"O permutes the contents of \fIargv\fP as it
245 .\"O scans, so that eventually all the nonoptions are at the end.
246 .\"O Two other modes are also implemented.
247 .\"O If the first character of
248 .\"O \fIoptstring\fP is \(aq+\(aq or the environment variable
249 .\"O .B POSIXLY_CORRECT
250 .\"O is set, then option processing stops as soon as a nonoption argument is
252 .\"O If the first character of \fIoptstring\fP is \(aq\-\(aq, then
253 .\"O each nonoption \fIargv\fP-element is handled as if it were the argument of
254 .\"O an option with character code 1. (This is used by programs that were
255 .\"O written to expect options and other \fIargv\fP-elements in any order
256 .\"O and that care about the ordering of the two.)
257 .\"O The special argument "\-\-" forces an end of option-scanning regardless
258 .\"O of the scanning mode.
261 は \fIargv\fP をスキャンする際に順序を変更し、
264 \fIoptstring\fP の先頭文字が \(aq+\(aq であるか、環境変数
266 が設定されている場合には、オプションを対象とする動作は、
267 非オプションの引き数が現れた段階で終了する。
268 \fIoptstring\fP の先頭文字が \(aq\-\(aq である場合には、
269 オプションでない \fIargv\fP 要素は、
270 文字コード 1 のオプションであるかのように扱われる (これを用いるプログラムは、
271 オプションや \fIargv\fP 要素を任意の順序で受け入れ、かつそれらの順序が
272 意味を持つように書かれている必要がある)。
273 "\-\-" は特殊な引き数で、スキャンのモードによらず、
274 オプションのスキャンを強制的に終了させる。
278 .\"O does not recognize an option character, it prints an
279 .\"O error message to \fIstderr\fP, stores the character in \fIoptopt\fP, and
280 .\"O returns \(aq?\(aq.
281 .\"O The calling program may prevent the error message by
282 .\"O setting \fIopterr\fP to 0.
285 はエラーメッセージを標準エラー出力 \fIstderr\fP に表示し、
286 その文字を \fIoptopt\fP に保存して \(aq?\(aq を返す。
287 呼び出したプログラムで \fIopterr\fP を 0 にしておけば、
292 .\"O finds an option character in \fIargv\fP that was not
293 .\"O included in \fIoptstring\fP, or if it detects a missing option argument,
294 .\"O it returns \(aq?\(aq and sets the external variable \fIoptopt\fP to the
295 .\"O actual option character.
296 .\"O If the first character
297 .\"O (following any optional \(aq+\(aq or \(aq\-\(aq described above)
298 .\"O of \fIoptstring\fP
299 .\"O is a colon (\(aq:\(aq), then
301 .\"O returns \(aq:\(aq instead of \(aq?\(aq to
302 .\"O indicate a missing option argument.
303 .\"O If an error was detected, and
304 .\"O the first character of \fIoptstring\fP is not a colon, and
305 .\"O the external variable \fIopterr\fP is nonzero (which is the default),
307 .\"O prints an error message.
310 \fIoptstring\fP にないオプション文字を見つけた場合、
311 またはオプション引き数が足りないことが分かった場合、
312 \&\(aq?\(aq を返して外部変数 \fIoptopt\fP をそのオプション文字に設定する。
313 \fIoptstring\fP の (上で説明したオプションで指定できる
314 \&\(aq+\(aq または \(aq\-\(aq 後に続く) 最初の文字が
317 はオプション引き数が足りない場合に \(aq?\(aq ではなく \(aq:\(aq を返す。
318 エラーを見つけた場合で、かつ \fIoptstring\fP の最初の文字がコロンでなく、
319 かつ外部変数 \fIopterr\fP が 0 でない場合 (これがデフォルト)、
322 .\"O .SS getopt_long() and getopt_long_only()
323 .SS getopt_long() と getopt_long_only()
325 .\"O .BR getopt_long ()
326 .\"O function works like
328 .\"O except that it also accepts long options, started with two dashes.
329 .\"O (If the program accepts only long options, then
331 .\"O should be specified as an empty string (""), not NULL.)
332 .\"O Long option names may be abbreviated if the abbreviation is
333 .\"O unique or is an exact match for some defined option.
335 .\"O may take a parameter, of the form
336 .\"O .B \-\-arg=param
338 .\"O .BR "\-\-arg param" .
340 関数は、長いオプション (2 つのダッシュ "\-\-" で始まるオプション) を
344 (プログラムに長いオプションだけが渡された場合、
346 は NULL ではなく空文字列 ("") となる)。
347 長いオプションの名前は、他と重ならない範囲において短縮できる。
348 あるいは定義されたオプションに正確にマッチするものでも (当然) かまわない。
356 .\"O is a pointer to the first element of an array of
357 .\"O .I struct option
364 の要素からなる配列の、先頭要素へのポインタである。
381 .\"O The meanings of the different fields are:
385 .\"O is the name of the long option.
390 .\"O \fBno_argument\fP (or 0) if the option does not take an argument;
391 .\"O \fBrequired_argument\fP (or 1) if the option requires an argument; or
392 .\"O \fBoptional_argument\fP (or 2) if the option takes an optional argument.
393 \fBno_argument\fP (または 0) なら、オプションは引き数をとらない。
394 \fBrequired_argument\fP (または 1) なら、オプションは引き数を必要とする。
395 \fBoptional_argument\fP (または 2) なら、オプションは引き数をとっても
399 .\"O specifies how results are returned for a long option.
402 .\"O .BR getopt_long ()
403 .\"O returns \fIval\fP. (For
404 .\"O example, the calling program may set \fIval\fP to the equivalent short
405 .\"O option character.)
407 .\"O .BR getopt_long ()
409 .\"O \fIflag\fP points to a variable which is set to \fIval\fP if the
410 .\"O option is found, but left unchanged if the option is not found.
411 長いオプションに対する結果の返し方を指定する。\fIflag\fP が
416 \fIval\fP に等価なオプション文字を代入することができる)。
420 このときオプションが見つかると \fIflag\fP がポイントする変数に
421 \fIval\fP が代入される。見つからないとこの変数は変更されない。
424 .\"O is the value to return, or to load into the variable pointed
425 .\"O to by \fIflag\fP.
426 返り値、または \fIflag\fP がポイントする変数へロードされる値。
428 .\"O The last element of the array has to be filled with zeros.
429 配列の最後の要素は、全て 0 で埋められていなければならない。
431 .\"O If \fIlongindex\fP is not NULL, it
432 .\"O points to a variable which is set to the index of the long option relative to
434 \fIlongindex\fP は、NULL でなければ、
437 からの相対位置として保持している変数へのポインタとなる。
439 .\"O .BR getopt_long_only ()
441 .\"O .BR getopt_long (),
442 .\"O but \(aq\-\(aq as well
443 .\"O as "\-\-" can indicate a long option.
444 .\"O If an option that starts with \(aq\-\(aq
445 .\"O (not "\-\-") doesn't match a long option, but does match a short option,
446 .\"O it is parsed as a short option instead.
447 .BR getopt_long_only ()
450 と同様の動作をするが、 \(aq\-\(aq も "\-\-" と同様に、
451 長いオプションとして扱われる。\(aq\-\(aq で始まる
452 ("\-\-" 以外の) オプションが、長いものにはマッチしないが短いものに
453 マッチする場合においては、それは短いオプションとして解釈される。
454 .\"O .SH "RETURN VALUE"
456 .\"O If an option was successfully found, then
458 .\"O returns the option character.
459 .\"O If all command-line options have been parsed, then
464 .\"O encounters an option character that was not in
466 .\"O then \(aq?\(aq is returned.
469 .\"O encounters an option with a missing argument,
470 .\"O then the return value depends on the first character in
472 .\"O if it is \(aq:\(aq, then \(aq:\(aq is returned; otherwise \(aq?\(aq is returned.
476 すべてのコマンドラインオプションの解析が終わったら、
480 に含まれないオプション文字が見つかると、\(aq?\(aq を返す。
481 引き数が足りないオプションが見つかった場合、
484 の最初の文字による異なる: 最初の文字が \(aq:\(aq であれば \(aq:\(aq を返し、
485 それ以外の場合は \(aq?\(aq を返す。
487 .\"O .BR getopt_long ()
489 .\"O .BR getopt_long_only ()
490 .\"O also return the option
491 .\"O character when a short option is recognized.
492 .\"O For a long option, they
493 .\"O return \fIval\fP if \fIflag\fP is NULL, and 0 otherwise.
494 .\"O Error and \-1 returns are the same as for
496 .\"O plus \(aq?\(aq for an
497 .\"O ambiguous match or an extraneous parameter.
500 .BR getopt_long_only ()
502 短いオプション文字を認識した場合にはその文字を返す。
504 \fIflag\fP が NULL なら \fIval\fP を返し、
505 \fIflag\fP が NULL 以外なら 0 を返す。
509 さらに \(aq?\(aq は、マッチが確定できない場合や余分なパラメーターがある場合にも返る。
514 .\"O If this is set, then option processing stops as soon as a nonoption
515 .\"O argument is encountered.
516 これが設定されていると、非オプションの引き数に到達した時点でオプション
519 .B _<PID>_GNU_nonoption_argv_flags_
520 .\"O This variable was used by
522 .\"O 2.0 to communicate to glibc which arguments are the results of
523 .\"O wildcard expansion and so should not be considered as options.
524 .\"O This behavior was removed in
526 .\"O version 2.01, but the support remains in glibc.
529 2.0 が glibc と通信するために用いられた。
530 どの引き数がワイルドカードを展開した結果で、
531 したがってオプションとみなすべきでないかを知らせるものである。
534 のバージョン 2.01 で削除されたが、glibc にはまだ残っている。
535 .\"O .SH "CONFORMING TO"
539 .\"O POSIX.2 and POSIX.1-2001,
540 .\"O provided the environment variable
541 .\"O .B POSIXLY_CORRECT
543 .\"O Otherwise, the elements of \fIargv\fP aren't really const, because we
545 .\"O We pretend they're const in the prototype to be
546 .\"O compatible with other systems.
549 が設定されている場合は POSIX.2 と POSIX.1-2001 に準拠する。
550 他の場合は \fIargv\fP の要素は本当の意味での定数にはならない。
551 なぜなら順序が変更されてしまうからである。
552 ただしそれらは、プロトタイプでは定数であるかのようにしてある。
553 これは他のシステムとの互換性のためである。
555 .\"O The use of \(aq+\(aq and \(aq\-\(aq in
557 .\"O is a GNU extension.
559 で \(aq+\(aq や \(aq\-\(aq を使うのは GNU による拡張である.
561 .\"O On some older implementations,
565 .\"O SUSv1 permitted the declaration to appear in either
569 .\"O POSIX.1-2001 marked the use if
571 .\"O for this purpose as LEGACY.
572 .\"O POSIX.1-2001 does not allow the declaration to appear in
594 .\"O .BR getopt_long "() and " getopt_long_only ():
595 .\"O These functions are GNU extensions.
596 .BR getopt_long "(), " getopt_long_only ():
597 これらの関数は GNU による拡張である。
600 .\"O A program that scans multiple argument vectors,
601 .\"O or rescans the same vector more than once,
602 .\"O and wants to make use of GNU extensions such as \(aq+\(aq
603 .\"O and \(aq\-\(aq at the start of
605 .\"O or changes the value of
606 .\"O .B POSIXLY_CORRECT
608 .\"O must reinitialize
612 .\"O to 0, rather than the traditional value of 1.
613 .\"O (Resetting to 0 forces the invocation of an internal initialization
614 .\"O routine that rechecks
615 .\"O .B POSIXLY_CORRECT
616 .\"O and checks for GNU extensions in
617 .\"O .IR optstring .)
618 複数の引き数ベクトルをスキャンしたり、同じ引き数ベクトルを二回以上
621 の先頭で \(aq+\(aq や \(aq\-\(aq といった GNU による拡張機能を使用したり、
626 を伝統的な 1 ではなく 0 にリセットすることで
633 の GNU 拡張機能のチェックを行う内部初期化ルーチンが起動される)。
636 .\"O The POSIX.2 specification of
638 .\"O has a technical error described in POSIX.2 Interpretation 150.
640 .\"O implementation (and probably all other implementations) implements the
641 .\"O correct behavior rather than that specified.
645 その内容は POSIX.2 Interpretation 150 に記されている。
646 GNU による実装では (おそらく他のすべての実装でも)、
647 仕様と異なる正しい動作をするように実装されている。
650 .\"O The following trivial example program uses
652 .\"O to handle two program options:
654 .\"O with no associated value; and
656 .\"O which expects an associated value.
672 main(int argc, char *argv[])
680 while ((opt = getopt(argc, argv, "nt:")) != \-1) {
686 nsecs = atoi(optarg);
689 default: /* \(aq?\(aq */
690 fprintf(stderr, "Usage: %s [\-t nsecs] [\-n] name\\n",
696 printf("flags=%d; tfnd=%d; optind=%d\\n", flags, tfnd, optind);
698 if (optind >= argc) {
699 fprintf(stderr, "Expected argument after options\\n");
703 printf("name argument = %s\\n", argv[optind]);
705 /* Other code omitted */
711 .\"O The following example program illustrates the use of
712 .\"O .BR getopt_long ()
713 .\"O with most of its features.
716 の使用法を、ほぼすべての機能について示したプログラムの例である。
719 #include <stdio.h> /* for printf */
720 #include <stdlib.h> /* for exit */
724 main(int argc, char **argv) {
726 int digit_optind = 0;
729 int this_option_optind = optind ? optind : 1;
730 int option_index = 0;
731 static struct option long_options[] = {
732 {"add", required_argument, 0, 0 },
733 {"append", no_argument, 0, 0 },
734 {"delete", required_argument, 0, 0 },
735 {"verbose", no_argument, 0, 0 },
736 {"create", required_argument, 0, \(aqc\(aq},
737 {"file", required_argument, 0, 0 },
741 c = getopt_long(argc, argv, "abc:d:012",
742 long_options, &option_index);
748 printf("option %s", long_options[option_index].name);
750 printf(" with arg %s", optarg);
757 if (digit_optind != 0 && digit_optind != this_option_optind)
758 printf("digits occur in two different argv\-elements.\\n");
759 digit_optind = this_option_optind;
760 printf("option %c\\n", c);
764 printf("option a\\n");
768 printf("option b\\n");
772 printf("option c with value \(aq%s\(aq\\n", optarg);
776 printf("option d with value \(aq%s\(aq\\n", optarg);
783 printf("?? getopt returned character code 0%o ??\\n", c);
788 printf("non-option ARGV\-elements: ");
789 while (optind < argc)
790 printf("%s ", argv[optind++]);