getopt实例

来源:互联网 发布:药品数据 编辑:程序博客网 时间:2024/06/03 15:52
#include <stdio.h>
#include <getopt.h>

int main(int argc, const char **argv)
{
    int c;

    while (1)
    {
        int option_index = 0;

        struct option long_options[] = {
                { "type", 1, 0, 't' },
                { "help", 0, 0, 'h' },
                { 0, 0, 0, 0 }};//END

        c = getopt_long(argc, (char **)argv, "t:h", long_options, &option_index);

        if (c == -1)
            break;

        char * opt_args = NULL;
        switch (c)
        {
        case 't':
                    {
                        opt_args = optarg;
                        if(opt_args)
                        {
                            printf("t:%s\r\n", opt_args);
                        }
                        break;
                    }
        case 'h':
                    {

                        printf("help\r\n");
                        break;
                    }
                    default:
                        {
                            printf("unknow!\r\n");
                            break;
                        }
        }
    }

    return 0;
}