On 16/05/10 02:04, Ian wrote:
Incidentally, this is such a common requirement ISTR there are some library functions that do these tests for you but I cannot for the life of me remember where they are!
getopt is the 'normal' way of parsing command line options in C, although I rarely use it for less than 4 options (too much pain just to avoid a few extra lines, and not posix): http://refspecs.freestandards.org/LSB_1.2.0/gLSB/libutil-getopt-3.html I typically write: #include <string.h> // For strncmp() etc. ... main (int argc, char **argv) { int arg; char *str_arg = "default"; ... for (arg=1; argc; arg++) { if (strncmp(argv[arg], "-h", 2)==0) { // They need help :) return usage(); } else if (strncmp(argv[arg], "-o", 2)==0) { str_arg = argv[++arg]; } else if (strncmp(argv[arg], "-x", 2)==0) { ... } else { return usage(); } } ... } Using strncmp() instead of strcmp() means this supporta shorter options identified by their unique prefix parts (eg: -o[ption] -x[tend] etc.) HTH, Phil.