nsprpub/lib/tests/getopt.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/nsprpub/lib/tests/getopt.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,44 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +#include <stdio.h>
     1.9 +#include <string.h>
    1.10 +#include <stdlib.h>
    1.11 +
    1.12 +#include "nspr.h"
    1.13 +#include "plgetopt.h"
    1.14 +
    1.15 +
    1.16 +
    1.17 +static const PLLongOpt optArray[] = {
    1.18 +    { "longa", 'a'        , PR_TRUE  },
    1.19 +    { "longb", 'b'        , PR_TRUE  },
    1.20 +    { "longc", 'c'        , PR_FALSE },
    1.21 +    { "longd", 'd' | 0x100, PR_TRUE  },
    1.22 +    { "longe", 'e' | 0x100, PR_FALSE },
    1.23 +    {    NULL,                       }
    1.24 +};
    1.25 +
    1.26 +int
    1.27 +main(int argc, char **argv) 
    1.28 +{
    1.29 +    PLOptState *opt;
    1.30 +    PLOptStatus ostat;
    1.31 +
    1.32 +    opt = PL_CreateLongOptState(argc, argv, "a:b:c", optArray);
    1.33 +
    1.34 +    while (PL_OPT_OK == (ostat = PL_GetNextOpt(opt))) {
    1.35 +	if (opt->option == 0 && opt->longOptIndex < 0) 
    1.36 +	    printf("Positional parameter: \"%s\"\n", opt->value);
    1.37 +	else
    1.38 +	    printf("%s option: %x (\'%c\', index %d), argument: \"%s\"\n",
    1.39 +		   (ostat == PL_OPT_BAD) ? "BAD" : "GOOD",
    1.40 +		   opt->longOption, opt->option ? opt->option : ' ',
    1.41 +		   opt->longOptIndex, opt->value);
    1.42 +
    1.43 +    }
    1.44 +    printf("last result was %s\n", (ostat == PL_OPT_BAD) ? "BAD" : "EOL");
    1.45 +    PL_DestroyOptState(opt);
    1.46 +    return 0;
    1.47 +}

mercurial