Linux C getopt_long

#include <stdio.h>
#include <getopt.h>

char *l_opt_arg;
char* const short_options = "c:";


struct option long_options[] = {
  { "aa", 0, NULL, 0 },
  { "bb", 0, NULL, 0 },
  { "cc", required_argument, NULL, 'c' },
  { 0, 0, 0, 0},
};


int main(int argc, char *argv[])
{
        int c;
        int index = 0;
        while((c = getopt_long (argc, argv, short_options, long_options, &index)) != -1)
        {
                switch (c)
                {
                        case 0:
                                switch (index)
                                {
                                        case 0:
                                                printf("is aa : c = %d, index = %d ", c, index);
                                                break;
                                        case 1:
                                                printf("is bb : c = %d, index = %d ", c, index);
                                                break;
                                        default:
                                                printf("no one : c= %d, index = %d ", c, index);

                                                break;

                                  }
            break;
        case 'c':
            l_opt_arg = optarg;
            printf("cc is %s(optarg), c = %c, index = %d! ", l_opt_arg, c, index);
            break;
      }
  }
  return 0;
}

原文地址:https://www.cnblogs.com/banwhui/p/4597318.html