Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4820874
  • 博文数量: 930
  • 博客积分: 12070
  • 博客等级: 上将
  • 技术积分: 11448
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-15 16:57
文章分类

全部博文(930)

文章存档

2011年(60)

2010年(220)

2009年(371)

2008年(279)

分类: C/C++

2008-10-05 19:02:04

   不要说我无聊,无聊的人很多,绝对不只我一个.要不哥们你也不会路过...大家无聊才是真的无聊嘛!!其实也算是getopt_long的一个应用.

#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! |
+---------------+
阅读(1119) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~