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.

2

4 Answers 4

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
Sign up to request clarification or add additional context in comments.

Comments

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");

Comments

0

Change int ca= 0 to int ca= 1

Because argv[0] is the name of your executable

1 Comment

sorry, I didn't copy the code correctly from my c file, check it now. thanks.
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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.