Guido van Rossum | 871b805 | 1994-04-28 12:33:58 +0000 | [diff] [blame] | 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 | *---------------------------------------------------------------------------*/ |
Guido van Rossum | 2508ade | 1994-04-14 14:08:22 +0000 | [diff] [blame] | 26 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 27 | /* Modified to support --help and --version, as well as /? on Windows |
| 28 | * by Georg Brandl. */ |
| 29 | |
Guido van Rossum | 2508ade | 1994-04-14 14:08:22 +0000 | [diff] [blame] | 30 | #include <stdio.h> |
| 31 | #include <string.h> |
| 32 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 33 | #ifdef __cplusplus |
| 34 | extern "C" { |
| 35 | #endif |
| 36 | |
Thomas Wouters | 2cffc7d | 2000-11-03 08:18:37 +0000 | [diff] [blame] | 37 | int _PyOS_opterr = 1; /* generate error messages */ |
| 38 | int _PyOS_optind = 1; /* index into argv array */ |
| 39 | char *_PyOS_optarg = NULL; /* optional argument */ |
Guido van Rossum | 2508ade | 1994-04-14 14:08:22 +0000 | [diff] [blame] | 40 | |
Thomas Wouters | 2cffc7d | 2000-11-03 08:18:37 +0000 | [diff] [blame] | 41 | int _PyOS_GetOpt(int argc, char **argv, char *optstring) |
Guido van Rossum | 2508ade | 1994-04-14 14:08:22 +0000 | [diff] [blame] | 42 | { |
Thomas Wouters | 2cffc7d | 2000-11-03 08:18:37 +0000 | [diff] [blame] | 43 | static char *opt_ptr = ""; |
| 44 | char *ptr; |
| 45 | int option; |
Guido van Rossum | 2508ade | 1994-04-14 14:08:22 +0000 | [diff] [blame] | 46 | |
Guido van Rossum | 871b805 | 1994-04-28 12:33:58 +0000 | [diff] [blame] | 47 | if (*opt_ptr == '\0') { |
Guido van Rossum | 2508ade | 1994-04-14 14:08:22 +0000 | [diff] [blame] | 48 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 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 */ ) |
Guido van Rossum | 871b805 | 1994-04-28 12:33:58 +0000 | [diff] [blame] | 60 | return -1; |
Guido van Rossum | 2508ade | 1994-04-14 14:08:22 +0000 | [diff] [blame] | 61 | |
Thomas Wouters | 2cffc7d | 2000-11-03 08:18:37 +0000 | [diff] [blame] | 62 | else if (strcmp(argv[_PyOS_optind], "--") == 0) { |
| 63 | ++_PyOS_optind; |
Guido van Rossum | 871b805 | 1994-04-28 12:33:58 +0000 | [diff] [blame] | 64 | return -1; |
| 65 | } |
Guido van Rossum | 2508ade | 1994-04-14 14:08:22 +0000 | [diff] [blame] | 66 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 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 | |
Thomas Wouters | 2cffc7d | 2000-11-03 08:18:37 +0000 | [diff] [blame] | 78 | opt_ptr = &argv[_PyOS_optind++][1]; |
Guido van Rossum | 871b805 | 1994-04-28 12:33:58 +0000 | [diff] [blame] | 79 | } |
Guido van Rossum | 2508ade | 1994-04-14 14:08:22 +0000 | [diff] [blame] | 80 | |
Guido van Rossum | b4102bf | 1997-09-30 22:00:13 +0000 | [diff] [blame] | 81 | if ( (option = *opt_ptr++) == '\0') |
Thomas Wouters | 2cffc7d | 2000-11-03 08:18:37 +0000 | [diff] [blame] | 82 | return -1; |
Guido van Rossum | b4102bf | 1997-09-30 22:00:13 +0000 | [diff] [blame] | 83 | |
| 84 | if ((ptr = strchr(optstring, option)) == NULL) { |
Thomas Wouters | 2cffc7d | 2000-11-03 08:18:37 +0000 | [diff] [blame] | 85 | if (_PyOS_opterr) |
Guido van Rossum | 871b805 | 1994-04-28 12:33:58 +0000 | [diff] [blame] | 86 | fprintf(stderr, "Unknown option: -%c\n", option); |
Guido van Rossum | 2508ade | 1994-04-14 14:08:22 +0000 | [diff] [blame] | 87 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 88 | return '_'; |
Guido van Rossum | 871b805 | 1994-04-28 12:33:58 +0000 | [diff] [blame] | 89 | } |
Guido van Rossum | 2508ade | 1994-04-14 14:08:22 +0000 | [diff] [blame] | 90 | |
Guido van Rossum | 871b805 | 1994-04-28 12:33:58 +0000 | [diff] [blame] | 91 | if (*(ptr + 1) == ':') { |
| 92 | if (*opt_ptr != '\0') { |
Thomas Wouters | 2cffc7d | 2000-11-03 08:18:37 +0000 | [diff] [blame] | 93 | _PyOS_optarg = opt_ptr; |
Guido van Rossum | 871b805 | 1994-04-28 12:33:58 +0000 | [diff] [blame] | 94 | opt_ptr = ""; |
| 95 | } |
Guido van Rossum | 2508ade | 1994-04-14 14:08:22 +0000 | [diff] [blame] | 96 | |
Guido van Rossum | 871b805 | 1994-04-28 12:33:58 +0000 | [diff] [blame] | 97 | else { |
Thomas Wouters | 2cffc7d | 2000-11-03 08:18:37 +0000 | [diff] [blame] | 98 | if (_PyOS_optind >= argc) { |
| 99 | if (_PyOS_opterr) |
Guido van Rossum | 871b805 | 1994-04-28 12:33:58 +0000 | [diff] [blame] | 100 | fprintf(stderr, |
| 101 | "Argument expected for the -%c option\n", option); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 102 | return '_'; |
Guido van Rossum | 871b805 | 1994-04-28 12:33:58 +0000 | [diff] [blame] | 103 | } |
Guido van Rossum | 2508ade | 1994-04-14 14:08:22 +0000 | [diff] [blame] | 104 | |
Thomas Wouters | 2cffc7d | 2000-11-03 08:18:37 +0000 | [diff] [blame] | 105 | _PyOS_optarg = argv[_PyOS_optind++]; |
Guido van Rossum | 871b805 | 1994-04-28 12:33:58 +0000 | [diff] [blame] | 106 | } |
| 107 | } |
Guido van Rossum | 2508ade | 1994-04-14 14:08:22 +0000 | [diff] [blame] | 108 | |
Guido van Rossum | 871b805 | 1994-04-28 12:33:58 +0000 | [diff] [blame] | 109 | return option; |
Guido van Rossum | 2508ade | 1994-04-14 14:08:22 +0000 | [diff] [blame] | 110 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 111 | |
| 112 | #ifdef __cplusplus |
| 113 | } |
| 114 | #endif |
| 115 | |