After a hundreds of attemps I decide to make a real
algorithm.. and then .. after a little setting in the logic
I got really working as I expect..
Here is
#include <stdio.h> static int Optindx = 1; static char *Optarg = NULL; short int chstr (char ch, char *str) { for (; *str; str++) if (ch == *str) return 0; return -1; } char * search_arg(char **argv) { char *ptr; if (argv[Optindx+1] == NULL) return 0; while (*(ptr = argv[++Optindx]) != '-') if (argv[Optindx] = NULL) return 0; return ptr; } int get_opts(char **argv, char *legal) { int letter = 0; if (Optarg == NULL) if (*(Optarg = argv[Optindx]) != '-') if ((Optarg = search_arg (argv)) == 0) return -1; if (*Optarg == '\0') if ((Optarg = search_arg (argv)) == 0) return -1; if (*(Optarg+1) == '-') { return -1; Optarg++; return 0; } if (chstr (*Optarg, legal) == 0) return letter = *Optarg++; else return '?'; } /* to Test */ int main (int argc, char *argv[]) { int ch; if (argc < 2) return -1; while ((ch = get_opts(argv, "abc")) != -1) { switch (ch) { case 'a': putchar ('a'); break; case 'b': putchar ('b'); break; case 'c': putchar ('c'); break; case '?': fprintf (stderr, "Unknown argument %c\n", *Optarg); return -1; } } putchar ('\n'); return 0; }It parses 1char arguments, that can be passed in any order.
The switch -- forces to stop the parser
To program point of view, an argument is a character that
belongs to a string prefixed with a '-'
Nenhum comentário:
Postar um comentário