Chinaunix首页 | 论坛 | 博客
  • 博客访问: 372772
  • 博文数量: 84
  • 博客积分: 2010
  • 博客等级: 大尉
  • 技术积分: 940
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-12 20:30
文章分类

全部博文(84)

文章存档

2010年(18)

2009年(27)

2008年(39)

我的朋友

分类: C/C++

2009-04-01 20:40:30

/*终于给我找到这个例子了,想找一个带选项的程序示例*/
#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) |
给主人留下些什么吧!~~