Advertisement
userxbw

getopt and getsubopt C

Aug 18th, 2022
1,134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.74 KB | None | 0 0
  1. #include <ctype.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <getopt.h>
  6.  
  7.  
  8.  
  9. int
  10. main (int argc, char **argv)
  11. {
  12.  
  13. enum
  14. {
  15.    T, TH, D, BUTT
  16. };
  17.  
  18. const char *other_options[]=
  19. {
  20.    [T] = "t",
  21.    [TH] = "u",
  22.    [D] = "cc",
  23.    [BUTT] = NULL
  24. };
  25.  
  26.  
  27.   int aflag = 0;
  28.   int bflag = 0;
  29.   char *cvalue = NULL;
  30.   int index;
  31.   int c;
  32.  
  33.   opterr = 0;
  34.  
  35.   /* for subopts within opts */
  36.    char *subopts;
  37.    char *value;
  38.  /* required arguments use : after them
  39.     optional arguments :: after them */
  40.   static char string_options[]=
  41.   "ab::c:df::e:";
  42.   static struct option long_options[]=
  43.   { /* required_argument 1 { : }
  44.        optional_argument 2 { ::} */
  45.     {"alpha",0,0,'a'},
  46.     {"beta",0,0,'b'},
  47.     {"charlie",1,0,'c'},
  48.     {"delta",0,0,'d'},
  49.     {"echo",1,0,'e'},
  50.     {"foxtrot",0,0,'f'},
  51.     {0,0,0,0} // required at end
  52.   };
  53.   while ((c = getopt_long_only (argc, argv,
  54.     string_options,long_options,
  55.     &index)) != -1)
  56.   {
  57.     switch (c)
  58.       {
  59.       case 'a':
  60.         aflag = 1;
  61.         printf("alpha %s: aflag=%d\n",
  62.     optarg,aflag);
  63.         break;
  64.       case 'b':
  65.         bflag = 1;
  66.         printf("beta %s: bflag=%d\n",
  67.     optarg,bflag);
  68.         break;
  69.       case 'c':
  70.     if(optarg == NULL)
  71.        printf("argument is required\n");
  72.         cvalue = optarg;
  73.     printf("\ncvalue= %s\noptarg= %s\n",
  74.         cvalue,optarg);
  75.         break;
  76.      case 'd':
  77.      printf("delta\n");
  78.     break;
  79.       case '?':
  80.         if (optopt == 'c')
  81.           fprintf (stderr, "Option -%c "
  82.     "requires an argument.\n", optopt);
  83.         else if (isprint (optopt))
  84.           fprintf (stderr, "Unknown option"
  85.     "  `-%c'.\n", optopt);
  86.         else
  87.           fprintf (stderr,
  88.                    "Unknown option "
  89.         "character `\\x%x'.\n",
  90.                    optopt);
  91.            break;
  92.       case 'e':
  93.          subopts=optarg;
  94.           while(*subopts != '\0')
  95.           {
  96.         switch(getsubopt(&subopts,
  97.                    (char **)other_options,
  98.                   &value))
  99.            {  //cli ./programName -e t=value
  100.  
  101.             case T:
  102.                  if(value == NULL)
  103.                 printf("value in"
  104.             " subopt not "
  105.             "present\n");
  106.             else
  107.             printf("subopt value\n"
  108.                 " = %s\n",value);
  109.             break;
  110.         default:
  111.         printf("unknown < %s >\n",
  112.             value);
  113.         exit(1);
  114.         break;
  115.        } //end switch subopt
  116.             break;
  117.     }//end while subopt
  118.         case 'f':
  119.             printf("foxtrot\n");
  120.         break;
  121.       default:
  122.            abort ();
  123.        break;
  124.       } // end 1st switch
  125.   } // 1st while
  126.   printf ("aflag = %d, bflag = %d, cvalue = %s\n",
  127.           aflag, bflag, cvalue);
  128.  
  129.   for (index = optind; index < argc; index++)
  130.     printf ("Non-option argument %s\n",
  131.              argv[index]);
  132.  
  133. return 0;
  134. }
  135.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement