I'm trying to figure out how to parse options such as -h out of the arguments passed on the command line when the program is called. Here is my code so far. I compiled it with gcc -Wall -o options options.c and got no warnings or errors. Although, when I run it, it doesn't do anything. I have tried ./options -h as well as ./options -help and ./options help and it seems to find nothing. Oh and I'm running Ubuntu linux on an x86.

/**************************************************************************
 *
 * Purpose: To practice programming options
 *          
 *************************************************************************/

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

int main(int argc, char *argv[])
{
  int count;
  char * strhold;               // Used to hold option values
  int len = 0;                  // Used to hold arguments' string lengths

  for(count=1; count == argc; count++)
    {
      strcpy(strhold, argv[count]);
      len = strlen(strhold);

          if((strhold[0] == '-') && (strhold[1] == 'h'))
            {
              printf("\nPurpose: To practice programming options\n");
              exit(0);
            } else if((strhold[0] == '-') && (strhold[1] != 'h'))
                {
                  printf("Error: Invalid option - %s\n", strhold);
                  exit(1);
                }

    }

 return 0;

}

Anybody know what's going on?

Thanks
diode

Another batch of code I whipped up. Surprisingly, gcc doesn't seem to mind the wierd declaration of the char array...and this program was able to read arguments on the command line, but was unable to distinguish between an argument and an option (such as -h or -help); the for loop never executes.

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

int main(int argc, char *argv[])
{

  int len = 0;                  // Used to hold arguments' string lengths
  char strhold[strlen(argv[1])];                // Used to hold option values
  int count = 0;

  strcpy(strhold, argv[1]);
  printf("The option passed was %s\n", strhold);

  for(count=1; count == argc; count++)
    {
      printf("Inside the for loop\n");
      strcpy(strhold, argv[count]);
      len = strlen(strhold);

          if((strhold[0] == '-') && (strhold[1] == 'h'))
            {
              printf("\nPurpose: To practice programming options\n");
              exit(0);
            } else if((strhold[0] == '-') && (strhold[1] != 'h'))
                {
                  printf("Error: Invalid option - %s\n", strhold);
                  exit(1);
                }

    }


  return 0;
}

diode

I solved it. argc counts the program call as the first argument, which is argument 0. If there is an argument supplied to the program on the command line, that is argument 1. I didn't take this into consideration when I was programming. I went too fast and didn't think it through.

It works now. For any interested party, here is my code which you can play with and edit to your own liking.

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

int main(int argc, char *argv[])
{

  int len = 0;                  // Used to hold arguments' string lengths
  char strhold[strlen(argv[1])];                // Used to hold option values
  int count = 1;

  strcpy(strhold, argv[1]);
  printf("The option passed was %s\n", strhold);

  while(count <= argc-1)
    {
      printf("Inside the while loop%d\n", argc);
      strcpy(strhold, argv[count]);
      len = strlen(strhold);

          if((strhold[0] == '-') && (strhold[1] == 'h'))
            {
              printf("\nPurpose: To practice programming options\n");
              exit(0);
            } else if((strhold[0] == '-') && (strhold[1] != 'h'))
                {
                  printf("Error: Invalid option - %s\n", strhold);
                  exit(1);
                }

      count++;

    }


  return 0;
}

diode

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.