nsprpub/lib/tests/getopt.c

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 #include <stdio.h>
     6 #include <string.h>
     7 #include <stdlib.h>
     9 #include "nspr.h"
    10 #include "plgetopt.h"
    14 static const PLLongOpt optArray[] = {
    15     { "longa", 'a'        , PR_TRUE  },
    16     { "longb", 'b'        , PR_TRUE  },
    17     { "longc", 'c'        , PR_FALSE },
    18     { "longd", 'd' | 0x100, PR_TRUE  },
    19     { "longe", 'e' | 0x100, PR_FALSE },
    20     {    NULL,                       }
    21 };
    23 int
    24 main(int argc, char **argv) 
    25 {
    26     PLOptState *opt;
    27     PLOptStatus ostat;
    29     opt = PL_CreateLongOptState(argc, argv, "a:b:c", optArray);
    31     while (PL_OPT_OK == (ostat = PL_GetNextOpt(opt))) {
    32 	if (opt->option == 0 && opt->longOptIndex < 0) 
    33 	    printf("Positional parameter: \"%s\"\n", opt->value);
    34 	else
    35 	    printf("%s option: %x (\'%c\', index %d), argument: \"%s\"\n",
    36 		   (ostat == PL_OPT_BAD) ? "BAD" : "GOOD",
    37 		   opt->longOption, opt->option ? opt->option : ' ',
    38 		   opt->longOptIndex, opt->value);
    40     }
    41     printf("last result was %s\n", (ostat == PL_OPT_BAD) ? "BAD" : "EOL");
    42     PL_DestroyOptState(opt);
    43     return 0;
    44 }

mercurial