From 8233695a2b68bf264d49e0b5e25e73af4abf8db0 Mon Sep 17 00:00:00 2001 From: Snaipe Date: Wed, 18 Mar 2015 07:10:17 +0100 Subject: [PATCH] Added version switch, updated documentation on CLI --- doc/env.rst | 29 +++++++++++++++++++++-------- samples/tests/help.sh | 1 + src/main.c | 34 ++++++++++++++++++++++++++-------- 3 files changed, 48 insertions(+), 16 deletions(-) diff --git a/doc/env.rst b/doc/env.rst index 091f489..7946a20 100644 --- a/doc/env.rst +++ b/doc/env.rst @@ -1,15 +1,28 @@ Environment and CLI =================== -Tests built with Criterion support environment variables to alter -their runtime behaviour. +Tests built with Criterion expose by default various command line switchs +and environment variables to alter their runtime behaviour. + +Command line arguments +---------------------- + +* `-h or --help`: Show a help message with the available switches +* `--no-early-exit`: The test workers shall not prematurely exit when done and + will properly return from the main, cleaning up their process space. + This is useful when tracking memory leaks with `valgrind --tool=memcheck`. +* `--always-succeed`: The process shall exit with a status of `0`. +* `--tap`: Enables the TAP (Test Anything Protocol) output format. +* `--verbose[=level]`: Makes the output verbose. When provided with an integer, + sets the verbosity level to that integer. Environment Variables --------------------- -* `CRITERION_ALWAYS_SUCCEED`: when set to `1`, the exit status of the test - process will be 0, regardless if the tests failed or not. -* `CRITERION_NO_EARLY_EXIT`: when set to `1`, the test workers shall not - call `_exit` when done and will properly return from the main and - clean up their process space. This is useful when tracking memory leaks with - `valgrind --tool=memcheck`. +Environment variables are alternatives to command line switches when set to 1. + +* `CRITERION_ALWAYS_SUCCEED`: Same as `--always-succeed`. +* `CRITERION_NO_EARLY_EXIT`: Same as `--no-early-exit`. +* `CRITERION_ENABLE_TAP`: Same as `--tap`. +* `CRITERION_VERBOSITY_LEVEL`: Same as `--verbose`. Sets the verbosity level + to its value. diff --git a/samples/tests/help.sh b/samples/tests/help.sh index 21daba7..6b60bb5 100755 --- a/samples/tests/help.sh +++ b/samples/tests/help.sh @@ -1,2 +1,3 @@ #!/bin/sh ./simple --help +./simple --version diff --git a/src/main.c b/src/main.c index ad350c0..ab362ea 100644 --- a/src/main.c +++ b/src/main.c @@ -4,20 +4,37 @@ #include #include -# define USAGE \ - "usage: %s OPTIONS\n" \ - "options: \n" \ - " -h or --help: prints this message\n" \ - " --verbose [level]: sets verbosity to level\n" +# define VERSION "v1.0.0" +# define VERSION_MSG "Tests compiled with Criterion " VERSION "\n" + +# define USAGE \ + VERSION_MSG "\n" \ + "usage: %s OPTIONS\n" \ + "options: \n" \ + " -h or --help: prints this message\n" \ + " -v or --version: prints the version of criterion " \ + "these tests have been linked against\n" \ + " --tap: enables TAP formatting\n" \ + " --always-succeed: always exit with 0\n" \ + " --no-early-exit: do not exit the test worker " \ + "prematurely after the test\n" \ + " --verbose[=level]: sets verbosity to level " \ + "(1 by default)\n" int print_usage(char *progname) { fprintf(stderr, USAGE, progname); return 0; } +int print_version(void) { + fputs(VERSION_MSG, stderr); + return 0; +} + int main(int argc, char *argv[]) { static struct option opts[] = { - {"verbose", optional_argument, 0, 'v'}, + {"verbose", optional_argument, 0, 'b'}, + {"version", no_argument, 0, 'v'}, {"tap", no_argument, 0, 't'}, {"help", no_argument, 0, 'h'}, {"always-succeed", no_argument, 0, 'y'}, @@ -32,12 +49,13 @@ int main(int argc, char *argv[]) { .logging_threshold = atoi(getenv("CRITERION_VERBOSITY_LEVEL") ?: "2"), }; - for (int c; (c = getopt_long(argc, argv, "h", opts, NULL)) != -1;) { + for (int c; (c = getopt_long(argc, argv, "hv", opts, NULL)) != -1;) { switch (c) { - case 'v': criterion_options.logging_threshold = atoi(optarg ?: "1"); break; + case 'b': criterion_options.logging_threshold = atoi(optarg ?: "1"); break; case 't': criterion_options.enable_tap_format = true; break; case 'y': criterion_options.always_succeed = true; break; case 'z': criterion_options.no_early_exit = true; break; + case 'v': return print_version(); case 'h': default : return print_usage(argv[0]); }