LCOV - code coverage report
Current view: top level - Python - getopt.c (source / functions) Hit Total Coverage
Test: CPython lcov report Lines: 54 67 80.6 %
Date: 2022-07-07 18:19:46 Functions: 2 2 100.0 %

          Line data    Source code
       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             : 
      23             : /* Modified to support --help and --version, as well as /? on Windows
      24             :  * by Georg Brandl. */
      25             : 
      26             : #include <Python.h>
      27             : #include <stdio.h>
      28             : #include <string.h>
      29             : #include <wchar.h>
      30             : #include "pycore_getopt.h"
      31             : 
      32             : #ifdef __cplusplus
      33             : extern "C" {
      34             : #endif
      35             : 
      36             : int _PyOS_opterr = 1;                 /* generate error messages */
      37             : Py_ssize_t _PyOS_optind = 1;          /* index into argv array   */
      38             : const wchar_t *_PyOS_optarg = NULL;   /* optional argument       */
      39             : 
      40             : static const wchar_t *opt_ptr = L"";
      41             : 
      42             : /* Python command line short and long options */
      43             : 
      44             : #define SHORT_OPTS L"bBc:dEhiIJm:OPqRsStuvVW:xX:?"
      45             : 
      46             : static const _PyOS_LongOption longopts[] = {
      47             :     /* name, has_arg, val (used in switch in initconfig.c) */
      48             :     {L"check-hash-based-pycs", 1, 0},
      49             :     {L"help-all", 0, 1},
      50             :     {L"help-env", 0, 2},
      51             :     {L"help-xoptions", 0, 3},
      52             :     {NULL, 0, -1},                     /* sentinel */
      53             : };
      54             : 
      55             : 
      56        8908 : void _PyOS_ResetGetOpt(void)
      57             : {
      58        8908 :     _PyOS_opterr = 1;
      59        8908 :     _PyOS_optind = 1;
      60        8908 :     _PyOS_optarg = NULL;
      61        8908 :     opt_ptr = L"";
      62        8908 : }
      63             : 
      64       29408 : int _PyOS_GetOpt(Py_ssize_t argc, wchar_t * const *argv, int *longindex)
      65             : {
      66             :     wchar_t *ptr;
      67             :     wchar_t option;
      68             : 
      69       29408 :     if (*opt_ptr == '\0') {
      70             : 
      71       26490 :         if (_PyOS_optind >= argc)
      72         190 :             return -1;
      73             : #ifdef MS_WINDOWS
      74             :         else if (wcscmp(argv[_PyOS_optind], L"/?") == 0) {
      75             :             ++_PyOS_optind;
      76             :             return 'h';
      77             :         }
      78             : #endif
      79             : 
      80       26300 :         else if (argv[_PyOS_optind][0] != L'-' ||
      81       25177 :                  argv[_PyOS_optind][1] == L'\0' /* lone dash */ )
      82        1135 :             return -1;
      83             : 
      84       25165 :         else if (wcscmp(argv[_PyOS_optind], L"--") == 0) {
      85           0 :             ++_PyOS_optind;
      86           0 :             return -1;
      87             :         }
      88             : 
      89       25165 :         else if (wcscmp(argv[_PyOS_optind], L"--help") == 0) {
      90           3 :             ++_PyOS_optind;
      91           3 :             return 'h';
      92             :         }
      93             : 
      94       25162 :         else if (wcscmp(argv[_PyOS_optind], L"--version") == 0) {
      95           3 :             ++_PyOS_optind;
      96           3 :             return 'V';
      97             :         }
      98             : 
      99       25159 :         opt_ptr = &argv[_PyOS_optind++][1];
     100             :     }
     101             : 
     102       28077 :     if ((option = *opt_ptr++) == L'\0')
     103           0 :         return -1;
     104             : 
     105       28077 :     if (option == L'-') {
     106             :         // Parse long option.
     107          23 :         if (*opt_ptr == L'\0') {
     108           0 :             if (_PyOS_opterr) {
     109           0 :                 fprintf(stderr, "expected long option\n");
     110             :             }
     111           0 :             return -1;
     112             :         }
     113          23 :         *longindex = 0;
     114             :         const _PyOS_LongOption *opt;
     115          61 :         for (opt = &longopts[*longindex]; opt->name; opt = &longopts[++(*longindex)]) {
     116          56 :             if (!wcscmp(opt->name, opt_ptr))
     117          18 :                 break;
     118             :         }
     119          23 :         if (!opt->name) {
     120           5 :             if (_PyOS_opterr) {
     121           1 :                 fprintf(stderr, "unknown option %ls\n", argv[_PyOS_optind - 1]);
     122             :             }
     123           5 :             return '_';
     124             :         }
     125          18 :         opt_ptr = L"";
     126          18 :         if (!opt->has_arg) {
     127           9 :             return opt->val;
     128             :         }
     129           9 :         if (_PyOS_optind >= argc) {
     130           0 :             if (_PyOS_opterr) {
     131           0 :                 fprintf(stderr, "Argument expected for the %ls options\n",
     132           0 :                         argv[_PyOS_optind - 1]);
     133             :             }
     134           0 :             return '_';
     135             :         }
     136           9 :         _PyOS_optarg = argv[_PyOS_optind++];
     137           9 :         return opt->val;
     138             :     }
     139             : 
     140       28054 :     if (option == 'J') {
     141           0 :         if (_PyOS_opterr) {
     142           0 :             fprintf(stderr, "-J is reserved for Jython\n");
     143             :         }
     144           0 :         return '_';
     145             :     }
     146             : 
     147       28054 :     if ((ptr = wcschr(SHORT_OPTS, option)) == NULL) {
     148          31 :         if (_PyOS_opterr) {
     149           3 :             fprintf(stderr, "Unknown option: -%c\n", (char)option);
     150             :         }
     151          31 :         return '_';
     152             :     }
     153             : 
     154       28023 :     if (*(ptr + 1) == L':') {
     155       14593 :         if (*opt_ptr != L'\0') {
     156        2965 :             _PyOS_optarg  = opt_ptr;
     157        2965 :             opt_ptr = L"";
     158             :         }
     159             : 
     160             :         else {
     161       11628 :             if (_PyOS_optind >= argc) {
     162           6 :                 if (_PyOS_opterr) {
     163           2 :                     fprintf(stderr,
     164           2 :                         "Argument expected for the -%c option\n", (char)option);
     165             :                 }
     166           6 :                 return '_';
     167             :             }
     168             : 
     169       11622 :             _PyOS_optarg = argv[_PyOS_optind++];
     170             :         }
     171             :     }
     172             : 
     173       28017 :     return option;
     174             : }
     175             : 
     176             : #ifdef __cplusplus
     177             : }
     178             : #endif
     179             : 

Generated by: LCOV version 1.14