[ << ] [ >> ]           [Top] [Contents] [Index] [ ? ]

7. 複数回オプション

オプションが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.