X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=tests%2Ftest-argp.c;h=2831dc13c2fd63b436ce9ecd43216e8dfa48f10c;hb=35600a9a833bc287a025214c9601574e4a8115d7;hp=667ca9d66b06fca071d44d9165860931e8730159;hpb=6e714e7a1e47ada667d0a7db1cef42e9fe7d21f0;p=gnulib.git diff --git a/tests/test-argp.c b/tests/test-argp.c index 667ca9d66..2831dc13c 100644 --- a/tests/test-argp.c +++ b/tests/test-argp.c @@ -1,24 +1,23 @@ /* Test suite for argp. - Copyright (C) 2006 Free Software Foundation, Inc. + Copyright (C) 2006-2007 Free Software Foundation, Inc. This file is part of the GNUlib Library. - This program is free software; you can redistribute it and/or modify + This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2, or (at your option) - any later version. + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - You should have received a copy of the GNU General Public License along - with this program; if not, write to the Free Software Foundation, - Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ -#ifdef HAVE_CONFIG_H -# include -#endif +#include + +#include "argp.h" #include #include @@ -28,8 +27,7 @@ #if HAVE_STRINGS_H # include #endif - -#include "argp.h" +#include "progname.h" struct test_args { @@ -40,14 +38,16 @@ struct test_args int opt; char *optional; int optional_set; + int group_2_1_option; + int group_1_1_option; }; -static struct argp_option group1_option[] = +static struct argp_option group1_option[] = { { NULL, 0, NULL, 0, "Option Group 1", 0}, { "verbose", 'v', NULL, 0, "Simple option without arguments", 1 }, { "file", 'f', "FILE", 0, "Option with a mandatory argument", 1 }, - { "input", 0, NULL, OPTION_ALIAS, NULL, 1 }, + { "input", 0, NULL, OPTION_ALIAS, NULL, 1 }, { "hidden", 'H', "FILE", OPTION_HIDDEN, "Hidden option", 1 }, { NULL, 0, NULL, 0, NULL, 0 } }; @@ -62,15 +62,15 @@ group1_parser (int key, char *arg, struct argp_state *state) case 'v': args->verbose++; break; - + case 'f': args->file = arg; break; - + case 'H': args->hidden = arg; break; - + default: return ARGP_ERR_UNKNOWN; } @@ -90,15 +90,52 @@ struct argp_child group1_child = { }; -static struct argp_option group2_option[] = +static struct argp_option group1_1_option[] = +{ + { NULL, 0, NULL, 0, "Option Group 1.1", 0}, + { "cantiga", 'C', NULL, 0, "create a cantiga" }, + { "sonet", 'S', NULL, 0, "create a sonet" }, + { NULL, 0, NULL, 0, NULL, 0 } +}; + +static error_t +group1_1_parser (int key, char *arg, struct argp_state *state) +{ + struct test_args *args = state->input; + switch (key) + { + case 'C': + case 'S': + args->group_1_1_option = key; + break; + default: + return ARGP_ERR_UNKNOWN; + } + return 0; +} + +struct argp group1_1_argp = { + group1_1_option, + group1_1_parser +}; + +struct argp_child group1_1_child = { + &group1_1_argp, + 0, + "", + 2 +}; + + +static struct argp_option group2_option[] = { { NULL, 0, NULL, 0, "Option Group 2", 0}, { "option", 'O', NULL, 0, "An option", 1 }, { "optional", 'o', "ARG", OPTION_ARG_OPTIONAL, - "Option with an optional argument. ARG is one of the following:", 1 }, - { "one", 0, NULL, OPTION_DOC|OPTION_NO_TRANS, "one unit", 2 }, - { "two", 0, NULL, OPTION_DOC|OPTION_NO_TRANS, "two units", 2 }, - { "many", 0, NULL, OPTION_DOC|OPTION_NO_TRANS, "many units", 2 }, + "Option with an optional argument. ARG is one of the following:", 2 }, + { "one", 0, NULL, OPTION_DOC|OPTION_NO_TRANS, "one unit", 3 }, + { "two", 0, NULL, OPTION_DOC|OPTION_NO_TRANS, "two units", 3 }, + { "many", 0, NULL, OPTION_DOC|OPTION_NO_TRANS, "many units", 3 }, { NULL, 0, NULL, 0, NULL, 0 } }; @@ -112,12 +149,12 @@ group2_parser (int key, char *arg, struct argp_state *state) case 'O': args->opt = 1; break; - + case 'o': args->optional_set = 1; args->optional = arg; break; - + default: return ARGP_ERR_UNKNOWN; } @@ -136,10 +173,48 @@ struct argp_child group2_child = { 2 }; + +static struct argp_option group2_1_option[] = +{ + { NULL, 0, NULL, 0, "Option Group 2.1", 0}, + { "poem", 'p', NULL, 0, "create a poem" }, + { "limerick", 'l', NULL, 0, "create a limerick" }, + { NULL, 0, NULL, 0, NULL, 0 } +}; + +static error_t +group2_1_parser (int key, char *arg, struct argp_state *state) +{ + struct test_args *args = state->input; + switch (key) + { + case 'p': + case 'e': + args->group_2_1_option = key; + break; + default: + return ARGP_ERR_UNKNOWN; + } + return 0; +} + +struct argp group2_1_argp = { + group2_1_option, + group2_1_parser +}; + +struct argp_child group2_1_child = { + &group2_1_argp, + 0, + "", + 2 +}; + + static struct argp_option main_options[] = { { NULL, 0, NULL, 0, "Main options", 0}, - { "test", 't', NULL, 0, NULL, 1 }, + { "test", 't', NULL, 0, NULL, 1 }, { NULL, 0, NULL, 0, NULL, 0 } }; @@ -148,7 +223,7 @@ parse_opt (int key, char *arg, struct argp_state *state) { struct test_args *args = state->input; int i; - + switch (key) { case ARGP_KEY_INIT: @@ -159,7 +234,7 @@ parse_opt (int key, char *arg, struct argp_state *state) case 't': args->test = 1; break; - + default: return ARGP_ERR_UNKNOWN; } @@ -325,7 +400,7 @@ test10(struct argp *argp) INIT_TEST2 (10, "--optional", "ARG"); test_optional(argp, argc, argv, &test_args, NULL, "ARG"); } - + void test11(struct argp *argp) { @@ -338,7 +413,28 @@ test12(struct argp *argp) { INIT_TEST3 (12, "--option", "--optional=OPT", "FILE"); test_optional(argp, argc, argv, &test_args, "OPT", "FILE"); -} +} + +void +test13(struct argp *argp) +{ + INIT_TEST1 (1, "--cantiga"); + if (argp_parse (argp, argc, argv, 0, NULL, &test_args)) + fail("argp_parse failed"); + else if (test_args.group_1_1_option != 'C') + fail("option not processed"); +} + +void +test14(struct argp *argp) +{ + INIT_TEST1 (1, "--limerick"); + if (argp_parse (argp, argc, argv, 0, NULL, &test_args)) + fail("argp_parse failed"); + else if (test_args.group_2_1_option != 'l') + fail("option not processed"); +} + typedef void (*test_fp) (struct argp *argp); @@ -347,15 +443,26 @@ test_fp test_fun[] = { test1, test2, test3, test4, test5, test6, test7, test8, test9, test10, test11, test12, + test13, test14, NULL }; int main (int argc, char **argv) { - struct argp_child argp_children[3]; + struct argp_child argp_children[3], group1_children[2], group2_children[2]; test_fp *fun; - + + set_program_name (argv[0]); + + group1_children[0] = group1_1_child; + group1_children[1].argp = NULL; + group1_argp.children = group1_children; + + group2_children[0] = group2_1_child; + group2_children[1].argp = NULL; + group2_argp.children = group2_children; + argp_children[0] = group1_child; argp_children[1] = group2_child; argp_children[2].argp = NULL; @@ -363,11 +470,11 @@ main (int argc, char **argv) if (argc > 0) { - struct test_args test_args; - init_args(test_args); + struct test_args test_args; + init_args(test_args); return argp_parse (&test_argp, argc, argv, 0, NULL, &test_args); } - + for (fun = test_fun; *fun; fun++) (*fun) (&test_argp);