Added version switch, updated documentation on CLI
This commit is contained in:
parent
0285d32ce1
commit
8233695a2b
3 changed files with 48 additions and 16 deletions
29
doc/env.rst
29
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.
|
||||
|
|
|
@ -1,2 +1,3 @@
|
|||
#!/bin/sh
|
||||
./simple --help
|
||||
./simple --version
|
||||
|
|
34
src/main.c
34
src/main.c
|
@ -4,20 +4,37 @@
|
|||
#include <stdio.h>
|
||||
#include <getopt.h>
|
||||
|
||||
# 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]);
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue