📄 getopt.cpp
字号:
/* * getopt.c : Compatability function for PC implementations * * Ported from SPRLIB by for compatibility with "m" projects by milbo 04/2004 Petaluma * Made by : W.F. Schmidt * SccsId : %Z%$RCSfile: getopt.c,v $ V%Q% $Revision: 1.6 $ $Date: 1996/10/07 06:50:52 $ */#include <stdio.h>#include <string.h>//#include <sprlib.h>void __cdecl lprintf(const char *pArgs, ...);/* * Global variables used by getopt. */char *optarg=NULL;int optind=1, opterr=1;/* * GETOPT: The following function is an implementation of the getopt * function available on UNIX machines. This little bit obsolete function * is not present on most PC systems and therefore this implementation * is added to sprlib to support the programs found in the example and tools * directory. * The manual page of this function is added here, so users on PC can read * how to use this function for their own purpose. * * BTW : My implementation is not fully compatible with the UNIX version, * in my version every option MUST be proceeded by a '-'. In the UNIX version * options with no argument can be collected into one string, starting with the * '-' character. WSC. NAME getopt, optarg, optind - get option letter from argument vector SYNOPSIS int getopt(argc, argv, optstring) int argc; char **argv; char *optstring; extern char *optarg; extern int optind, opterr; DESCRIPTION getopt() returns the next option letter in argv that matches a letter in optstring. optstring must contain the option letters the command using getopt() will recognize; if a letter is followed by a colon, the option is expected to have an argument, or group of arguments, which must be separated from it by white space. optarg is set to point to the start of the option argument on return from getopt. getopt() places in optind the argv index of the next argu- ment to be processed. optind is external and is initialized to 1 before the first call to getopt. When all options have been processed (that is, up to the first non-option argument), getopt() returns -1. The spe- cial option ``--'' may be used to delimit the end of the options; when it is encountered, -1 will be returned, and ``--'' will be skipped. DIAGNOSTICS getopt() prints an error message on the standard error and returns a question mark (?) when it encounters an option letter not included in optstring or no option-argument after an option that expects one. This error message may be dis- abled by setting opterr to 0. EXAMPLE The following code fragment shows how one might process the arguments for a command that can take the mutually exclusive options a and b, and the option o, which requires an option argument: main(argc, argv) int argc; char **argv; { int c; extern char *optarg; extern int optind; . . . while ((c = getopt(argc, argv, "abo:")) != -1) switch (c) { case 'a': if (bflg) errflg++; else aflg++; break; case 'b': if (aflg) errflg++; else bproc (); break; case 'o': ofile = optarg; break; case '?': errflg++; } if (errflg) { (void)fprintf(stderr, "usage: . . . "); exit (2); } for (; optind < argc; optind++) { if (access(argv[optind], 4)) { . . . } * */int getopt (int argc, char **argv, char *optstring){ static int LocalArgc=1; static char *LocalOpt, **LocalArgv, *OptPos; /* * If first call store arguments in local storage. * The arguments to getopt are only used the first call to getopt. */ if (optind == 1) { LocalArgv = argv; LocalArgc = argc; LocalOpt = optstring; } /* * Check if last argument is found. */ if (optind >= LocalArgc) return -1; /* * Check the length of the current argument. * This must be length 2 for options. */ if (strlen(LocalArgv[optind]) != 2) return -1; /* * Check if argument is a potential options. */ if (*(LocalArgv[optind]) != '-') return -1; /* * Check if argument is special '--' break option. */ if (*(LocalArgv[optind]+1) == '-') return -1; /* * Test if options is a valid option. */ if ((OptPos = strchr(LocalOpt, (int) (*(LocalArgv[optind]+1)))) == NULL) { if (opterr) lprintf("Illegal option -%s\n", (LocalArgv[optind]+1)); return '?'; } /* * Check if option requires argument. */ if (*(OptPos+1) == ':') { /* * Check if argument is present. */ if ((optind+1) >= LocalArgc) { if (opterr) lprintf("Illegal option \"%s\"\n", (LocalArgv[optind]+1)); return '?'; } optarg= LocalArgv[optind+1]; optind++; } optind++; return ((int) *OptPos);}/* * End of Source */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -