From aca092b9d6ed033f0c63235a0453734ef8af6b96 Mon Sep 17 00:00:00 2001 From: Snaipe Date: Mon, 7 Sep 2015 15:27:15 +0200 Subject: [PATCH] Refactored CLI parameter handling in its own function for reusability --- CMakeLists.txt | 1 + doc/internal.rst | 23 +++++++++++++++++++++++ include/criterion/criterion.h | 1 + src/entry.c | 8 ++++++++ src/main.c | 14 +++++++------- 5 files changed, 40 insertions(+), 7 deletions(-) create mode 100644 src/entry.c diff --git a/CMakeLists.txt b/CMakeLists.txt index d222268..958a62d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -99,6 +99,7 @@ set(SOURCE_FILES src/posix-compat.c src/theories.c src/main.c + src/entry.c ) if (PCRE_FOUND) diff --git a/doc/internal.rst b/doc/internal.rst index 8c94237..873d6be 100644 --- a/doc/internal.rst +++ b/doc/internal.rst @@ -34,12 +34,35 @@ fail_fast bool True iff the test runner pattern const char * The pattern of the tests that should be executed =================== ================================== ============================================================== +if you want criterion to provide its own default CLI parameters and environment +variables handling, you can also call ``criterion_initialize(int argc, char *argv[], bool handle_unknown_arg)`` +with the proper ``argc/argv``. ``handle_unknown_arg``, if set to true, is here +to tell criterion to print its usage when an unknown CLI parameter is encountered. +If you want to add your own parameters, you should set it to false. + +The function returns 0 if the main should exit immediately, and 1 if it should +continue. + Starting the test runner ~~~~~~~~~~~~~~~~~~~~~~~~ The test runner can be called with ``criterion_run_all_tests``. The function returns 0 if one test or more failed, 1 otherwise. +Example main +~~~~~~~~~~~~ + +.. code-block:: c + + #include + + int main(int argc, char *argv[]) { + if (!criterion_initialize(argc, argv, true)) + return 0; + + return !criterion_run_all_tests(); + } + Implementing your own output provider ------------------------------------- diff --git a/include/criterion/criterion.h b/include/criterion/criterion.h index 172c585..ba8185a 100644 --- a/include/criterion/criterion.h +++ b/include/criterion/criterion.h @@ -79,6 +79,7 @@ CR_BEGIN_C_API CR_API int criterion_run_all_tests(void); +CR_API int criterion_initialize(int argc, char *argv[], bool handle_unknown_arg); CR_END_C_API diff --git a/src/entry.c b/src/entry.c new file mode 100644 index 0000000..b5700b0 --- /dev/null +++ b/src/entry.c @@ -0,0 +1,8 @@ +#include "criterion/criterion.h" + +CR_API int main(int argc, char *argv[]) { + if (!criterion_initialize(argc, argv, true)) + return 0; + + return !criterion_run_all_tests(); +} diff --git a/src/main.c b/src/main.c index 50a86d6..86d49fa 100644 --- a/src/main.c +++ b/src/main.c @@ -22,13 +22,13 @@ * THE SOFTWARE. */ #define _GNU_SOURCE -#include -#include -#include #include #include #include #include +#include "criterion/criterion.h" +#include "criterion/options.h" +#include "criterion/ordered-set.h" #include "runner.h" #include "config.h" #include "common.h" @@ -120,7 +120,7 @@ int list_tests(bool unicode) { return 0; } -CR_API int main(int argc, char *argv[]) { +CR_API int criterion_initialize(int argc, char *argv[], bool handle_unknown_arg) { static struct option opts[] = { {"verbose", optional_argument, 0, 'b'}, {"version", no_argument, 0, 'v'}, @@ -176,8 +176,8 @@ CR_API int main(int argc, char *argv[]) { case 't': use_tap = true; break; case 'l': do_list_tests = true; break; case 'v': do_print_version = true; break; - case 'h': - default : do_print_usage = true; break; + case 'h': do_print_usage = true; break; + default : do_print_usage = handle_unknown_arg; break; } } if (use_tap) @@ -189,5 +189,5 @@ CR_API int main(int argc, char *argv[]) { if (do_list_tests) return list_tests(!criterion_options.use_ascii); - return !criterion_run_all_tests(); + return 1; }