/*终于给我找到这个例子了,想找一个带选项的程序示例*/
#include
#include
#include
int
main( int argc, char *argv[] )
{
int c;
while (1) {
int option_index = 0;
static struct option long_options[] = {
{ "help", 0, 0, 'h' },
{ "version", 0, 0, 'v' },
{ "traditional", 0, 0, 't' },
{ "next-generation", 0, 0, 'n' },
{ 0, 0, 0, 0 }
};
c = getopt_long( argc, argv, "hvtn",
long_options, &option_index );
if (c == -1)
break;
switch (c) {
case 'h':
printf( "``hello'' is a greeting program which wrote by flw.\n"
"\n"
"Usage: hello [OPTIONS]\n"
" -h, --help display this message then exit.\n"
" -v, --version display version information then exit.\n"
"\n"
" -t, --traditional output a greeting message with traditional format.\n"
" -n, --next-generation output a greeting message with next-generation format.\n"
"\n"
"Report bugs to \n"
);
break;
case 'v':
printf( "hello - flw's hello world. 0.8 version\n" );
break;
case 't':
printf( "hello, world\n" );
break;
case 'n':
printf(
"+---------------+\n"
"| Hello, world! |\n"
"+---------------+\n"
);
break;
default:
break;
}
}
if ( optind < argc ){
fprintf( stderr,
"Too many arguments\n"
"Try `hello --help' for more information.\n"
);
exit(1);
}
if ( optind == 1 ){
printf( "Hello, world!\n" );
}
exit (0);
}
阅读(1617) | 评论(0) | 转发(0) |