[ << ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
オプションがmultiple
として指定されている場合,それをコマンドライ
ンで複数回指定することが可能です.ここで,オプションをfoo
と仮定
すると,生成される引数の構造体のfoo_given
フィールドには,指定さ
れた回数が含まれ,生成されるfoo_arg
フィールドは,このオプション
で指定されたすべての値を含んでいる配列になります.
複数回オプションのデフォルト値が指定されている場合,それ以外の値がコマ
ンドラインで指定されていない場合だけ割り当てられることに注意してくださ
い(そのときは,対応する_given
フィールドは1に設定されます).すな
わち,デフォルト値は常に複数回オプションの値になるわけではありません.
例えば,gengetoptへのファイルが以下のものを考えます.
# test options that can be given more than once option "string" s "string option" string no multiple option "int" i "int option" int no multiple |
そして,コマンドラインオプションを以下のようにまとめることが可能です.
プログラムが以下のコマンドラインオプションで呼び出されると仮定します.
/* test options that can be given more than once */ #include <stdlib.h> #include <stdio.h> #include "test_multiple_cmd.h" static struct gengetopt_args_info args_info; int main (int argc, char **argv) { int i = 0; if (test_multiple_cmd_parser (argc, argv, &args_info) != 0) exit(1) ; for (i = 0; i < args_info.string_given; ++i) printf ("passed string: %s\n", args_info.string_arg[i]); for (i = 0; i < args_info.int_given; ++i) printf ("passed int: %d\n", args_info.int_arg[i]); return 0; } |
プログラムの出力は以下のようになります.
passed string: world passed string: hello passed string: bar passed string: foo passed int: 200 passed int: 100 |
以下のように,複数回オプションにカンマで分離した引数を渡すことも可能で す.
./test_multiple -s"foo","bar","hello" -i100,200 -s "world" |
[ << ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This document was generated by Akihiro Sagawa on November, 2 2005 using texi2html 1.70.