/*---------------------------------------------------------------------- | This program is used to show how integer, double, and string | parameters can be retrieved from the command-line using | getopt() | | Usage: %s [-i int] [-d double] [-f filename] |----------------------------------------------------------------------*/ #include #include #include #include #define MAX_LINE 120 void parseCommandLine(int argc, char *argv[], int *myInt, double *myDouble, char **myString); void printUsage(char *argv[]); /*---------------------------------------------------------------------- | |----------------------------------------------------------------------*/ int main(int argc, char *argv[]) { int anInt = -1; double aDouble = -1.0; char filename[MAX_LINE]; sprintf(filename, "initialized"); parseCommandLine(argc, argv, &anInt, &aDouble, (char **) &filename); printf("Current values: \n\n"); printf("\tanInt = %d\n", anInt); printf("\taDouble = %.3f\n", aDouble); printf("\tFile = [%s]\n]n", filename); return 0; } /*---------------------------------------------------------------------- | Output: | 1. myInt: optional integer argument | 2. myDouble: optional double (floating point) argument | 3. myString: optional pathname argument |----------------------------------------------------------------------*/ void parseCommandLine(int argc, char *argv[], int *myInt, double *myDouble, char **myString) { extern char *optarg; int c; while ((c = getopt(argc, argv, "i:d:f:")) != -1) { switch (c) { case 'i': *myInt = atoi(optarg); break; case 'd': *myDouble = atof(optarg); break; case 'f': strncpy( (char *) myString, optarg, MAX_LINE); break; default : printUsage(argv); exit(1); } } } /*---------------------------------------------------------------------- | printUsage: output the USAGE for using this application. |----------------------------------------------------------------------*/ void printUsage(char *argv[]) { printf("Usage: %s [-i int] [-d double] [-f filename]\n", argv[0]); printf("\n"); printf("This program is used to show how integer, double, and string\n"); printf("parameters can be retrieved from the command-line using getopt()\n"); fflush(stdout); }