You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@apr.apache.org by Greg Stein <gs...@lyra.org> on 2000/11/25 23:40:06 UTC

Re: cvs commit: apr/test testargs.c

Speaking of testargs.c ... the "d::" part means that the argument for -d is
*optional*. Currently, getopt() requires an argument (read: it doesn't
understand double-colons).

Q: should it understand a double-colon?

[ the man page for getopt() describes the double-colon ]

Cheers,
-g

On Sat, Nov 25, 2000 at 10:34:04PM -0000, gstein@locus.apache.org wrote:
> gstein      00/11/25 14:34:04
> 
>   Modified:    test     testargs.c
>   Log:
>   add a "default:" case, just to be sure.
>   print out any additional arguments.
>   factor out the maybe_arg() functionality.
>   
>   Revision  Changes    Path
>   1.16      +20 -7     apr/test/testargs.c
>   
>   Index: testargs.c
>   ===================================================================
>   RCS file: /home/cvs/apr/test/testargs.c,v
>   retrieving revision 1.15
>   retrieving revision 1.16
>   diff -u -u -r1.15 -r1.16
>   --- testargs.c	2000/08/09 14:55:44	1.15
>   +++ testargs.c	2000/11/25 22:34:04	1.16
>   @@ -63,6 +63,16 @@
>    #include <unistd.h>
>    #endif
>    
>   +static void maybe_arg(const char *arg)
>   +{
>   +    if (arg) {
>   +        printf(" with %s\n", arg);
>   +    }
>   +    else {
>   +        printf("\n");
>   +    }
>   +}
>   +
>    int main(int argc, char * const argv[])
>    {
>        apr_pool_t *context;
>   @@ -80,7 +90,7 @@
>            exit(1);
>        }
>        while (apr_getopt(opt, "abc:d::", &data, &optarg) == APR_SUCCESS) {
>   -        switch(data) {
>   +        switch (data) {
>                case 'a':
>                case 'b':
>                    printf("option %c\n", data);
>   @@ -90,14 +100,17 @@
>                    break;
>                case 'd':
>                    printf("option %c", data);
>   -                if (optarg) {
>   -                    printf(" with %s\n", optarg);
>   -                }
>   -                else {
>   -                    printf("\n");
>   -                }
>   +                maybe_arg(optarg);
>                    break;
>   +            default:
>   +                printf("unknown option: %c", data);
>   +                maybe_arg(optarg);
>   +                break;
>            }
>        }
>   +
>   +    while (opt->ind < opt->argc)
>   +        printf("extra arg: %s\n", opt->argv[opt->ind++]);
>   +
>        return 1;
>    }
>   
>   
>   

-- 
Greg Stein, http://www.lyra.org/

Re: cvs commit: apr/test testargs.c

Posted by Greg Stein <gs...@lyra.org>.
[ OtherBill forgot to copy the list; I'm including it here without cutting ]

On Sat, Nov 25, 2000 at 04:50:19PM -0600, William A. Rowe, Jr. wrote:
> > From: Greg Stein [mailto:gstein@lyra.org]
> > Sent: Saturday, November 25, 2000 4:40 PM
> > 
> > Speaking of testargs.c ... the "d::" part means that the argument for -d is
> > *optional*. Currently, getopt() requires an argument (read: it doesn't
> > understand double-colons).
> > 
> > Q: should it understand a double-colon?
> > 
> > [ the man page for getopt() describes the double-colon ]
> 
> Without reading it (just yet)...
> 
> I'd agree that getopt() could understand the required-optional-invalid cases,
> and that would be potentially quite useful.  The downside to ever using the
> d:: syntax is that the parser is easily confused between the -f opt arg
> case and the -f arg case (which could be -f opt).

Yes, this could be confusing, but "it isn't our problem". The regular
getopt() allows this behavior, so it is really a question of whether we want
APR's getopt to follow it, too.

I'm also fine with saying "sorry, we don't do that because it makes for a
confusing cmdline interface."

> I'm reminded that apr needs to address the fact that 1. filenames contain
> spaces (on some platforms >;-) and 2. that x:bleh is a legit filespec.

1. APR doesn't have any problem with spaces. argv[n] can easily be a
   filename with a space. The getopt() function does look inside the
   "argument" strings.
   
   $ program "file with a space"
   
   The quote handling is done by the shell on most Unixen. If Windows
   doesn't handle the quotes automagically, then you're going to need to
   come up with a patch :-)

2. Since we don't parse out arguments, we'll never see the ":".

> I mention these only to be sure, as we do 'more and more' getopt work, that
> we shouldn't break anything :-)
> 
> Bill

Sure :-)

Cheers,
-g

-- 
Greg Stein, http://www.lyra.org/