コマンドラインからいくつかの引数を取得するプログラムが必要です。構文は次のとおりです。
getpwd -l user1 user2 ... -L -X -S...
そのため、ユーザーに-l
オプションを提供する必要があります。を使用してみgetopt
ましたが、運が悪かったため、他のオプションを の前に配置した場合にのみ機能します-l
。
getpwd -L -X -S ... -l user1 user2 ...
私のコード(-l
と-S
):
while((c = getopt(argc, argv, "l:S")) != -1){
switch(c){
case 'l':
index = optind-1;
while(index < argc){
next = strdup(argv[index]); /* get login */
index++;
if(next[0] != '-'){ /* check if optarg is next switch */
login[lcount++] = next;
}
else break;
}
break;
case 'S':
sflag++; /* other option */
break;
case ':': /* error - missing operand */
fprintf(stderr, "Option -%c requires an operand\n", optopt);
break;
case '?': /* error - unknown option */
fprintf(stderr,"Unrecognized option: -%c\n", optopt);
break;
}
}
optopt
とoptind
ですextern int
。
そこで問題は:getopt()
関数 (またはgetopt_long()
) を使用できますか? それとも、必要なものを取得するために独自のパーサーを作成する必要がありますか?