Saggy Posted September 26, 2009 Share Posted September 26, 2009 I'm trying to use the getopt() function in glibc to parse through command line options. Here is the man page here. http://linux.die.net/man/3/getopt It says that "optstring" holds characters for options that are valid, and if they are followed by a ':' they require an argument. The only problem is in my code, only the last option character in optstring will trip an error message for a missing argument. If "optstring" starts with a ':' then it returns ':' for any option missing an argument, and '?' for any option character it does not recognize. This is my code, and option 'f' is the only one that will return a ':' if it lacks its option argument. I'm not sure how many people are even familiar with getopt() here but maybe they can read that man page and spot something I'm missing... while ((opt = getopt(argc, argv, "n:p:x:f:hard")) != -1) { switch(opt) { case 'h': printSyntax("passmanager"); return 1; break; case 'a': options.Add = 1; break; case 'r': options.Read = 1; break; case 'd': options.Delete = 1; break; case 'f': if(options.Add == 1) passWordFile = fopen(optarg, "ab"); if(options.Read == 1) passWordFile = fopen(optarg, "rb"); if(options.Delete == 1) { passWordFile = fopen(optarg, "rb+"); strcpy(options.passWordFileName, optarg); } break; case 'n': if(optarg[0] == '-') { printf("Option -n requires an operand\n"); errflg++; } else options.entrySearch = 1; if(strlen(optarg) > 24) { printf("entry name too long"); printSyntax("passmanager"); return 1; } strcpy(entryName, optarg); break; case 'p': if(optarg[0] == '-') { printf("Option -p requires an operand\n"); errflg++; } if(strlen(optarg) > 24) { printf("password too long"); printSyntax("passmanager"); return 1; } strcpy(passWord, optarg); break; case 'x': if(optarg[0] == '-') { errflg++; printf("Option -x requires an operand\n"); } strcpy(xcetPass, optarg); break; case ':': printf("Option -%c requires an operand\n", optopt); errflg++; break; case '?': if (optopt == 'n') fprintf (stderr, "Option -%c requires an argument.\n", optopt); if (optopt == 'f') fprintf (stderr, "Option -%c requires an argument.\n", optopt); if (optopt == 'x') fprintf (stderr, "Option -%c requires an argument.\n", optopt); if (optopt == 'p') fprintf (stderr, "Option -%c requires an argument.\n", optopt); else printf("Unrecognized option: -%c\n", optopt); errflg++; } } if(errflg) { printSyntax("passmanger"); return 1; } So far I've modified the way to parse the commands so that each case argument checks for an argument value instead of relying on getopt() to do it, but I would like to know why it's not working correctly. QUOTE (K^2) ...not only is it legal for you to go around with a concealed penis, it requires absolutely no registration! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now