#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
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 <[email]flw@cpan.org[/email]>\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( EXIT_FAILURE );
}
if ( optind == 1 ){
printf( "Hello, world!\n" );
}
exit ( EXIT_SUCCESS );
}
zj@zj:~/C_pram/practice$ ./helloworld -hvn ``hello'' is a greeting program which wrote by flw.
Usage: hello [OPTIONS] -h, --help display this message then exit. -v, --version display version information then exit.
-t, --traditional output a greeting message with traditional format. -n, --next-generation output a greeting message with next-generation format.
Report bugs to <[email]zj077543@gmail.com[/email]> hello - zj's hello world. 0.1 version +---------------+ | Hello, world! | +---------------+
|