パースペクティブを理解するために getopt を使用して簡単なコードを作成しました。
#include <stdio.h>
#include <unistd.h>
int main(int argc, char* argv[])
{
/* Here since c is followed with colon, so 'c' takes an argument */
const char *optstring = "abc:d";
int ret;
while((ret=getopt(argc, argv, optstring))!= -1)
{
switch(ret)
{
case 'a':
printf("Option found: a, optind = %d\n",optind);
break;
case 'b':
printf("Option found: b, optind = %d\n",optind);
break;
case 'c':
printf("Option found: c, optind = %d\n",optind);
printf("Option argument: %s\n",optarg);
break;
case 'd':
printf("Option found: d, optind = %d\n",optind);
break;
case ':':
printf("The option takes an argument which is missing");
break;
//case '?':
// printf("Didn't you enter an invalid option?");
// break;
}
}
}
問題は:
(1) ケース 1:case '?'
がコメント化さ
れている場合:
[root@dhcppc0 getopt]# ./a.out -a -b -c
Option found: a, optind = 2
Option found: b, optind = 3
./a.out: option requires an argument -- c
したがって、ご覧case ':'
のとおり、引数が欠落していると getopt によって ':' (コロン) が返されることが通常想定されるため、 は有効になりませんでした。
(2) ケース 2:
そして、コメントを外してプログラムを実行すると、case '?
引数が欠落している場合でもヒットします。
enter code here
[root@dhcppc0 getopt]# ./a.out -a -b -c
Option found: a, optind = 2
Option found: b, optind = 3
./a.out: option requires an argument -- c
Didn't you enter an invalid option?
ここで私が見逃している点は何ですか?
後で追加:
また./a.out: option requires an argument -- c
、デフォルトのエラーが発生するのはなぜですか? 私はすでに でそれを処理していてcase ':'
、デフォルトのエラー メッセージが必要ないので、どのように処理するのですか?
再度追加:答えで示唆されているように、 optstring - の先頭にコロンを使用しましたがconst char *optstring = ":abc:d"
、なぜこれが起こっているのですか?
./a.out -a -b -c -d returns -d as the argument to c?? -d is a separate optional character and not any argument