1 /**
2  * Command-line options for running unittests.
3  */
4 
5 module unit_threaded.options;
6 
7 import std.getopt;
8 import std.stdio;
9 import std.random;
10 import std.exception;
11 
12 /**
13  * Options to the test-runner to be specified at run-time.
14  */
15 struct Options
16 {
17     bool multiThreaded;
18     string[] testsToRun;
19     bool debugOutput;
20     bool list;
21     bool exit;
22     bool forceEscCodes;
23     bool random;
24     uint seed;
25 }
26 
27 /**
28  * Parses the command-line args.
29  * Params:
30  *   args = The arguments passed to main.
31  * Returns: The options struct.
32  */
33 Options getOptions(string[] args)
34 {
35     bool single;
36     bool debugOutput;
37     bool list;
38     bool forceEscCodes;
39     bool random;
40     uint seed = unpredictableSeed;
41 
42     auto helpInfo = getopt(
43         args,
44         "single|s", "Single-threaded execution.", &single, //single-threaded
45         "debug|d", "Enable debug output.", &debugOutput, //print debug output
46         "esccodes|e", "Force ANSI escape codes even for !isatty.", &forceEscCodes,
47         "list|l", "List tests, don't run them.", &list,
48         "random|r", "Run tests in random order using one thread.", &random,
49         "seed", "Set the seed for the random order.", &seed,
50     );
51 
52     if (helpInfo.helpWanted)
53     {
54         defaultGetoptPrinter("Usage: <progname> <options> <tests>...", helpInfo.options);
55     }
56 
57     if (debugOutput)
58     {
59         if (!single)
60         {
61             writeln("-d implies -s, running in a single thread\n");
62         }
63         single = true;
64     }
65 
66     if (random)
67     {
68         if (!single)
69             writeln("-r implies -s, running in a single thread\n");
70         single = true;
71     }
72 
73     immutable exit = helpInfo.helpWanted || list;
74     return Options(!single, args[1 .. $], debugOutput, list, exit, forceEscCodes, random,
75         seed);
76 }