Rohit Deshmukh
Rohit Deshmukh

Reputation: 391

Properly parse command line arguments in C

What I am trying to do is take in command line arguments and change some variables according to the arguments. I have attached a chunk of my code because the whole code is ~400 lines.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]) {

    char somestring[500];
    int ca=0;
    if (argc==1) //if no arguments are specified use defaults
    {
    }
    else
    {
        while(ca<argc)
        {
               ca++
            if(strcmp(argv[ca],"-f")==0)
            {
                printf("This works");
                ca++; 
                if(strcmp(argv[ca],"red")==0){
                    printf("this will print red\n");
                }
                else{
                    printf("invalid color");
                }
            }
            if(strcmp(argv[ca),"")==0)
            {
                printf("invalid argument");
            }
            else {
                strcat(somestring,argv[ca]);
            }
        }
        printf("%s",somestring);
    }
}

If the user inputs:

./foobar -f red this is a string

the program should print:

"this will print red this is a string"

If the user inputs:

./foobar -f red

the program should print "invalid number of command line arguments".

What is the easiest way to do this? I have tried tons of possibilities with no luck. Varying number of arguments is the main problem for me (also I have more than 5 options e.g..-f -b -h -w -e)

Help would much appreciated. I can add my whole code if you want.

Upvotes: 1

Views: 14226

Answers (4)

MestreLion
MestreLion

Reputation: 13726

The proper way is to use one of the many existing parser libraries instead of manually parse yourself. It's easier, more powerful, and saves you the trouble of reinventing the wheel.

GNU libc manual suggests a few libraries, depending on how fancy/standard you want to be: http://www.gnu.org/software/libc/manual/html_node/Parsing-Program-Arguments.html

  • getopt: as mentioned by another answer
  • argp: my usual choice
  • suboptions: for complex solutions

Upvotes: 2

BLUEPIXY
BLUEPIXY

Reputation: 40155

char somestring[500]="";//need initialize

    while(++ca<argc){//increment before condition test
        if(strcmp(argv[ca],"-f")==0){

            if(ca < argc && strcmp(argv[ca],"red")==0){//need ca check

        if(ca == argc){//bad strcmp(argv[ca],"")
            printf("invalid argument");

Upvotes: 1

wildplasser
wildplasser

Reputation: 44250

Things will get much clearer if you use a for-loop instead of the silly "else while" construct:

  for(ca=1; ca < argc ; ca++)
  {
      if(!strcmp(argv[ca],"-f"))
      {
         printf("This works");
         ca++; /* need to test if ca can be incremented */
         if(!strcmp(argv[ca],"red")){
             printf("this will print red\n");
         }
         else{
             printf("invalid color");
         }
      }
      else if(!strcmp(argv[ca],""))
      {
         printf("invalid argument");
      }
      else{
          strcat(somestring,argv[ca]);
      }
  }
  printf("%s",somestring);

Upvotes: 0

apprentice
apprentice

Reputation: 64

Change int ca= 0 to int ca= 1

Because argv[0] is the name of your executable

Upvotes: 0

Related Questions