Here is another way of processing them, especially if you are expecting specific arguments (code is from C, but c++ works the same as do most language). This code snippet allows for tacked arguments (i.e. the -al in ls -al) it is handled very similarly in bash scripting as well, but some different stuff has to be done.
previous code taken from svr_tty.c provided here if you have any more questionsCode:while ((argc > 1) && (argv[1][0] == '-')) { switch (argv[1][1]) { case '?': case 'h': usage(0); case 'D': DebugFileName = argv[2]; argc--; argv++; /* fall thru */ case 'd': Debugging = 1; break; case 'o': ConfigType = CONFIG_OLD; break; case 'n': ConfigType = CONFIG_NEW; break; case 'B': daemonize = 1; break; case 'F': daemonize = 0; break; case 'f': NetworkOnDemand = 1; break; case 'm': NetworkDrop = 1; break; case 'p': ConfigType = CONFIG_ARG; if (ParseConfig(0, argv[2]) < 0) { fprintf(stderr, "%s: Invalid port configuration\n", whoami); usage(1); } break; case 'v': printf("Version: %s %s\n", VERSION, rcsid); exit(0); case 'k': Keepalives = 1; break; case 't': n = sscanf(argv[2], "%d,%d", &SocketTimeout, &SocketWaitTime); if ((n != 1) && (n != 2)) { fprintf(stderr, "%s: Invalid timeout specification\n", whoami); usage(1); } if (SocketTimeout < 0 || SocketWaitTime < 0) { fprintf(stderr, "%s: Invalid time specification\n", whoami); usage(1); } argc--; argv++; break; default: fprintf(stderr, "%s: Invalid option -- '%c'\n", whoami, argv[1][1]); usage(1); } argc--; argv++; }
Cheers,




Reply With Quote