char *setopt(char opt);
bool checkopt(char *head, int *table);
bool CHECKOPT(char *head, int_1, int_2, ..., int_n);
The definition of correct command line option combinations is done by a binary truth table that is more easy to maintain then combinations of logical and/or expressions.
Example:
#define USAGE "Usage: mycmd [ -l | -m ][ -z ][ -n | -f filename ]\n" int opt; while (( opt = getopt(argc, argv, "f:nlmz")) != -1) { switch (opt) { case 'f': file = true; filename = strdup(optarg); break; case 'n': file = false; break; case 'l': low = true; break; case 'm': medium = true; break; case 'z': zero = true; break; case '?': printf(USAGE); return 4; break; } setopt(opt); } for (int i=optind; i<argc; i++){ printf(USAGE); return 4; }
The header is a string with all options to be checked in the truth table.
In the truth table (=integer array) all allowed (good) option combinations are set by setting the related bit in the integer value list in the same order as the options given in the header string.
The last entry in the truth table needs to be set to -1.
Example:
int table[] = { 0b00000000, 0b00000100, 0b00001000, 0b00000001, 0b00000101, 0b00001001, 0b00000010, 0b00000110, 0b00001010, -1 }; if ( !checkopt("mlnf", table) ){ printf(USAGE); return 4; };
The header is a string with all options to be checked in the truth table.
In the truth table (=integer array) all allowed (good) option combinations are set by setting the related bit in the integer value list in the same order as the options given in the header string.
The CHECKOPT macro is suited to more conveniently define the truth table.
Example:
if ( !CHECKOPT( "mlnf", 0b00000000, 0b00000100, 0b00001000, 0b00000001, 0b00000101, 0b00001001, 0b00000010, 0b00000110, 0b00001010 )){ printf(USAGE); return 4; };
This is free software; see edrc/doc/COPYING for copying conditions. There is ABSOLUTELY NO WARRANTY; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.