root/misc/opttest.d

Revision 128, 2.4 kB (checked in by KirkMcDonald, 7 years ago)

Added module declarations to test modules.

Line 
1 module opttest;
2
3 import std.stdio : writefln;
4 import optparse;
5
6 void print_opts(Options options) {
7     writefln("file:        ", options["file"]);
8     writefln("imports:     ", options.list("importPath"));
9     writefln("verbosity:   ", options.count("verbose"));
10     writefln("value:       ", options.value("value"));
11     writefln("number list: ", options.valueList("list"));
12     writefln("switch:      ", options.flag("switch"));
13     writefln("turn-off:    ", options.flag("turn"));
14     writefln("color:       ", options["color"]);
15     writefln("args:        ", options.args);
16 }
17
18 int main(char[][] args) {
19     auto parser = new OptionParser("A test suite for optparser.");
20     parser.addOption("-f", "--file").help("Stores a single argument.");
21     parser.addOption(["-I", "--import"], "importPath", Action.Append).help("Adds arguments to a list.");
22     parser.addOption(["-c", "--callback"], {
23         writefln("callback encountered");
24     }).help("Calls a callback.");
25     parser.addOption(["-n", "--number"], (int i) {
26         writefln("number callback: %d * 2 = %d", i, i*2);
27     }).help("Calls a callback with a number.");
28     // A fancy callback
29     parser.addOption(["--fancy"], "a callback", (Options opts, inout char[][] a, inout int i, char[] name) {
30         writefln("Current file: %s", opts["file"]);
31         writefln("Remaining args: %s", a[i+1 .. $]);
32         // Skip the next arg
33         ++i;
34         writefln("This arg's name: %s", name);
35     }).help("Tests optparse's fancy callbacks.");
36     parser.addOption(["--enable"], "switch", Action.SetTrue).help("Enables the switch.");
37     parser.addOption(["--disable"], "switch", Action.SetFalse).help("Disables the switch.");
38     parser.addOption(["--turn-off"], "turn", Action.SetFalse).help("Tests default values for flags.").def(true);
39     parser.addOption(["--set-blue"], "color", Action.StoreConst, "blue").help("Tests default values for consts.").def("green");
40     parser.addOption(["-V", "--value"], ArgType.Integer).help("Stores a single number").argName("NUMBER");
41     parser.addOption(["-l", "--list"], Action.Append, ArgType.Integer).help("Adds numbers to a list.").argName("NUMBER");
42     parser.addOption(["-v", "--verbose"], Action.Count).help("Counts the number of times this option appears.");
43     parser.addOption(["-h", "--help"], Action.Help).help("Displays a help message.");
44     auto options = parser.parse(args);
45
46     print_opts(options);
47
48     return 0;
49 }
Note: See TracBrowser for help on using the browser.