これがあなたの意図したものかどうかはよくわかりませんが、これは私が別のプロジェクトでそれを実装した方法です:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define TRUE 1
#define FALSE 0
int usage( char *name, int quit );
int main (int argc, char **argv) {
int c;
static int vlevel = 0;
while ( (c = getopt(argc, argv, ":abc:d:hv012")) != -1) {
int this_option_optind = optind ? optind : 1;
switch (c) {
case 'v':
vlevel++;
printf ("verbosity level is %d\n", vlevel);
break;
case ':': /* Option without required operand */
fprintf(stderr, "option -%c requires an operand\n", optopt);
break;
case 'h':
case '?':
usage( argv[0], TRUE );
break;
default:
printf ("?? getopt returned character code 0%o ??\n", c);
}
}
if (optind < argc) {
printf ("non-option ARGV-elements:\n");
while (optind < argc)
printf ("\t%s\n", argv[optind++]);
}
exit (0);
}
int usage( char *progname, int quit )
{
printf ( "Usage:\n\t%s [-vh]\n", progname );
if ( quit ) exit( 1 );
return 0;
}
これにより、次のような結果が得られます。
eroux@smaug:~$ ./testverbose -h
Usage:
./testverbose [-vh]
eroux@smaug:~$ ./testverbose -vvvv
verbosity level is 1
verbosity level is 2
verbosity level is 3
verbosity level is 4
eroux@smaug:~$
そこから、[main() で] vlevel 変数を使用して、関連する詳細レベルで正しいメッセージを出力できるはずです。