Search code examples
clinuxgetopt

Cant seem to access multiple options using getopt() for C


TL;DR - My issue is that I can't seem to get both options to work. Only '-n' is working. I also want '-h' to work.

I am trying to create a program that essentially prints out the last few letters of '.txt' or '.log' file. However, I am running into an issue using getopt(). I am trying to access the different cases using the command line, but I can only access the first case

I have already tried include the colon (:) after "nLh" however it ends up outputting a "segmentation fault" (core dumped)" error.

Ex1: ./print.out -h (fails)

What I pass in

./print.out -h

Expected output

Usage: ./print.out -n

Actual output

Segmentation fault (core dump)

Ex2: ./print.out -n 60 (Successful)

What I pass in

./print.out -n 60

Expected output

Random text file from a txt file ... Random text file from a txt file

Actual output

Random text file from a txt file ... Random text file from a txt file

    if(argc >1)
    {   
        while ((option =getopt(argc,argv,"nLh"))!=-1)
        {
            switch (option)
            {
                case 'n':

                    if( isExtensionTXTorLog && charactersRead >0)
                    {
                    }

                    else if( argc == 3 && !isExtensionTXTorLog)
                    {   
                    }
                    else
                    {
                        exit(2);
                    }
                    break;
                case 'L':
                    break;
                case 'h':
                    printUsage();
                    break;
                case '?':
                    exit(0);
                    break;
                default:
                    break;
            }
        }

    }
    else
    {
        accessDefault(buffer);
        return 0;
    }

Solution

  • You're using optind in the wrong way. optind is used to get non-options argument after parsing all the options. To parse option with argument use n:, then read optarg variable

    Take look at this minimal example:

    #include <getopt.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
        char option;
        int n_param;
    
        while ((option =getopt(argc,argv,"n:h"))!=-1)
        {
            //Variable initialization
            switch (option)
            {
                case 'n':
                    n_param = atoi(optarg);
                    printf("Param N: %d\n", n_param);
                    break;
                case 'h':
                    printf("Help\n");
                    exit(0);
                    break;
                case '?':
                    printf("Unrecognized option\n");
                    exit(0);
                    break;
                default:
                    break;
            }
        }
    
        for (int index = optind; index < argc; index++)
            printf ("Non-option argument %s\n", argv[index]);
    
        return 0;
    }
    

    Example:

    ./a.out ARG1 -n 50 ARG2  
    

    Output:

    Param N: 50
    Non-option argument ARG1
    Non-option argument ARG2