1

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.

mindless.panda
  • 4,014
  • 4
  • 35
  • 57
Rohit Deshmukh
  • 381
  • 2
  • 8
  • 21
  • 1
    if you don't put the text "this is a string" between double quotes the program will interpret every word as a single argument – juergen d Dec 04 '11 at 11:02
  • Have a look at #include http://www.crasseux.com/books/ctutorial/argp-example.html also http://stackoverflow.com/questions/7677562/whats-the-difference-between-argp-and-getopt – Captain Giraffe Dec 04 '11 at 11:03

4 Answers4

2

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
MestreLion
  • 12,698
  • 8
  • 66
  • 57
1
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");
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
0

Change int ca= 0 to int ca= 1

Because argv[0] is the name of your executable

Captain Giraffe
  • 14,407
  • 6
  • 39
  • 67
apprentice
  • 64
  • 2
0

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);
wildplasser
  • 43,142
  • 8
  • 66
  • 109