PageRenderTime 71ms CodeModel.GetById 48ms app.highlight 7ms RepoModel.GetById 14ms app.codeStats 0ms

/Python/getopt.c

http://unladen-swallow.googlecode.com/
C | 127 lines | 73 code | 22 blank | 32 comment | 36 complexity | f6b958155aa3dd4bf3838c2869fcb478 MD5 | raw file
  1/*---------------------------------------------------------------------------*
  2 * <RCS keywords>
  3 *
  4 * C++ Library
  5 *
  6 * Copyright 1992-1994, David Gottner
  7 *
  8 *                    All Rights Reserved
  9 *
 10 * Permission to use, copy, modify, and distribute this software and its 
 11 * documentation for any purpose and without fee is hereby granted, 
 12 * provided that the above copyright notice, this permission notice and
 13 * the following disclaimer notice appear unmodified in all copies.
 14 *
 15 * I DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL I
 17 * BE LIABLE FOR ANY SPECIAL, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY
 18 * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA, OR PROFITS, WHETHER
 19 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
 20 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 21 *
 22 * Nevertheless, I would like to know about bugs in this library or
 23 * suggestions for improvment.  Send bug reports and feedback to
 24 * davegottner@delphi.com.
 25 *---------------------------------------------------------------------------*/
 26
 27/* Modified to support --help and --version, as well as /? on Windows
 28 * by Georg Brandl. */
 29
 30#include <stdio.h>
 31#include <string.h>
 32
 33#ifdef __cplusplus
 34extern "C" {
 35#endif
 36
 37int _PyOS_opterr = 1;          /* generate error messages */
 38int _PyOS_optind = 1;          /* index into argv array   */
 39char *_PyOS_optarg = NULL;     /* optional argument       */
 40
 41int _PyOS_GetOpt(int argc, char **argv, char *optstring)
 42{
 43	static char *opt_ptr = "";
 44	char *ptr;
 45	int option;
 46
 47	if (*opt_ptr == '\0') {
 48
 49		if (_PyOS_optind >= argc)
 50			return -1;
 51#ifdef MS_WINDOWS
 52		else if (strcmp(argv[_PyOS_optind], "/?") == 0) {
 53			++_PyOS_optind;
 54			return 'h';
 55		}
 56#endif
 57
 58		else if (argv[_PyOS_optind][0] != '-' ||
 59		         argv[_PyOS_optind][1] == '\0' /* lone dash */ )
 60			return -1;
 61
 62		else if (strcmp(argv[_PyOS_optind], "--") == 0) {
 63			++_PyOS_optind;
 64			return -1;
 65		}
 66
 67		else if (strcmp(argv[_PyOS_optind], "--help") == 0) {
 68			++_PyOS_optind;
 69			return 'h';
 70		}
 71
 72		else if (strcmp(argv[_PyOS_optind], "--version") == 0) {
 73			++_PyOS_optind;
 74			return 'V';
 75		}
 76
 77		opt_ptr = &argv[_PyOS_optind++][1];
 78	}
 79
 80	if ( (option = *opt_ptr++) == '\0')
 81		return -1;
 82
 83	if (option == 'J') {
 84		fprintf(stderr, "-J is reserved for Jython\n");
 85		return '_';
 86	}
 87
 88	if ((ptr = strchr(optstring, option)) == NULL) {
 89		if (_PyOS_opterr)
 90			fprintf(stderr, "Unknown option: -%c\n", option);
 91
 92		return '_';
 93	}
 94
 95	/* The * modifier says that this option _can_ take an argument, but
 96	   not necessarily (unlike :, which requires the argument). If no
 97	   argument was passed, _PyOS_optarg will be \0. The * modifier
 98	   requires the option's argument to be cuddled up to the flag, for
 99	   example: -O2 is legal, -O 2 is not. */
100	if (*(ptr + 1) == '*') {
101		_PyOS_optarg = opt_ptr;
102		opt_ptr = "";
103	} else if (*(ptr + 1) == ':') {
104		if (*opt_ptr != '\0') {
105			_PyOS_optarg  = opt_ptr;
106			opt_ptr = "";
107		}
108
109		else {
110			if (_PyOS_optind >= argc) {
111				if (_PyOS_opterr)
112					fprintf(stderr,
113			    "Argument expected for the -%c option\n", option);
114				return '_';
115			}
116
117			_PyOS_optarg = argv[_PyOS_optind++];
118		}
119	}
120
121	return option;
122}
123
124#ifdef __cplusplus
125}
126#endif
127