From stdcxx-commits-return-187-apmail-incubator-stdcxx-commits-archive=incubator.apache.org@incubator.apache.org Wed Nov 23 02:03:51 2005 Return-Path: Delivered-To: apmail-incubator-stdcxx-commits-archive@www.apache.org Received: (qmail 56569 invoked from network); 23 Nov 2005 02:03:51 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 23 Nov 2005 02:03:51 -0000 Received: (qmail 16253 invoked by uid 500); 23 Nov 2005 02:03:51 -0000 Delivered-To: apmail-incubator-stdcxx-commits-archive@incubator.apache.org Received: (qmail 16231 invoked by uid 500); 23 Nov 2005 02:03:51 -0000 Mailing-List: contact stdcxx-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: stdcxx-dev@incubator.apache.org Delivered-To: mailing list stdcxx-commits@incubator.apache.org Received: (qmail 16220 invoked by uid 500); 23 Nov 2005 02:03:50 -0000 Delivered-To: apmail-incubator-stdcxx-cvs@incubator.apache.org Received: (qmail 16217 invoked by uid 99); 23 Nov 2005 02:03:50 -0000 X-ASF-Spam-Status: No, hits=-9.4 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [209.237.227.194] (HELO minotaur.apache.org) (209.237.227.194) by apache.org (qpsmtpd/0.29) with SMTP; Tue, 22 Nov 2005 18:03:50 -0800 Received: (qmail 56518 invoked by uid 65534); 23 Nov 2005 02:03:30 -0000 Message-ID: <20051123020330.56515.qmail@minotaur.apache.org> Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r348342 - /incubator/stdcxx/trunk/tests/src/cmdopt.cpp Date: Wed, 23 Nov 2005 02:03:30 -0000 To: stdcxx-cvs@incubator.apache.org From: sebor@apache.org X-Mailer: svnmailer-1.0.5 X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N Author: sebor Date: Tue Nov 22 18:03:26 2005 New Revision: 348342 URL: http://svn.apache.org/viewcvs?rev=348342&view=rev Log: 2005-11-22 Martin Sebor * cmdopt.cpp (rw_runopts): Enhanced options specified using the "#" character (such as "|-foo#") and allowed them to be followed by the quals sign and a numeric argument, such as --foo=123. Modified: incubator/stdcxx/trunk/tests/src/cmdopt.cpp Modified: incubator/stdcxx/trunk/tests/src/cmdopt.cpp URL: http://svn.apache.org/viewcvs/incubator/stdcxx/trunk/tests/src/cmdopt.cpp?rev=348342&r1=348341&r2=348342&view=diff ============================================================================== --- incubator/stdcxx/trunk/tests/src/cmdopt.cpp (original) +++ incubator/stdcxx/trunk/tests/src/cmdopt.cpp Tue Nov 22 18:03:26 2005 @@ -41,7 +41,10 @@ { char loptbuf_ [32]; // buffer for long option name optcallback_t *callback_; // function to call to process option - int *pcntr_; // counter to increment for each occurrence + + // counter to increment for each occurrence of an option + // or to set to the numeric argument (when specified) + int *pcntr_; char *lopt_; // long option name char sopt_; // short option name @@ -148,8 +151,8 @@ if (_RWSTD_SIZE_MAX == cmdopts [i].maxcalls_) printf (" (each occurrence evaluated)\n"); else if (1 < cmdopts [i].maxcalls_) - printf (" (at most %lu occurrences evaluated)\n", - cmdopts [i].maxcalls_); + printf (" (at most %u occurrences evaluated)\n", + unsigned (cmdopts [i].maxcalls_)); else printf (" (only the first occurrence evaluated)\n"); @@ -361,17 +364,13 @@ int arg_is_callback = true; - if (':' == *next || '=' == *next) { - // ':' : argument optional - // '=' : argument required - cmdopts [ncmdopts].arg_ = true; - ++next; - } - else if ('#' == *next) { + if ('#' == *next) { // insead of a pointer to a callback, the argument // is a pointer to an int counter that is to be // incremented for each occurrence of the option - // during processing + // during processing; when the option is immediately + // followed by the equals sign ('=') and a numeric + // argument the value of the argument will be stored arg_is_callback = false; ++next; @@ -379,6 +378,12 @@ // are allowed and will be counted cmdopts [ncmdopts].maxcalls_ = _RWSTD_SIZE_MAX; } + else if (':' == *next || '=' == *next) { + // ':' : argument optional + // '=' : argument required + cmdopts [ncmdopts].arg_ = true; + ++next; + } if ('@' == *next) { @@ -411,7 +416,9 @@ } else { // retrieve the address of the int counter where to keep - // track of the number of occurrences of the option + // track of the number of occurrences of the option, or + // where to store the value of the numeric argument of + // the option cmdopts [ncmdopts].pcntr_ = va_arg (va, int*); } @@ -487,6 +494,15 @@ break; } + // the name of the option without the leading dash + const char* const optname = argv [i] + 1; + + // look for the first equals sign + const char* const eq = strchr (optname, '='); + + // compute the length of the option including the equals sign (if any) + const size_t optlen = eq ? size_t (eq - optname + 1) : strlen (optname); + int found = false; // look up each command line option (i.e., a string that starts @@ -498,16 +514,8 @@ if ('-' == argv [i][0]) { - // the name of the option without the leading dash - const char* const optname = argv [i] + 1; - - // look for the first equals sign - const char* const eq = strchr (optname, '='); - - // compute the length of the ooption including the equals - // sign (if any) - const size_t optlen = - eq ? size_t (eq - optname + 1) : strlen (optname); + const size_t cmplen = + eq && cmdopts [j].pcntr_ ? optlen - 1 : optlen; // get a pointer to the (possibly empty) name // of the long option @@ -516,8 +524,8 @@ // try to match the long option first, and only if it // doesn't match try the short single-character option - if ( optlen == strlen (lopt) - && 0 == memcmp (optname, lopt, optlen) + if ( cmplen == strlen (lopt) + && 0 == memcmp (optname, lopt, cmplen) || cmdopts [j].sopt_ && optname [0] == cmdopts [j].sopt_ && (1 == optlen || cmdopts [j].arg_)) { @@ -546,6 +554,39 @@ // when the command line argument matched // the option, invoke the callback function status = cmdopts [j].callback_ (argc - i, argv + i); + } + } + else if (eq) { + assert (0 != cmdopts [j].pcntr_); + + // obtain the numeric argument + char *end = 0; + const long optval = strtol (eq + 1, &end, 0); + + if (end && '\0' != *end) { + fprintf (stderr, "expected numeric argument: %s\n", + optname); + ignenv = true; + errno = EINVAL; + status = 1; + } + +#if _RWSTD_INT_SIZE < _RWSTD_LONG_SIZE + + else if ( optval < _RWSTD_INT_MIN + || _RWSTD_INT_MAX < optval) { + fprintf (stderr, "numeric argument %ld out of range" + " [%d, %d]: %s\n", optval, + _RWSTD_INT_MIN, _RWSTD_INT_MAX, optname); + ignenv = true; + errno = EINVAL; + status = 1; + } + +#endif // _RWSTD_INT_SIZE < _RWSTD_LONG_SIZE + + else { + *cmdopts [j].pcntr_ = optval; } } else {