On 2014-09-10 20:15:31 +0900, Michael Paquier wrote: > On Wed, Sep 10, 2014 at 10:04 AM, Andres Freund <[email protected]> wrote: > > The best plan after that seems to be to add optional_argument support to > > getopt_long.c - looks easy enough. Do we need a configure test for > > optional_argument? I don't think so, but I could see somebody arguing > > the other way round. > Adding support for optional_argument in src/port would be the best > solution for long-term purposes. So +1 for it or we'll be limited in > the future as well. Here is for example attached a proof-of-concept > patch that it is not that complicated to add support for optional > arguments... Interestingly, the current implementation dates of 2003 > in 939a59f and has not been majorly touched since. The patch has been > tested with MSVC.
I've attached a edited verion of that patch. Besides cosmetic stuff I made one imo important change. Your version looked to the next argument for optional arguments: When --help is an optional argument and e.g. the commandline is "--help --host" we can't accept --host as --help's value. So there's now a check for required_arg for that behaviour - which is in line with all the getopt_long() behaviours I could find docs (or code in case of netbsd) for. I'd not mind a review, but I'll push this soon. Doesn't seem worth reverting the entire psql patch when the fix is as localized as this. We might conceivably want to change the behaviour of optional_argument, but as there's only one caller at this point. Greetings, Andres Freund -- Andres Freund http://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Training & Services
>From a445cf989fe4421a1ff27cc60abb261595ed0a49 Mon Sep 17 00:00:00 2001 From: Andres Freund <[email protected]> Date: Wed, 10 Sep 2014 16:51:04 +0200 Subject: [PATCH] Add support for optional_argument to our own getopt_long() implementation. 07c8651dd91d5a currently causes compilation errors on mscv (and probably some other) compilers because our getopt_long() implementation doesn't have support for optional_argument. Thus implement optional_argument in our fallback implemenation. It's quite possibly also useful in other cases. Arguably this needs a configure check for optional_argument, but it has existed pretty much since getopt_long() was introduced and thus doesn't seem worth the configure runtime. Author: Michael Paquier and Andres Freund Discussion: CAB7nPqS5VeedSCxrK=qouokbawggklpyc1q++rrfca_sjcs...@mail.gmail.com --- src/include/getopt_long.h | 1 + src/port/getopt_long.c | 16 ++++++++++++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/include/getopt_long.h b/src/include/getopt_long.h index e23c21f..71a884c 100644 --- a/src/include/getopt_long.h +++ b/src/include/getopt_long.h @@ -23,6 +23,7 @@ struct option #define no_argument 0 #define required_argument 1 +#define optional_argument 2 #endif #ifndef HAVE_GETOPT_LONG diff --git a/src/port/getopt_long.c b/src/port/getopt_long.c index b099091..aa5b731 100644 --- a/src/port/getopt_long.c +++ b/src/port/getopt_long.c @@ -100,11 +100,14 @@ getopt_long(int argc, char *const argv[], if (strlen(longopts[i].name) == namelen && strncmp(place, longopts[i].name, namelen) == 0) { - if (longopts[i].has_arg) + int has_arg = longopts[i].has_arg; + + if (has_arg != no_argument) { if (place[namelen] == '=') optarg = place + namelen + 1; - else if (optind < argc - 1) + else if (optind < argc - 1 && + has_arg == required_argument) { optind++; optarg = argv[optind]; @@ -113,13 +116,18 @@ getopt_long(int argc, char *const argv[], { if (optstring[0] == ':') return BADARG; - if (opterr) + + if (opterr && has_arg == required_argument) fprintf(stderr, "%s: option requires an argument -- %s\n", argv[0], place); + place = EMSG; optind++; - return BADCH; + + if (has_arg == required_argument) + return BADCH; + optarg = NULL; } } else -- 2.0.0.rc2.4.g1dc51c6.dirty
-- Sent via pgsql-committers mailing list ([email protected]) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-committers
