blob: af5b03c4f42eb53c0937d5b09cdb05220ab225ae [file] [log] [blame]
Guido van Rossum871b8051994-04-28 12:33:58 +00001/*---------------------------------------------------------------------------*
2 * <RCS keywords>
3 *
4 * C++ Library
5 *
6 * Copyright 1992-1994, David Gottner
7 *
8 * All Rights Reserved
9 *
Antoine Pitrouc83ea132010-05-09 14:46:46 +000010 * Permission to use, copy, modify, and distribute this software and its
11 * documentation for any purpose and without fee is hereby granted,
Guido van Rossum871b8051994-04-28 12:33:58 +000012 * 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 *---------------------------------------------------------------------------*/
Guido van Rossum2508ade1994-04-14 14:08:22 +000026
Georg Brandl9dceedb2006-07-12 15:31:17 +000027/* Modified to support --help and --version, as well as /? on Windows
28 * by Georg Brandl. */
29
Guido van Rossum2508ade1994-04-14 14:08:22 +000030#include <stdio.h>
31#include <string.h>
32
Anthony Baxterac6bd462006-04-13 02:06:09 +000033#ifdef __cplusplus
34extern "C" {
35#endif
36
Thomas Wouters2cffc7d2000-11-03 08:18:37 +000037int _PyOS_opterr = 1; /* generate error messages */
38int _PyOS_optind = 1; /* index into argv array */
39char *_PyOS_optarg = NULL; /* optional argument */
Antoine Pitroucc3fa882012-02-21 20:42:48 +010040static char *opt_ptr = "";
41
42void _PyOS_ResetGetOpt(void)
43{
Ezio Melottiec6486d2012-11-23 18:46:11 +020044 _PyOS_opterr = 1;
Antoine Pitroucc3fa882012-02-21 20:42:48 +010045 _PyOS_optind = 1;
46 _PyOS_optarg = NULL;
47 opt_ptr = "";
48}
Guido van Rossum2508ade1994-04-14 14:08:22 +000049
Thomas Wouters2cffc7d2000-11-03 08:18:37 +000050int _PyOS_GetOpt(int argc, char **argv, char *optstring)
Guido van Rossum2508ade1994-04-14 14:08:22 +000051{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000052 char *ptr;
53 int option;
Guido van Rossum2508ade1994-04-14 14:08:22 +000054
Antoine Pitrouc83ea132010-05-09 14:46:46 +000055 if (*opt_ptr == '\0') {
Guido van Rossum2508ade1994-04-14 14:08:22 +000056
Antoine Pitrouc83ea132010-05-09 14:46:46 +000057 if (_PyOS_optind >= argc)
58 return -1;
Georg Brandl9dceedb2006-07-12 15:31:17 +000059#ifdef MS_WINDOWS
Antoine Pitrouc83ea132010-05-09 14:46:46 +000060 else if (strcmp(argv[_PyOS_optind], "/?") == 0) {
61 ++_PyOS_optind;
62 return 'h';
63 }
Georg Brandl9dceedb2006-07-12 15:31:17 +000064#endif
65
Antoine Pitrouc83ea132010-05-09 14:46:46 +000066 else if (argv[_PyOS_optind][0] != '-' ||
67 argv[_PyOS_optind][1] == '\0' /* lone dash */ )
68 return -1;
Guido van Rossum2508ade1994-04-14 14:08:22 +000069
Antoine Pitrouc83ea132010-05-09 14:46:46 +000070 else if (strcmp(argv[_PyOS_optind], "--") == 0) {
71 ++_PyOS_optind;
72 return -1;
73 }
Guido van Rossum2508ade1994-04-14 14:08:22 +000074
Antoine Pitrouc83ea132010-05-09 14:46:46 +000075 else if (strcmp(argv[_PyOS_optind], "--help") == 0) {
76 ++_PyOS_optind;
77 return 'h';
78 }
Georg Brandl9dceedb2006-07-12 15:31:17 +000079
Antoine Pitrouc83ea132010-05-09 14:46:46 +000080 else if (strcmp(argv[_PyOS_optind], "--version") == 0) {
81 ++_PyOS_optind;
82 return 'V';
83 }
Georg Brandl9dceedb2006-07-12 15:31:17 +000084
85
Antoine Pitrouc83ea132010-05-09 14:46:46 +000086 opt_ptr = &argv[_PyOS_optind++][1];
87 }
Guido van Rossum2508ade1994-04-14 14:08:22 +000088
Ezio Melottidb5947f2012-11-18 13:46:38 +020089 if ((option = *opt_ptr++) == '\0')
Antoine Pitrouc83ea132010-05-09 14:46:46 +000090 return -1;
Christian Heimes7a98d272008-04-12 13:03:03 +000091
Antoine Pitrouc83ea132010-05-09 14:46:46 +000092 if (option == 'J') {
Ezio Melottidb5947f2012-11-18 13:46:38 +020093 if (_PyOS_opterr)
94 fprintf(stderr, "-J is reserved for Jython\n");
Antoine Pitrouc83ea132010-05-09 14:46:46 +000095 return '_';
96 }
Christian Heimes7a98d272008-04-12 13:03:03 +000097
Antoine Pitrouc83ea132010-05-09 14:46:46 +000098 if (option == 'X') {
Ezio Melottidb5947f2012-11-18 13:46:38 +020099 if (_PyOS_opterr)
100 fprintf(stderr,
101 "-X is reserved for implementation-specific arguments\n");
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000102 return '_';
103 }
Christian Heimes7a98d272008-04-12 13:03:03 +0000104
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000105 if ((ptr = strchr(optstring, option)) == NULL) {
106 if (_PyOS_opterr)
107 fprintf(stderr, "Unknown option: -%c\n", option);
Guido van Rossum2508ade1994-04-14 14:08:22 +0000108
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000109 return '_';
110 }
Guido van Rossum2508ade1994-04-14 14:08:22 +0000111
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000112 if (*(ptr + 1) == ':') {
113 if (*opt_ptr != '\0') {
114 _PyOS_optarg = opt_ptr;
115 opt_ptr = "";
116 }
Guido van Rossum2508ade1994-04-14 14:08:22 +0000117
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000118 else {
119 if (_PyOS_optind >= argc) {
120 if (_PyOS_opterr)
121 fprintf(stderr,
Ezio Melottidb5947f2012-11-18 13:46:38 +0200122 "Argument expected for the -%c option\n", option);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000123 return '_';
124 }
Guido van Rossum2508ade1994-04-14 14:08:22 +0000125
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000126 _PyOS_optarg = argv[_PyOS_optind++];
127 }
128 }
Guido van Rossum2508ade1994-04-14 14:08:22 +0000129
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000130 return option;
Guido van Rossum2508ade1994-04-14 14:08:22 +0000131}
Anthony Baxterac6bd462006-04-13 02:06:09 +0000132
133#ifdef __cplusplus
134}
135#endif
136