1

I have a C program where I accept multiple arguments. Here, I have a common flag d for both data-store and disk. Is there a way that I can check for the flags in-order and get the value of store before I check with case d. I've tried various ways like adding a while loop before this to check for s and then enter this loop etc.

static void
ParseOptions(int argc, char* argv[])
{
   int c, option_index;
   int ind = 0;

   while((c = getopt_long(argc, argv, "s:d:",
                          long_options, &option_index))!= -1) {
       ind = optind;
       switch(c) {
        case 's':
            optionStore = true;
            store = strdup(optarg);
            break;
        case 'd':
            if(strcmp(store,"datastore") == 0){
                printf("In datastore\n");
                datastore = strdup(optarg);
            }
            else if(strcmp(store,"disk") == 0){
                printf("In disk\n");
                disk = strdup(optarg);

            }            
            break;
        default:
            exit(-1);
       }
   }
}

Not sure how to go about this.

nidHi
  • 833
  • 3
  • 10
  • 21
  • Possible duplicate of [Can I use getopt to process options in a certain order?](https://stackoverflow.com/questions/26473642/can-i-use-getopt-to-process-options-in-a-certain-order) – Karsten Koop Nov 28 '17 at 10:23
  • set a flag in the `case 's':` and test it in the `case 'd':` Note: there are **three** possible values for this flag, since `'s'` doesn't have to be present when `'d'` is seen. – joop Nov 28 '17 at 10:26
  • 1
    Check the options for compatibility after you've finished parsing them all. – Ian Abbott Nov 28 '17 at 10:27
  • I faced a small issue with the solution @dasblinkenlight gave. When I provide the flags in the command line, I am not able to interchange the ordering of the flags. As of now i have to pass ```s``` after which if i pass ```d``` the code works fine. Otherwise, the code leads to an invalid option error and eventually segmentation fault. Is there a way i can avoid this? – nidHi Nov 29 '17 at 05:22

1 Answers1

1

You need to store optarg returned for flag d in a temporary variable, and use it after the loop exits to set either disk or datastore:

char *temp_disk_or_datastore;
while((c = getopt_long(argc, argv, "s:d:",
                      long_options, &option_index))!= -1) {
    ind = optind;
    switch(c) {
        case 's':
            optionStore = true;
            store = strdup(optarg);
            break;
        case 'd':
            temp_disk_or_datastore = strdup(optarg);       
            break;
        default:
            exit(-1);
    }
}
if (store == NULL) {
    printf("Missing storage option");
    exit(-1);
}
if(strcmp(store,"datastore") == 0){
    printf("In datastore\n");
    datastore = temp_disk_or_datastore;
}
else if(strcmp(store,"disk") == 0){
    printf("In disk\n");
    disk = temp_disk_or_datastore;
}     
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • I faced a small issue with this solution. When I provide the flags in the command line, I am not able to interchange the ordering of the flags. As of now i have to pass ```s``` after which if i pass ```d``` the code works fine. Otherwise, the code leads to an ```invalid option``` error and eventually ```segmentation fault```. Is there a way i can avoid this? – nidHi Nov 29 '17 at 04:59
  • @nidHi I didn't realize `getopt_long` keeps state statically. I rewrote the answer to account for that. – Sergey Kalinichenko Nov 29 '17 at 12:50