Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef GETOPT_H
- #define GETOPT_H
- // GetOpt is a way to parse the list of arguments received by main looking for option and their parameters.
- // Option shorthand start with / or -, followed by single letter, followed by zero or more argument.
- // For shorthand option, the first argument may be glued to option char, without space.
- // Option longhand start with -- followed by a string of more than one character, followed by zero or more arguments.
- // Arguments and options are space separated.
- // When arguments contains space that should not be considered separator, enclose them in rabbit-ears: "
- // If GetOpt encounters an option it does not understand, or other type of wrongly formatted command-line parameter,
- // then then it ignores it, parse the rest, and finally prints the list of allowed options, and return a non zero value.
- // The first two arguments of GetOpt shall be the argc and argv received by main
- // The third argument of GetOpt shall be a list, where each element is a possible option, with its shorthand, longhand, description, and function pointer
- // Option shorthand is char, longhand and description are strings.
- // 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.
- // To disable option shorthand, set it to a character that is not alphanumerical.
- // The option list shall be terminated by a fake option made of NULL.
- /* Example implementation:
- int SetOptA(int optcount, char *optvalues[])
- {
- int n;
- printf("SetOptA called with");
- if(!optcount)
- printf(" no arguments.");
- else
- printf(" %d arguments:",optcount);
- for(n=0;optvalues[n];++n)
- printf(" [%s]",optvalues[n]);
- printf("\n");
- return 0;
- }
- int SetOptB(int optc, char *optv[])
- {
- int n;
- printf("SetOptB called with %d arguments\n",optc);
- for(n=0;n<optc;++n)
- printf("\tSetOptB optv[%d]=%s\n",n,optv[n]);
- return 0;
- }
- tOptionList options={
- {'a',"opta","This sets Option A",&SetOptA},
- {'b',"optb","This sets Option B",&SetOptB},
- {'\0',NULL,NULL,NULL}};
- int main(int argc, char *argv[])
- {
- GetOpt(argc,argv,options);
- }
- */
- typedef struct tOptionListEntry
- {
- char shortopt;
- char* longopt;
- char* description;
- int (*fct)(int, char*[]);
- } tOptionListEntry;
- typedef tOptionListEntry tOptionList[];
- int GetOpt(int argc, char *argv[],tOptionList optlst);
- void PrintOptList(tOptionList optlst);
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement