.\" Copyright (c) 1993 by Thomas Koenig (ig25@rz.uni-karlsruhe.de) .\" .\" Permission is granted to make and distribute verbatim copies of this .\" manual provided the copyright notice and this permission notice are .\" preserved on all copies. .\" .\" Permission is granted to copy and distribute modified versions of this .\" manual under the conditions for verbatim copying, provided that the .\" entire resulting derived work is distributed under the terms of a .\" permission notice identical to this one. .\" .\" Since the Linux kernel and libraries are constantly changing, this .\" manual page may be incorrect or out-of-date. The author(s) assume no .\" responsibility for errors or omissions, or for damages resulting from .\" the use of the information contained herein. The author(s) may not .\" have taken the same level of care in the production of this manual, .\" which is licensed free of charge, as they might when working .\" professionally. .\" .\" Formatted or processed versions of this manual, if unaccompanied by .\" the source, must acknowledge the copyright and authors of this work. .\" License. .\" Modified Sat Jul 24 19:27:50 1993 by Rik Faith (faith@cs.unc.edu) .\" Modified Mon Aug 30 22:02:34 1995 by Jim Van Zandt .\" longindex is a pointer, has_arg can take 3 values, using consistent .\" names for optstring and longindex, "\n" in formats fixed. Documenting .\" opterr and getopt_long_only. Clarified explanations (borrowing heavily .\" from the source code). .\" Modified 8 May 1998 by Joseph S. Myers (jsm28@cam.ac.uk) .\" Modified 990715, aeb: changed `EOF' into `-1' since that is what POSIX .\" says; moreover, EOF is not defined in . .\" Modified 2002-02-16, joey: added information about nonexisting .\" option character and colon as first option character .\" Modified 2004-07-28, Michael Kerrisk .\" Added text to explain how to order both '[-+]' and ':' at .\" the start of optstring .\" Modified 2006-12-15, mtk, Added getopt() example program. .\" .\" Japanese Version Copyright (c) 1998 NAKANO Takeo all rights reserved. .\" Translated 1998-08-12, NAKANO Takeo .\" Updated & Modified 1999-08-21, NAKANO Takeo .\" Updated & Modified 2001-07-01, Yuichi SATO .\" Updated & Modified 2002-03-23, Yuichi SATO .\" Updated & Modified 2003-09-07, Yuichi SATO .\" Updated & Modified 2005-01-10, Yuichi SATO .\" Updated & Modified 2005-10-28, Akihiro MOTOKI .\" Updated & Modified 2006-01-18, Akihiro MOTOKI .\" Updated & Modified 2007-05-01, Akihiro MOTOKI, LDP v2.46 .\" Updated 2008-08-11, Akihiro MOTOKI, LDP v3.05 .\" .\"WORD: argv-element argv 要素 .\" .TH GETOPT 3 2010-11-01 "GNU" "Linux Programmer's Manual" .\"O .SH NAME .SH 名前 .\"O getopt, getopt_long, getopt_long_only, .\"O optarg, optind, opterr, optopt \- Parse command-line options getopt, getopt_long, getopt_long_only, optarg, optind, opterr, optopt \- コマンドラインオプションを解釈する .\"O .SH SYNOPSIS .SH 書式 .nf .B #include .sp .BI "int getopt(int " argc ", char * const " argv[] , .BI " const char *" optstring ); .sp .BI "extern char *" optarg ; .BI "extern int " optind ", " opterr ", " optopt ; .sp .B #include .sp .BI "int getopt_long(int " argc ", char * const " argv[] , .BI " const char *" optstring , .BI " const struct option *" longopts ", int *" longindex ); .sp .BI "int getopt_long_only(int " argc ", char * const " argv[] , .BI " const char *" optstring , .BI " const struct option *" longopts ", int *" longindex ); .fi .sp .in -4n .\"O Feature Test Macro Requirements for glibc (see .\"O .BR feature_test_macros (7)): glibc 向けの機能検査マクロの要件 .RB ( feature_test_macros (7) 参照): .ad l .in .sp .BR getopt (): _POSIX_C_SOURCE\ >=\ 2 || _XOPEN_SOURCE .br .BR getopt_long (), .BR getopt_long_only (): _GNU_SOURCE .ad b .\"O .SH DESCRIPTION .SH 説明 .\"O The .\"O .BR getopt () .\"O function parses the command-line arguments. .\"O Its arguments .\"O .I argc .\"O and .\"O .I argv .\"O are the argument count and array as passed to the .\"O .IR main () .\"O function on program invocation. .BR getopt () 関数はコマンドライン引き数を解釈する。 .BR getopt () がとる引き数 .I argc と .I argv は、それぞれプログラムの起動時に .IR main () 関数に渡された引き数の個数と配列である。 .\"O An element of \fIargv\fP that starts with \(aq\-\(aq .\"O (and is not exactly "\-" or "\-\-") .\"O is an option element. .\"O The characters of this element .\"O (aside from the initial \(aq\-\(aq) are option characters. .\"O If .\"O .BR getopt () .\"O is called repeatedly, it returns successively each of the option characters .\"O from each of the option elements. \fIargv\fP の要素のうち \(aq\-\(aq で始まるもの (かつ "\-" 単独や "\-\-" 単独ではないもの) は オプション要素 (option element) とみなされる。 この要素から先頭の \(aq\-\(aq を除いた文字は オプション文字 (option character) とされる。 .BR getopt () は、繰り返し呼び出されるごとに、次のオプション文字を返す。 .PP .\"O The variable .\"O .I optind .\"O is the index of the next element to be processed in .\"O .IR argv . .\"O The system initializes this value to 1. .\"O The caller can reset it to 1 to restart scanning of the same .\"O .IR argv , .\"O or when scanning a new argument vector. 変数 .I optind は、 .I argv の次に処理される要素のインデックスである。 システムによりこの変数の値は 1 に初期化される。 呼び出し側でこの値を 1 にリセットすることで、同じ .I argv のスキャンをやり直したり、新しい引き数ベクトルをスキャンすることができる。 .PP .\"O If .\"O .BR getopt () .\"O finds another option character, it returns that .\"O character, updating the external variable \fIoptind\fP and a static .\"O variable \fInextchar\fP so that the next call to .\"O .BR getopt () .\"O can .\"O resume the scan with the following option character or .\"O \fIargv\fP-element. 新たなオプション文字を見つけると、 .BR getopt () はその文字を返し、 外部変数 \fIoptind\fP とスタティックな変数 \fInextchar\fP を更新する。 これらによって、 .BR getopt () は次回の呼び出しの際に、 以降のオプション文字や \fIargv\fP 要素のスキャンを継続できる。 .PP .\"O If there are no more option characters, .\"O .BR getopt () .\"O returns \-1. .\"O Then \fIoptind\fP is the index in \fIargv\fP of the first .\"O \fIargv\fP-element that is not an option. オプション文字がそれ以上見つからなくなると、 .BR getopt () は \-1 を返す。そして \fIoptind\fP は、\fIargv\fP の要素のうち、 オプションでない最初の要素を示すようになる。 .PP .\"O .I optstring .\"O is a string containing the legitimate option characters. .\"O If such a .\"O character is followed by a colon, the option requires an argument, so .\"O .BR getopt () .\"O places a pointer to the following text in the same .\"O \fIargv\fP-element, or the text of the following \fIargv\fP-element, in .\"O .IR optarg . .I optstring は受け付けるオプション文字からなる文字列である。 文字のあとにコロン (:) が置かれている場合は、 オプションには引き数が必要であることを示す。 このとき .BR getopt () は、現在注目している \fIargv\fP 要素で、オプション文字に引き続くテキストへのポインタか、 あるいは次の \fIargv\fP 要素のテキストへのポインタを .I optarg に代入する。 .\"O Two colons mean an option takes .\"O an optional arg; if there is text in the current \fIargv\fP-element .\"O (i.e., in the same word as the option name itself, for example, "\-oarg"), .\"O then it is returned in \fIoptarg\fP, otherwise \fIoptarg\fP is set to zero. .\"O This is a GNU extension. .\"O If .\"O .I optstring .\"O contains .\"O .B W .\"O followed by a semicolon, then .\"O .B \-W foo .\"O is treated as the long option .\"O .BR \-\-foo . 2 個連続してコロンが置かれている場合は、 そのオプションは引き数をとってもとらなくてもよい。 現在の \fIargv\fP 要素にテキストがあれば (つまり、"\-oarg" のように、オプション名自身と同じワード内に テキストがある場合)、それが \fIoptarg\fP に返される。 なければ \fIoptarg\fP は 0 に設定される。 これは GNU による拡張である。 .I optstring に .B W とそれに続くセミコロンが入っていると、 .B \-W foo は長いオプション .B \-\-foo と同じように扱われる .\"O (The .\"O .B \-W .\"O option is reserved by POSIX.2 for implementation extensions.) .\"O This behavior is a GNU extension, not available with libraries before .\"O glibc 2. (POSIX.2 は .B \-W オプションを実装依存の拡張として予約している)。 この動作は GNU による拡張であり、glibc 2 以前のライブラリでは 利用できない。 .PP .\"O By default, .\"O .BR getopt () .\"O permutes the contents of \fIargv\fP as it .\"O scans, so that eventually all the nonoptions are at the end. .\"O Two other modes are also implemented. .\"O If the first character of .\"O \fIoptstring\fP is \(aq+\(aq or the environment variable .\"O .B POSIXLY_CORRECT .\"O is set, then option processing stops as soon as a nonoption argument is .\"O encountered. .\"O If the first character of \fIoptstring\fP is \(aq\-\(aq, then .\"O each nonoption \fIargv\fP-element is handled as if it were the argument of .\"O an option with character code 1. (This is used by programs that were .\"O written to expect options and other \fIargv\fP-elements in any order .\"O and that care about the ordering of the two.) .\"O The special argument "\-\-" forces an end of option-scanning regardless .\"O of the scanning mode. デフォルトでは .BR getopt () は \fIargv\fP をスキャンする際に順序を変更し、 オプション以外の要素を最後に移動する。 他にも 2 つのモードが実装されている。 \fIoptstring\fP の先頭文字が \(aq+\(aq であるか、環境変数 .B POSIXLY_CORRECT が設定されている場合には、オプションを対象とする動作は、 非オプションの引き数が現れた段階で終了する。 \fIoptstring\fP の先頭文字が \(aq\-\(aq である場合には、 オプションでない \fIargv\fP 要素は、 文字コード 1 のオプションであるかのように扱われる (これを用いるプログラムは、 オプションや \fIargv\fP 要素を任意の順序で受け入れ、かつそれらの順序が 意味を持つように書かれている必要がある)。 "\-\-" は特殊な引き数で、スキャンのモードによらず、 オプションのスキャンを強制的に終了させる。 .PP .\"O If .\"O .BR getopt () .\"O does not recognize an option character, it prints an .\"O error message to \fIstderr\fP, stores the character in \fIoptopt\fP, and .\"O returns \(aq?\(aq. .\"O The calling program may prevent the error message by .\"O setting \fIopterr\fP to 0. 認識できないオプション文字があると、 .BR getopt () はエラーメッセージを標準エラー出力 \fIstderr\fP に表示し、 その文字を \fIoptopt\fP に保存して \(aq?\(aq を返す。 呼び出したプログラムで \fIopterr\fP を 0 にしておけば、 エラーメッセージの表示を抑制できる。 .PP .\"O If .\"O .BR getopt () .\"O finds an option character in \fIargv\fP that was not .\"O included in \fIoptstring\fP, or if it detects a missing option argument, .\"O it returns \(aq?\(aq and sets the external variable \fIoptopt\fP to the .\"O actual option character. .\"O If the first character .\"O (following any optional \(aq+\(aq or \(aq\-\(aq described above) .\"O of \fIoptstring\fP .\"O is a colon (\(aq:\(aq), then .\"O .BR getopt () .\"O returns \(aq:\(aq instead of \(aq?\(aq to .\"O indicate a missing option argument. .\"O If an error was detected, and .\"O the first character of \fIoptstring\fP is not a colon, and .\"O the external variable \fIopterr\fP is nonzero (which is the default), .\"O .BR getopt () .\"O prints an error message. .BR getopt () は \fIargv\fP の中に \fIoptstring\fP にないオプション文字を見つけた場合、 またはオプション引き数が足りないことが分かった場合、 \&\(aq?\(aq を返して外部変数 \fIoptopt\fP をそのオプション文字に設定する。 \fIoptstring\fP の (上で説明したオプションで指定できる \&\(aq+\(aq または \(aq\-\(aq 後に続く) 最初の文字が コロン (\(aq:\(aq) のとき、 .BR getopt () はオプション引き数が足りない場合に \(aq?\(aq ではなく \(aq:\(aq を返す。 エラーを見つけた場合で、かつ \fIoptstring\fP の最初の文字がコロンでなく、 かつ外部変数 \fIopterr\fP が 0 でない場合 (これがデフォルト)、 .BR getopt () はエラーメッセージを表示する。 .\"O .SS getopt_long() and getopt_long_only() .SS getopt_long() と getopt_long_only() .\"O The .\"O .BR getopt_long () .\"O function works like .\"O .BR getopt () .\"O except that it also accepts long options, started with two dashes. .\"O (If the program accepts only long options, then .\"O .I optstring .\"O should be specified as an empty string (""), not NULL.) .\"O Long option names may be abbreviated if the abbreviation is .\"O unique or is an exact match for some defined option. .\"O A long option .\"O may take a parameter, of the form .\"O .B \-\-arg=param .\"O or .\"O .BR "\-\-arg param" . .BR getopt_long () 関数は、長いオプション (2 つのダッシュ "\-\-" で始まるオプション) を 受け入れることを除いて .BR getopt () と同じように動作する (プログラムに長いオプションだけが渡された場合、 .I optstring は NULL ではなく空文字列 ("") となる)。 長いオプションの名前は、他と重ならない範囲において短縮できる。 あるいは定義されたオプションに正確にマッチするものでも (当然) かまわない。 長いオプションは引き数を取ることができ、 .B \-\-arg=param または .B "\-\-arg param" と言う形式で指定する。 .PP .\"O .I longopts .\"O is a pointer to the first element of an array of .\"O .I struct option .\"O declared in .\"O .I .\"O as .I longopts は .I struct option の要素からなる配列の、先頭要素へのポインタである。 .I struct option は .I で以下のように定義されている。 .in +4n .nf .sp struct option { const char *name; int has_arg; int *flag; int val; }; .fi .in .PP .\"O The meanings of the different fields are: それぞれのフィールドの意味は以下の通り。 .TP .I name .\"O is the name of the long option. 長いオプションの名前。 .TP .I has_arg .\"O is: .\"O \fBno_argument\fP (or 0) if the option does not take an argument; .\"O \fBrequired_argument\fP (or 1) if the option requires an argument; or .\"O \fBoptional_argument\fP (or 2) if the option takes an optional argument. \fBno_argument\fP (または 0) なら、オプションは引き数をとらない。 \fBrequired_argument\fP (または 1) なら、オプションは引き数を必要とする。 \fBoptional_argument\fP (または 2) なら、オプションは引き数をとっても とらなくても良い。 .TP .I flag .\"O specifies how results are returned for a long option. .\"O If \fIflag\fP .\"O is NULL, then .\"O .BR getopt_long () .\"O returns \fIval\fP. (For .\"O example, the calling program may set \fIval\fP to the equivalent short .\"O option character.) .\"O Otherwise, .\"O .BR getopt_long () .\"O returns 0, and .\"O \fIflag\fP points to a variable which is set to \fIval\fP if the .\"O option is found, but left unchanged if the option is not found. 長いオプションに対する結果の返し方を指定する。\fIflag\fP が NULL なら .BR getopt_long () は \fIval\fP を返す (例えば呼び出し元のプログラムは、 \fIval\fP に等価なオプション文字を代入することができる)。 NULL 以外の場合には、 .BR getopt_long () は 0 を返す。 このときオプションが見つかると \fIflag\fP がポイントする変数に \fIval\fP が代入される。見つからないとこの変数は変更されない。 .TP \fIval\fP .\"O is the value to return, or to load into the variable pointed .\"O to by \fIflag\fP. 返り値、または \fIflag\fP がポイントする変数へロードされる値。 .PP .\"O The last element of the array has to be filled with zeros. 配列の最後の要素は、全て 0 で埋められていなければならない。 .PP .\"O If \fIlongindex\fP is not NULL, it .\"O points to a variable which is set to the index of the long option relative to .\"O .IR longopts . \fIlongindex\fP は、NULL でなければ、 長いオプションのインデックスを .I longopts からの相対位置として保持している変数へのポインタとなる。 .PP .\"O .BR getopt_long_only () .\"O is like .\"O .BR getopt_long (), .\"O but \(aq\-\(aq as well .\"O as "\-\-" can indicate a long option. .\"O If an option that starts with \(aq\-\(aq .\"O (not "\-\-") doesn't match a long option, but does match a short option, .\"O it is parsed as a short option instead. .BR getopt_long_only () は .BR getopt_long () と同様の動作をするが、 \(aq\-\(aq も "\-\-" と同様に、 長いオプションとして扱われる。\(aq\-\(aq で始まる ("\-\-" 以外の) オプションが、長いものにはマッチしないが短いものに マッチする場合においては、それは短いオプションとして解釈される。 .\"O .SH "RETURN VALUE" .SH 返り値 .\"O If an option was successfully found, then .\"O .BR getopt () .\"O returns the option character. .\"O If all command-line options have been parsed, then .\"O .BR getopt () .\"O returns \-1. .\"O If .\"O .BR getopt () .\"O encounters an option character that was not in .\"O .IR optstring , .\"O then \(aq?\(aq is returned. .\"O If .\"O .BR getopt () .\"O encounters an option with a missing argument, .\"O then the return value depends on the first character in .\"O .IR optstring : .\"O if it is \(aq:\(aq, then \(aq:\(aq is returned; otherwise \(aq?\(aq is returned. オプションが正常に見つかれば .BR getopt () はそのオプション文字を返す。 すべてのコマンドラインオプションの解析が終わったら、 .BR getopt () は \-1 を返す。 .I optstring に含まれないオプション文字が見つかると、\(aq?\(aq を返す。 引き数が足りないオプションが見つかった場合、 返り値は .I optstring の最初の文字による異なる: 最初の文字が \(aq:\(aq であれば \(aq:\(aq を返し、 それ以外の場合は \(aq?\(aq を返す。 .PP .\"O .BR getopt_long () .\"O and .\"O .BR getopt_long_only () .\"O also return the option .\"O character when a short option is recognized. .\"O For a long option, they .\"O return \fIval\fP if \fIflag\fP is NULL, and 0 otherwise. .\"O Error and \-1 returns are the same as for .\"O .BR getopt (), .\"O plus \(aq?\(aq for an .\"O ambiguous match or an extraneous parameter. .BR getopt_long () と .BR getopt_long_only () も、 短いオプション文字を認識した場合にはその文字を返す。 長いオプションに対しては、 \fIflag\fP が NULL なら \fIval\fP を返し、 \fIflag\fP が NULL 以外なら 0 を返す。 エラーと \-1 の返り値は .BR getopt () と同じである。 さらに \(aq?\(aq は、マッチが確定できない場合や余分なパラメーターがある場合にも返る。 .\"O .SH ENVIRONMENT .SH 環境 .TP .B POSIXLY_CORRECT .\"O If this is set, then option processing stops as soon as a nonoption .\"O argument is encountered. これが設定されていると、非オプションの引き数に到達した時点でオプション に対する操作が停止される。 .TP .B __GNU_nonoption_argv_flags_ .\"O This variable was used by .\"O .BR bash (1) .\"O 2.0 to communicate to glibc which arguments are the results of .\"O wildcard expansion and so should not be considered as options. .\"O This behavior was removed in .\"O .BR bash (1) .\"O version 2.01, but the support remains in glibc. この変数は .BR bash (1) 2.0 が glibc と通信するために用いられた。 どの引き数がワイルドカードを展開した結果で、 したがってオプションとみなすべきでないかを知らせるものである。 この機能は .BR bash (1) のバージョン 2.01 で削除されたが、glibc にはまだ残っている。 .\"O .SH "CONFORMING TO" .SH 準拠 .TP .BR getopt (): .\"O POSIX.2 and POSIX.1-2001, .\"O provided the environment variable .\"O .B POSIXLY_CORRECT .\"O is set. .\"O Otherwise, the elements of \fIargv\fP aren't really const, because we .\"O permute them. .\"O We pretend they're const in the prototype to be .\"O compatible with other systems. 環境変数 .B POSIXLY_CORRECT が設定されている場合は POSIX.2 と POSIX.1-2001 に準拠する。 他の場合は \fIargv\fP の要素は本当の意味での定数にはならない。 なぜなら順序が変更されてしまうからである。 ただしそれらは、プロトタイプでは定数であるかのようにしてある。 これは他のシステムとの互換性のためである。 .\"O The use of \(aq+\(aq and \(aq\-\(aq in .\"O .I optstring .\"O is a GNU extension. .I optstring で \(aq+\(aq や \(aq\-\(aq を使うのは GNU による拡張である. .\"O On some older implementations, .\"O .BR getopt () .\"O was declared in .\"O .IR . .\"O SUSv1 permitted the declaration to appear in either .\"O .I .\"O or .\"O .IR . .\"O POSIX.1-2001 marked the use if .\"O .I .\"O for this purpose as LEGACY. .\"O POSIX.1-2001 does not allow the declaration to appear in .\"O .IR . 古い実装のいくつかでは、 .BR getopt () は .I で宣言されていた。 SUSv1 では、 .I か .I のどちらかで 宣言してもよかった。 POSIX.1-2001 では、 .B getopt の宣言を .I で行うのは「過去の名残」であるとされた。 POSIX.1-2001 では .I で宣言を行うことを認めていない。 .TP .\"O .BR getopt_long "() and " getopt_long_only (): .\"O These functions are GNU extensions. .BR getopt_long "(), " getopt_long_only (): これらの関数は GNU による拡張である。 .\"O .SH NOTES .SH 注意 .\"O A program that scans multiple argument vectors, .\"O or rescans the same vector more than once, .\"O and wants to make use of GNU extensions such as \(aq+\(aq .\"O and \(aq\-\(aq at the start of .\"O .IR optstring , .\"O or changes the value of .\"O .B POSIXLY_CORRECT .\"O between scans, .\"O must reinitialize .\"O .BR getopt () .\"O by resetting .\"O .I optind .\"O to 0, rather than the traditional value of 1. .\"O (Resetting to 0 forces the invocation of an internal initialization .\"O routine that rechecks .\"O .B POSIXLY_CORRECT .\"O and checks for GNU extensions in .\"O .IR optstring .) 複数の引き数ベクトルをスキャンしたり、同じ引き数ベクトルを二回以上 スキャンするようなプログラムで、 .I optstring の先頭で \(aq+\(aq や \(aq\-\(aq といった GNU による拡張機能を使用したり、 引き数ベクトルの切り替え時に .B POSIXLY_CORRECT の値を変更したりする場合には、 .I optind を伝統的な 1 ではなく 0 にリセットすることで .BR getopt () を再初期化しなければならない (0 にリセットすることで、 .B POSIXLY_CORRECT や .I optstring の GNU 拡張機能のチェックを行う内部初期化ルーチンが起動される)。 .\"O .SH "BUGS" .SH バグ .\"O The POSIX.2 specification of .\"O .BR getopt () .\"O has a technical error described in POSIX.2 Interpretation 150. .\"O The GNU .\"O implementation (and probably all other implementations) implements the .\"O correct behavior rather than that specified. POSIX.2 における .BR getopt () の仕様には技術的な問題があり、 その内容は POSIX.2 Interpretation 150 に記されている。 GNU による実装では (おそらく他のすべての実装でも)、 仕様と異なる正しい動作をするように実装されている。 .\"O .SH EXAMPLE .SH 例 .\"O The following trivial example program uses .\"O .BR getopt () .\"O to handle two program options: .\"O .IR \-n , .\"O with no associated value; and .\"O .IR "\-t val" , .\"O which expects an associated value. 以下に示す簡単なサンプルプログラムでは、 二種類のプログラムオプションを扱うのに .BR getopt () を使用している。一つは値を伴わない .I \-n で、もう一つは対応する値が必要な .I "\-t val" である。 .nf .sp #include #include #include int main(int argc, char *argv[]) { int flags, opt; int nsecs, tfnd; nsecs = 0; tfnd = 0; flags = 0; while ((opt = getopt(argc, argv, "nt:")) != \-1) { switch (opt) { case \(aqn\(aq: flags = 1; break; case \(aqt\(aq: nsecs = atoi(optarg); tfnd = 1; break; default: /* \(aq?\(aq */ fprintf(stderr, "Usage: %s [\-t nsecs] [\-n] name\\n", argv[0]); exit(EXIT_FAILURE); } } printf("flags=%d; tfnd=%d; optind=%d\\n", flags, tfnd, optind); if (optind >= argc) { fprintf(stderr, "Expected argument after options\\n"); exit(EXIT_FAILURE); } printf("name argument = %s\\n", argv[optind]); /* Other code omitted */ exit(EXIT_SUCCESS); } .fi .PP .\"O The following example program illustrates the use of .\"O .BR getopt_long () .\"O with most of its features. 以下は、 .BR getopt_long () の使用法を、ほぼすべての機能について示したプログラムの例である。 .nf .sp #include /* for printf */ #include /* for exit */ #include int main(int argc, char **argv) { int c; int digit_optind = 0; while (1) { int this_option_optind = optind ? optind : 1; int option_index = 0; static struct option long_options[] = { {"add", required_argument, 0, 0 }, {"append", no_argument, 0, 0 }, {"delete", required_argument, 0, 0 }, {"verbose", no_argument, 0, 0 }, {"create", required_argument, 0, \(aqc\(aq}, {"file", required_argument, 0, 0 }, {0, 0, 0, 0 } }; c = getopt_long(argc, argv, "abc:d:012", long_options, &option_index); if (c == \-1) break; switch (c) { case 0: printf("option %s", long_options[option_index].name); if (optarg) printf(" with arg %s", optarg); printf("\\n"); break; case \(aq0\(aq: case \(aq1\(aq: case \(aq2\(aq: if (digit_optind != 0 && digit_optind != this_option_optind) printf("digits occur in two different argv\-elements.\\n"); digit_optind = this_option_optind; printf("option %c\\n", c); break; case \(aqa\(aq: printf("option a\\n"); break; case \(aqb\(aq: printf("option b\\n"); break; case \(aqc\(aq: printf("option c with value \(aq%s\(aq\\n", optarg); break; case \(aqd\(aq: printf("option d with value \(aq%s\(aq\\n", optarg); break; case \(aq?\(aq: break; default: printf("?? getopt returned character code 0%o ??\\n", c); } } if (optind < argc) { printf("non-option ARGV\-elements: "); while (optind < argc) printf("%s ", argv[optind++]); printf("\\n"); } exit(EXIT_SUCCESS); } .fi .\"O .SH "SEE ALSO" .SH 関連項目 .BR getsubopt (3)