#include<ctype.h>
#include<stdio.h>
#include<unistd.h>

void usage(){
	fprintf(stderr, "Usage: getoptExample1 [-a] [-b] [-c] [file ...]\n");
}

// Processing options without arguments
// The only options to this are -a -b -c. 
// Options have no arguments associated with them.
int main(int argc, char **argv){

	// The following track the options we have seen
	int aflag = 0;
	int bflag = 0;
	int cflag = 0;
	int c;

	// globals include optind, optarg 
	// the string "abc" represents the only options to this command
	// try 
	// getoptExample1
	// getoptExample1 -b -a
	// getoptExample1 -a -b -c
	// getoptExample1 -a -b -c file1 file2 file3
	// getoptExample1 -x
	// getoptExample1 -a -x -b
     
	while ((c = getopt (argc, argv, "abc")) != -1){
		switch(c) {
			case 'a':
				aflag = 1;
				break;
			case 'b':
				bflag = 1;
				break;
			case 'c':
				cflag = 1;
				break;
			default:
				usage();
				return 1;	
				break;
		}
	}
	printf ("aflag = %d, bflag = %d, cflag = %d\n", aflag, bflag, cflag);

	// argv[optind], argv[optind+1], ... are the remaining (no option) command line arguments
	int index;
	for (index = optind; index < argc; index++){
		printf ("Non-option argument %s\n", argv[index]);
	}
	return 0;
}
