Steven D'Aprano added the comment:

On Tue, Jul 26, 2016 at 03:27:29PM +0000, R. David Murray wrote:
[...]
> getopt is explicitly emulating the C getopt

There are lots of differences between the C getopt and the Python 
version, and the Python version is described as offering an API 
"designed to be familiar" to uses of the C version, not to emulate all 
the idiosyncrasies of the C version. For instance, the Python version 
raises an exception on errors, rather than returning -1; the C version 
requires argc ("argument count"), but the Python version doesn't.

But most critically, the C version DOES strip whitespace from long 
arguments. On my Centos box, it only strips *trailing* spaces, not 
leading spaces, but it does strip them. So if your argument is that we 
must do what the C getopt does, then we must likewise at least strip 
trailing spaces.

Attached is a demo, adapted from the code given by `man 3 getopt`.

[steve@ando ~]$ gcc getopdemo.c
[steve@ando ~]$ ./a.out "--  spam" 1 --eggs 2 "--  cheese" 3
option   spam   with arg 1
option eggs   with arg 2
option   cheese with arg 3

If Serhiy is going to insist that getopt.py must follow the C getopt 
precisely, then the failure to strip trailing spaces is certainly a bug.

----------
Added file: http://bugs.python.org/file43898/getopdemo.c

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue27619>
_______________________________________
       #include <stdio.h>     /* for printf */
       #include <stdlib.h>    /* for exit */
       #include <getopt.h>

       int
       main (int argc, char **argv) {
           int c;
           int digit_optind = 0;

           while (1) {
               int this_option_optind = optind ? optind : 1;
               int option_index = 0;
               static struct option long_options[] = {
                   {"  spam  ", 1, 0, 0},
                   {"eggs  ", 1, 0, 0},
                   {"  cheese", 1, 0, 0},
                   {0, 0, 0, 0}
               };

               c = getopt_long (argc, argv, "",
                        long_options, &option_index);
               if (c == -1)
                   break;

               switch (c) {
               case 0:
                   printf ("option %s", long_options[option_index].name);
                   if (optarg)
                       printf (" with arg %s", optarg);
                   printf ("\n");
                   break;

               case '?':
                   break;

               default:
                   printf ("?? getopt returned character code 0%o ??\n", c);
               }
           }
           exit (0);
       }
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to