引数の後にさらに optargs を取得したいのですが、その方法がわかりません。プログラムを次のように呼び出したい
./test -a first second -b third
引数 -a の後に値を 1 つだけ取得できるようになりました。2 つ以上入れようとすると、avalue が null になります。
私のコード:
char *avalue = NULL;
char *bvalue = NULL;
while ((c = getopt (argc, argv, "a:b:")) != -1)
switch (c)
{
case 'a':
avalue = optarg;
break;
case 'b':
bvalue = optarg;
break;
case '?':
if (optopt == 'c')
fprintf (stderr, "Option -%c requires an argument.\n", optopt);
else if (isprint (optopt))
fprintf (stderr, "Unknown option `-%c'.\n", optopt);
else
fprintf (stderr,
"Unknown option character `\\x%x'.\n",
optopt);
return 1;
default:
abort ();
}
printf ("avalue = %s\nbvalue = %s\n",avalue, bvalue);