Example defobj/Arguments/1.
たとえば、`protocol'という新しい引数を標準のコマンドリストに追加したいと
しましょう。つまり、コマンドラインに--helpと入力した際に以下のように表示
されるようにしたいとします。
------------------------
mgd@wijiji[/opt/src/mgd/src/mySwarmApp] $ ./mySwarmApp --help
Usage: mySwarmApp [OPTION...]
-s, --varyseed Run with a ramdom seed -ランダムシードで実行
-b, --batch Run in batch mode -バッチモードで実行
-m, --mode=MODE Specify mode of use (for archiving) -(アーカイブに対する)使用するモードを指定
-p, --protocol=PROTOCOL Set protocol -プロトコルを設定
-?, --help Give this help list -このヘルプリストを取得
--usage Give a short usage message -利用法を簡潔に取得
-V, --version Print program version -プログラムバージョンをプリント
Mandatory or optional arguments to long options are also mandatory or
optional for any corresponding short options.
-- 長い表記のオプションに対する必須あるいはオプショナルの引数は、それに
対応する短いオプションでも同様である必要があります。
Report bugs to bug-swarm@santafe.edu.
-- バグレポートはbug-swarm@santafe.eduに送ってください。
-----------------------
これを実装するには、以下のように、あなた自身のサブクラスをArgumentsから
作成する必要があります。
#import <defobj/Arguments.h>
@interface MySwarmAppArguments: Arguments_c
{
const char *protocolArg;
}
- (const char *)getProtocolArg;
@end
@implementation MySwarmAppArguments
+ createBegin: aZone
{
static struct argp_option options[] = {
{"protocol", 'p', "PROTOCOL", 0, "Set protocol", 3},
{ 0 }
};
MySwarmAppArguments *obj = [super createBegin: aZone];
[obj addOptions: options];
return obj;
}
- (int)parseKey: (int)key arg: (const char *)arg
{
if (key == 'p')
{
protocolArg = arg;
return 0;
}
else
return [super parseKey: key arg: arg];
}
- (const char *)getProtocolArg
{
return protocolArg;
}
@end
これを実際にmain.mプログラムから実行するためには、以下のようにします。
int
main (int argc, const char ** argv)
{
initSwarmArguments (argc, argv, [MySwarmAppArguments class]);
// いつも通りの - buildObjects:, - buildActions:, - activateIn: の呼び出し
return 0;
} |