From e8ea49cae5e294f70a0db72054f46e718cd75789 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sun, 30 Jun 2013 11:53:37 +0200 Subject: add c test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- Makefile | 1 + src/test.c | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 src/test.c diff --git a/Makefile b/Makefile index 54584d0..ebba7b7 100644 --- a/Makefile +++ b/Makefile @@ -4,6 +4,7 @@ JAVA_OPTIMISE = -O all: @mkdir -p bin $(CC) $(C_OPTIMISE) -std=gnu99 -Wall -Wextra -pedantic -fPIC -shared src/argparser.c -o bin/argparser.so + $(CC) $(C_OPTIMISE) -std=gnu99 -Wall -Wextra -pedantic src/test.c -o bin/test javac $(JAVA_OPTIMISE) -cp src -s src -d bin src/argparser/ArgParser.java javac $(JAVA_OPTIMISE) -cp src -s src -d bin src/Test.java cd bin ; jar cf ArgParser.jar argparser/ArgParser*.class diff --git a/src/test.c b/src/test.c new file mode 100644 index 0000000..9f85185 --- /dev/null +++ b/src/test.c @@ -0,0 +1,98 @@ +/** + * argparser – command line argument parser library + * + * Copyright © 2013 Mattias Andrée (maandree@member.fsf.org) + * + * This library 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 3 of the License, or + * (at your option) any later version. + * + * This library 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 library. If not, see . + */ +#include +#include +#include "argparser.h" + + +int main(int argc, char** argv) +{ + char* pname = args_parent_name(1); + printf("Parent: %s\n", pname); + free(pname); + + args_init("A test for argparser", "test [options] [files]", + "Copyright © 2013 Mattias Andrée (maandree@member.fsf.org)\n" + "\n" + "This library is free software: you can redistribute it and/or modify\n" + "it under the terms of the GNU General Public License as published by\n" + "the Free Software Foundation, either version 3 of the License, or\n" + "(at your option) any later version.\n" + "\n" + "This library is distributed in the hope that it will be useful,\n" + "but WITHOUT ANY WARRANTY; without even the implied warranty of\n" + "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" + "GNU General Public License for more details.\n" + "\n" + "You should have received a copy of the GNU General Public License\n" + "along with this library. If not, see .", 0, 1); + + args_add_option(args_new_argumentless(0, "-h", "-?", "--help"), "Prints this help message\n(and exits)"); + args_add_option(args_new_argumentless(0, "--hello"), "Prints the text: hello world'"); + args_add_option(args_new_argumentless(0, "++hidden"), 0); + + args_add_option(args_new_argumented("LINE", 0, "-l", "--line"), "Prints the choosen line"); + args_add_option(args_new_variadic("LINE", 0, "--l", "--lines"), "Prints the choosen lines"); + + args_parse(argc, argv); + args_support_alternatives(); + + if (args_opts_used("-?")) + args_help(); + else if (!args_unrecognised_count && args_arguments_count && !args_files_count) + { + char** arr; + long i = 0, n; + i = 0; + if (args_opts_used("--hello")) + for (n = args_opts_get_count("--hello"); i < n; i++) + printf("Hello World\n"); + if (args_opts_used("-l")) + { + i = 0; + arr = args_opts_get("--line"); + for (n = args_opts_get_count("--line"); i < n; i++) + printf("%s\n", *(arr + i)); + } + if (args_opts_used("--lines")) + { + i = 0; + arr = args_opts_get("--l"); + for (n = args_opts_get_count("--l"); i < n; i++) + printf("%s\n", *(arr + i)); + if (n == 0) + printf("--l(--lines) is used without and arguments\n"); + } + if (args_opts_used("++hidden")) + printf("Congratulations, you have found the secret option!\n"); + } + else + { + long i; + printf("Number of unrecognised options: %li\n", args_unrecognised_count); + printf("Entered message: %s\n", args_message ? args_message : "null"); + printf("Entered files:\n"); + for (i = 0; i < args_files_count; i++) + printf("\t%s\n", *(args_files + i)); + } + + args_dispose(); + return 0; +} + -- cgit v1.2.3-70-g09d2