Advertisement
Guest User

GetOpt.h

a guest
Oct 18th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.44 KB | None | 0 0
  1. #ifndef GETOPT_H
  2. #define GETOPT_H
  3.  
  4. // GetOpt is a way to parse the list of arguments received by main looking for option and their parameters.
  5.  
  6. // Option shorthand start with / or -, followed by single letter, followed by zero or more argument.
  7. // For shorthand option, the first argument may be glued to option char, without space.
  8. // Option longhand start with -- followed by a string of more than one character, followed by zero or more arguments.
  9. // Arguments and options are space separated.
  10. // When arguments contains space that should not be considered separator, enclose them in rabbit-ears: "
  11. // If GetOpt encounters an option it does not understand, or other type of wrongly formatted command-line parameter,
  12. // then then it ignores it, parse the rest, and finally prints the list of allowed options, and return a non zero value.
  13.  
  14. // The first two arguments of GetOpt shall be the argc and argv received by main
  15. // The third argument of GetOpt shall be a list, where each element is a possible option, with its shorthand, longhand, description, and function pointer
  16. // Option shorthand is char, longhand and description are strings.
  17. // Option function pointer shall be of a prototype similar to a main: receive argument count and argument list, output error code or 0 if ok.
  18. // To disable option shorthand, set it to a character that is not alphanumerical.
  19. // The option list shall be terminated by a fake option made of NULL.
  20.  
  21. /* Example implementation:
  22.  
  23.     int SetOptA(int optcount, char *optvalues[])
  24.     {
  25.         int n;
  26.         printf("SetOptA called with");
  27.         if(!optcount)
  28.             printf(" no arguments.");
  29.         else
  30.             printf(" %d arguments:",optcount);
  31.         for(n=0;optvalues[n];++n)
  32.             printf(" [%s]",optvalues[n]);
  33.         printf("\n");
  34.         return 0;
  35.     }
  36.  
  37.     int SetOptB(int optc, char *optv[])
  38.     {
  39.         int n;
  40.         printf("SetOptB called with %d arguments\n",optc);
  41.         for(n=0;n<optc;++n)
  42.             printf("\tSetOptB optv[%d]=%s\n",n,optv[n]);
  43.         return 0;
  44.     }
  45.  
  46.     tOptionList options={
  47.             {'a',"opta","This sets Option A",&SetOptA},
  48.             {'b',"optb","This sets Option B",&SetOptB},
  49.             {'\0',NULL,NULL,NULL}};
  50.  
  51.     int main(int argc, char *argv[])
  52.     {
  53.  
  54.         GetOpt(argc,argv,options);
  55.     }
  56.  
  57. */
  58.  
  59. typedef struct tOptionListEntry
  60. {
  61.     char shortopt;
  62.     char* longopt;
  63.     char* description;
  64.     int (*fct)(int, char*[]);
  65. } tOptionListEntry;
  66.  
  67. typedef tOptionListEntry tOptionList[];
  68.  
  69.  
  70. int GetOpt(int argc, char *argv[],tOptionList optlst);
  71. void PrintOptList(tOptionList optlst);
  72.  
  73. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement