getopt实例

#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;
}

你可能感兴趣的:(getopt实例)