bug-glibc
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

getopt_long


From: alain . bonnefoy
Subject: getopt_long
Date: Thu, 18 Nov 2004 08:04:11 +0100


Hello,

I send this email to suggest some improvment to the getopt_long() function.

According to the version of getopt_long I use, the optional argument parsing test is:

                if (o->has_arg>0) {
                    if (*max=='=')
                        optarg=max+1;
                    else {
                        optarg=argv[optind+1];
                        ++optind;
                        // These two 'if' code blocks fix a bug about optional argument missing.
                        // More, I added two additional test with (optarg[0] == '-') as I consider that dash
                        // specify an option only if it's followed by a character different from space or end of string
                        if ((o->has_arg==1) && (!optarg || ((optarg[0] == '-') && (optarg[1] != ' ') && (optarg[1] != '\0')))) { /* no mandatory argument there */
                            if (*optstring==':') return ':';
                            write(2,"argument required: `",20);
                            write(2,arg,max-arg);
                            write(2,"'.\n",3);
                            return '?';
                        }
                        if ((o->has_arg==2) && (!optarg || ((optarg[0] == '-') && (optarg[1] != ' ') && (optarg[1] != '\0')))) { /* no optional argument there */
                            optarg = NULL;
                            if (!o->flag) return o->val;
                            return 0;
                        }
                    }
                }

The problem of this code (in red) is that don't allow to specify negative values as optional parameters such as:

# my_cmd --setmin -100 --setmax 100

What I propose is to add an additional test to accept a dash character as optional argument if it is followed by a digit:

                if (o->has_arg>0) {
                    if (*max=='=')
                        optarg=max+1;
                    else {
                        optarg=argv[optind+1];
                        ++optind;
                        // These two 'if' code blocks fix a bug about optional argument missing.
                        // More, I added two additional test with (optarg[0] == '-') as I consider that dash
                        // specify an option only if it's followed by a character different from space or end of string AND it's not a digit like a negative value
                        if ((o->has_arg==1) && (!optarg || ((optarg[0] == '-') && (!isdigit(optarg[1])) && (optarg[1] != ' ') && (optarg[1] != '\0')))) { /* no mandatory argument there */
                            if (*optstring==':') return ':';
                            write(2,"argument required: `",20);
                            write(2,arg,max-arg);
                            write(2,"'.\n",3);
                            return '?';
                        }
                        if ((o->has_arg==2) && (!optarg || ((optarg[0] == '-') && (!isdigit(optarg[1])) && (optarg[1] != ' ') && (optarg[1] != '\0')))) { /* no optional argument there */
                            optarg = NULL;
                            if (!o->flag) return o->val;
                            return 0;
                        }
                    }
                }


Kind regards,
A.Bonnefoy

~~~~~~~~~~~¤¤¤¤¤¤¤¤¤¤¤~~~~~~~~~~~~
Do like some yet famous providers. Host spammers and belong to my black list, mails from your domain will be thrown to trash.
hotmail.com msn.com aol.com aol.net tiscali.fr yahoo.com freemail.com worldcom.ch

reply via email to

[Prev in Thread] Current Thread [Next in Thread]