Parsing the Command Line II

When processing the main() function’s arguments, you must keep in mind the possibilities. Options are, after all, options. They might be there, they might not.

In last week’s Lesson, I presented code that required one specific option. That’s highly unusual. Instead, the option would be, well, optional. The following code is a more realistic representation of how that single argument would be processed:

#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[])
{
    if(argc >= 2)
    {
        if( strcmp(argv[1],"hello")==0 )
        {
            puts("Valid argument specified");
            return(0);
        }
        else
        {
            fprintf(stderr,"Invalid argument specified\n");
            return(1);
        }
    }
    puts("No arguments specified, which is cool");
    return(0);
}

When a single argument is present, the argc value is always 2 —. That test is made at Line 6 above. If the test is false, then no arguments are present and the entire command line parsing code chunk is skipped.

The single valid option is the word hello, which is tested for at Line 8. When that specific option isn’t available, an error message is sent to stderr and the program terminates.

Any additional arguments aren’t required, so they’re ignored. The code could test for that condition, and either quit or simply display an error message such as, “Superfluous arguments ignored.”

In the following code, two arguments are required. They can be specified in any order, but both options must be present.

#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[])
{
    int x,first,second;

    if(argc != 3)
    {
        fprintf(stderr,"Both arguments must be specified\n");
        return(1);
    }

    for(x=1;x<3;x++)
    {
        if(strcmp(argv[x],"one")==0)
            first = 1;
        if(strcmp(argv[x],"two")==0)
            second = 1;
    }

    if( first && second )
        puts("Both arguments specified. Thank you!");
    else
    {
        if(!first)
            fprintf(stderr,"Argument 'one' is missing\n");
        if(!second)
            fprintf(stderr,"Argument 'two' is missing\n");
    }

    return(0);
}

The code is pretty strict: If both arguments aren’t detected at Line 8, an error message is displayed and the program stops.

At Line 14 the code cycles between available command line arguments. Each one is compared with the valid choice of either one or two. When a match is found, variables first or second are set accordingly. That way the program keeps track of the arguments internally.

Finally, the if-else test starting at Line 22 confirms that the options were set. If not, error messages are output. The code doesn’t stop at that point, although it could.

This code could easily be modified to provide for additional arguments. The key is to loop through all available arguments and compare them with valid options. I’ll cover that type of command line parsing in next week’s Lesson.

Leave a Reply