クラス toppers::cmdline

コマンドライン解析クラス [詳細]

#include "toppers/cmdline.hpp"

すべてのメンバ一覧

Public メソッド

 cmdline (int argc, char *argv[])
 main関数の引数を受け取るコンストラクタ
std::auto_ptr< std::vector<
std::string > > 
parse (const std::string &pattern, option_array &options) const
 コマンドライン引数の解析

Static Public メソッド

static std::vector< std::string
>::const_iterator 
find_illegal_options (std::vector< std::string >::const_iterator first, std::vector< std::string >::const_iterator last)
 不正オプションの探索

構成

class  option_array
 コマンドラインオプション格納用コンテナ [詳細]


説明

コマンドライン解析クラス

cmdline.hpp66 行で定義されています。


コンストラクタとデストラクタ

toppers::cmdline::cmdline int  argc,
char *  argv[]
 

main関数の引数を受け取るコンストラクタ

引数:
argc コマンドライン引数の個数
argv コマンドライン引数を格納した配列

cmdline.cpp239 行で定義されています。

00239                                            : argc_( argc ), argv_( argv )
00240   {
00241   }


関数

std::vector< std::string >::const_iterator toppers::cmdline::find_illegal_options std::vector< std::string >::const_iterator  first,
std::vector< std::string >::const_iterator  last
[static]
 

不正オプションの探索

引数:
first オプション列の先頭位置
last オプション列の終端位置+1
戻り値:
不正オプションを検出した場合はその位置を、それ以外はlastを返す。
覚え書き:
不正オプションを検出した場合、reportererror が呼び出されます。

cmdline.cpp362 行で定義されています。

参照先 _rout.

00363   {
00364     for ( std::vector<std::string>::const_iterator iter( first ); iter != last; ++iter )
00365     {
00366       if ( iter->at( 0 ) == '-' )
00367       {
00368         rout.error( _( "illegal option `%1%\'" ) % *iter );
00369         return iter;
00370       }
00371     }
00372     return last;
00373   }

std::auto_ptr< std::vector< std::string > > toppers::cmdline::parse const std::string &  pattern,
option_array options
const
 

コマンドライン引数の解析

引数:
pattern オプションとして受け入れるパターン
options 解析結果の格納先
戻り値:
オプション以外の引数
patternには、受け入れるオプションを空白文字で区切って列挙します。 その際、パラメータを受け取るオプションは、末尾に':'または'='を指定してください。 ':'を指定した場合、オプション名の後にそのままパラメータを渡します。 '='を指定した場合、オプション名の後に'='を挟んでパラメータを指定します。

  // example
  cmdline cmdl( argc, argv );
  option_array options;
  cmdl.parse( "-I: -v --source=", options );

cmdline.cpp261 行で定義されています。

参照先 _rout.

00262   {
00263     std::stringstream sstr( pattern );
00264     option_array::map optmap;
00265     option_array::set optset;
00266     std::list<const char*> args( argv_+1, argv_+argc_ );
00267 
00268     // `options'文字列内の重複要素をまとめる
00269     std::string name;
00270     while ( sstr >> name )
00271     {
00272       if ( name[0] == '-' ) // '-'で始まるもの以外はオプションとして認めない
00273       {
00274         optset.insert( name );
00275       }
00276     }
00277 
00278     const std::set<std::string>::const_iterator last( optset.end() );
00279     for ( std::set<std::string>::const_iterator iter( optset.begin() ); iter != last; ++iter )
00280     {
00281       std::string name( *iter );
00282       bool has_arg = false;
00283       char back = *name.rbegin();
00284       if ( back == ':' ) // ':'で終わるオプションは'='なしの引数を取る
00285       {
00286         has_arg = true;
00287         name.erase( name.size()-1 ); // ':'を除去
00288       }
00289       else if ( back == '=' ) // '='で終わるオプションは'='付きの引数を取る
00290       {
00291         has_arg = true;
00292       }
00293       std::list<const char*>::iterator iter2( args.begin() );
00294       while ( iter2 != args.end() )
00295       {
00296         const char* argv = *iter2;
00297         if ( ( back == '=' && strncmp( argv, name.c_str(), name.size() ) == 0 )
00298           || ( back != '=' && strcmp( argv, name.c_str() ) == 0 ) )
00299         {
00300           typedef option_array::map::value_type option;
00301           if ( has_arg ) // 引数付きオプション
00302           {
00303             if ( argv[name.size()] != '\0' )
00304             {
00305               argv = argv+name.size();
00306             }
00307             else
00308             {
00309               std::list<const char*>::iterator next( boost::next( iter2 ) );
00310               if ( next != args.end() )
00311               {
00312                 argv = *next;
00313               }
00314               else
00315               {
00316                 // このエラーメッセージは翻訳されそうにないが、とりあえず放置する
00317                 rout.error( _( "argument to `%1%\' is missing" ) % name );
00318               }
00319               args.erase( next );
00320             }
00321             while ( std::isspace( *argv ) ) // 念のため、空白類文字を取り除く
00322                  ;
00323             if ( back == ':' )
00324             {
00325               name += ':';
00326             }
00327             option opt( name, std::string( argv ) );
00328             optmap.insert( opt );
00329           }
00330           else // 引数なしオプション
00331           {
00332             option opt( name, "" );
00333             optmap.insert( opt );
00334           }
00335           std::list<const char*>::iterator next = boost::next( iter2 );
00336           args.erase( iter2 );
00337           iter2 = next;
00338         }
00339         else // nameが示すオプション以外
00340         {
00341           ++iter2;
00342         }
00343       }
00344     }
00345 
00346     // 例外安全のため std::auto_ptr を用いて、ここで返却値を組み立てる
00347     std::auto_ptr<std::vector<std::string> > result( new std::vector<std::string>( args.begin(), args.end() ) );
00348 
00349     option_array t( optmap, optset );
00350     options.swap( t );
00351 
00352     return result;
00353   }


このクラスの説明は次のファイルから生成されました:
Copyright © 2006 by TAKAGI Nobuhisa.
このページは Wed Apr 12 16:32:02 2006 に Doxygen によって生成されました。