コマンドライン引数の処理(getopt)

#include <iostream>
#include <unistd.h>
using namespace std;

int main( int argc, char **argv )
{

 int opt; 
 while( (opt = getopt(argc, argv, "abx:y:")) != -1 ) 3つめの引数でオプション文字を指定。引数付きはコロンを付ける。
   switch (opt) {
   case 'a' :
     cout << "a" << endl; break;
   case 'b' :
     cout << "b" << endl; break;
   case 'x' :
     cout << "x : " << optarg << endl; break;          引数は自動的に optarg に格納される
   case 'y' :
     cout << "y : " << optarg << endl; break;
   default :
     cout << "usage : ..." << endl;
   }
  
 while( optind < argc )
   cout << "option : " << argv[ optind++ ] << endl; オプション無しのデータはすべてのオプション文字を処理後に行う。optindはargvのインデックス
 return 0;
}
最終更新:2010年01月15日 16:22
ツールボックス

下から選んでください:

新しいページを作成する
ヘルプ / FAQ もご覧ください。