update peek documentation to implementation
[python/dscho.git] / Python / getopt.c
blobda9341f404d18a6a4b8eed7f60479f619b344557
1 /*---------------------------------------------------------------------------*
2 * <RCS keywords>
4 * C++ Library
6 * Copyright 1992-1994, David Gottner
8 * All Rights Reserved
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.
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.
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 *---------------------------------------------------------------------------*/
27 /* Modified to support --help and --version, as well as /? on Windows
28 * by Georg Brandl. */
30 #include <Python.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <wchar.h>
34 #include <pygetopt.h>
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
40 int _PyOS_opterr = 1; /* generate error messages */
41 int _PyOS_optind = 1; /* index into argv array */
42 wchar_t *_PyOS_optarg = NULL; /* optional argument */
44 int _PyOS_GetOpt(int argc, wchar_t **argv, wchar_t *optstring)
46 static wchar_t *opt_ptr = L"";
47 wchar_t *ptr;
48 wchar_t option;
50 if (*opt_ptr == '\0') {
52 if (_PyOS_optind >= argc)
53 return -1;
54 #ifdef MS_WINDOWS
55 else if (wcscmp(argv[_PyOS_optind], L"/?") == 0) {
56 ++_PyOS_optind;
57 return 'h';
59 #endif
61 else if (argv[_PyOS_optind][0] != L'-' ||
62 argv[_PyOS_optind][1] == L'\0' /* lone dash */ )
63 return -1;
65 else if (wcscmp(argv[_PyOS_optind], L"--") == 0) {
66 ++_PyOS_optind;
67 return -1;
70 else if (wcscmp(argv[_PyOS_optind], L"--help") == 0) {
71 ++_PyOS_optind;
72 return 'h';
75 else if (wcscmp(argv[_PyOS_optind], L"--version") == 0) {
76 ++_PyOS_optind;
77 return 'V';
81 opt_ptr = &argv[_PyOS_optind++][1];
84 if ( (option = *opt_ptr++) == L'\0')
85 return -1;
87 if (option == 'J') {
88 fprintf(stderr, "-J is reserved for Jython\n");
89 return '_';
92 if (option == 'X') {
93 fprintf(stderr,
94 "-X is reserved for implementation-specific arguments\n");
95 return '_';
98 if ((ptr = wcschr(optstring, option)) == NULL) {
99 if (_PyOS_opterr)
100 fprintf(stderr, "Unknown option: -%c\n", (char)option);
102 return '_';
105 if (*(ptr + 1) == L':') {
106 if (*opt_ptr != L'\0') {
107 _PyOS_optarg = opt_ptr;
108 opt_ptr = L"";
111 else {
112 if (_PyOS_optind >= argc) {
113 if (_PyOS_opterr)
114 fprintf(stderr,
115 "Argument expected for the -%c option\n", (char)option);
116 return '_';
119 _PyOS_optarg = argv[_PyOS_optind++];
123 return option;
126 #ifdef __cplusplus
128 #endif