0

I'm using dashes to handle large option names, such as:

  • --load-ptest
  • --load-program

Both of these options take no arguments for simplicity.

Here's the code snippet I'm using as an example:

enum FLAGS{
    LOAD_PTEST_FLAG,
    LOAD_PROGRAM_FLAG
};

static struct option long_options[] = {
    {"load-program", no_argument, 0, LOAD_PROGRAM_FLAG},
    {"load-ptest", no_argument, 0, LOAD_PTEST_FLAG},
    {NULL, 0, NULL, 0}
};

while((c = getopt_long(argc, argv, "", long_options, &option_index)) != -1) {
    case LOAD_PTEST_FLAG:
        load_ptest = 1;
        break;
    case LOAD_PROG_THRU_GLI_FLAG:
        load_program = 1;
        break;
    case '?':
        print_help();
        break;
}

However, I've noticed that when running this code, getopt_long() doesn't seem to be checking the complete option name. Running ./a.out --load-pt results with variable load_ptest set, while running ./a.out --load-pr results with variable load_program set. Additionaly, running ./a.out --load-p results with:

option '--load-p' is ambiguous; possibilities: '--load-program' '--load-ptest'

I'm wondering why getopt_long() is only matching partially. Is there any way to make it strictly check for a full match of the option name?

Franks
  • 50
  • 8

0 Answers0