0

I was using getopt_long read command line options. code:

#include <getopt.h>
#include <stdlib.h>
#include <stdio.h>
int
main(int argc, char *argv[])
{
    int ch;
    struct option longopts[] = {
        {"password", required_argument, NULL, 'p'},
        {"viewonly", no_argument, NULL, 'v'},
        {"help", no_argument, NULL, 'h'},
        {NULL, 0, NULL, 0}
    };
    while ((ch = getopt_long(argc, argv, "p:vh", longopts, NULL)) != -1) {
        switch (ch) {
        case 'p':
            printf("optarg: %x %s\n", optarg, optarg);
            break;
        case 'v':
            printf("viewonly is set\n");
            break;
        case 'h':
        case '?':
        default:
            fprintf(stderr, "error\n");
            exit(EXIT_FAILURE);
        }
    }
    return 0;
}

and I using this command line option: ./a.out --password --viewonly, It's supposed to print error message that --password is missing argument, but getopt_long never return '?', but treat --viewonly as the optarg of --password. and the output is:

optarg: 24992bc4 --viewonly

I think it's strange, and what should I do to prevent getopt_long treat option name as argument?

leeyiw
  • 414
  • 2
  • 6
  • 14
  • 1
    You can't. Instead you have to detect the problem later in the code, like when checking the password and then tell the user that the user provided the wrong password. – Some programmer dude Jun 24 '14 at 06:21
  • but it also hide `viewonly` option from being read. Can it be correct manually? – leeyiw Jun 24 '14 at 06:24
  • If the user provides the wrong password, will you even get far enough in the program to use the `viewonly` option? The user passing the wrong password (or it's "missing" like in your case) it should be a pretty fatal error, which means that the user will re-run your program with the (hopefully correct) password, and the issue is kind of moot. Your problem is only a problem in a small test program like the one provided here. – Some programmer dude Jun 24 '14 at 06:28
  • 1
    If you're using C++, try Boost Program Options instead. – John Zwinck Jun 24 '14 at 06:31
  • it's right here. but I think there are some other conditions to be consider. and why getopt_long didn't detect if the optarg is start with '-' or '--' and return '?' ? – leeyiw Jun 24 '14 at 06:31
  • Two things to consider: First of all `getopt_long` is not a standard function anywhere, it's a GNU libc extension. Secondly: There is nowhere specified that it *must* check for options when expecting a non-option argument. It simply sees that you have the `password` option, it requires an argument, and provides you with that argument, no matter what that argument is. – Some programmer dude Jun 24 '14 at 06:35
  • I understand. Looks like I must check the `optarg` myself, and I already tested that `getopt` has the same problem. – leeyiw Jun 24 '14 at 06:39
  • Just be careful, because who says that the user may not have e.g. `--viewonly` (or any other option you might have) as password? – Some programmer dude Jun 24 '14 at 06:41
  • That's right, I will ignore this issue at this moment, and try to avoid option required argument. – leeyiw Jun 24 '14 at 06:43

1 Answers1

0

I would have some error checking in my switch statement (maybe check that a password cannot start with '--')