root/misc/opttest_tango.d

Revision 129, 3.0 kB (checked in by KirkMcDonald, 7 years ago)

Updated opttest_tango.

Line 
1 module opttest_tango;
2
3 //import std.stdio : Stdout.formatln;
4 import tango.io.Stdout : Stdout;
5 import tango.text.convert.Layout : Layout;
6 import tango.text.Util : join;
7
8 import optparse;
9
10 Layout!(char) layout;
11
12 char[] format(T)(T t) {
13     return layout.convert("{0}", t);
14 }
15
16 char[] format_arr(T)(T[] arr) {
17     char[][] result = new char[][](arr.length);
18     foreach (i, s; arr) {
19         result[i] = format(s);
20         //if (i != arr.length-1) result ~= ",";
21     }
22     //result ~= "]";
23     return "[" ~ join(result, ",") ~ "]";
24 }
25
26 void print_opts(Options options) {
27     Stdout.formatln("file:        {0}", options["file"]);
28     Stdout.formatln("imports:     {0}", format_arr(options.list("importPath")));
29     Stdout.formatln("verbosity:   {0}", options.count("verbose"));
30     Stdout.formatln("value:       {0}", options.value("value"));
31     Stdout.formatln("number list: {0}", format_arr(options.valueList("list")));
32     Stdout.formatln("switch:      {0}", options.flag("switch"));
33     Stdout.formatln("args:        {0}", format_arr(options.args));
34 }
35
36 int main(char[][] args) {
37     layout = new Layout!(char);
38     //Stdout.println(format_arr(args));
39     auto parser = new OptionParser("A test suite for optparser.");
40     parser.addOption("-f", "--file").help("Stores a single argument.");
41     parser.addOption(["-I", "--import"], "importPath", Action.Append).help("Adds arguments to a list.");
42     parser.addOption(["-c", "--callback"], {
43         Stdout.formatln("callback encountered");
44     }).help("Calls a callback.");
45     parser.addOption(["-n", "--number"], (int i) {
46         Stdout.formatln("number callback: {0} * 2 = {1}", i, i*2);
47     }).help("Calls a callback with a number.");
48     // A fancy callback
49     parser.addOption(["--fancy"], "a callback", (Options opts, inout char[][] a, inout int i, char[] name) {
50         Stdout.formatln("Current file: {0}", opts["file"]);
51         Stdout.formatln("Remaining args: {0}", a[i+1 .. $]);
52         // Skip the next arg
53         ++i;
54         Stdout.formatln("This arg's name: {0}", name);
55     }).help("Tests optparse's fancy callbacks.");
56     parser.addOption(["--enable"], "switch", Action.SetTrue).help("Enables the switch.");
57     parser.addOption(["--disable"], "switch", Action.SetFalse).help("Disables the switch.");
58     parser.addOption(["--turn-off"], "turn", Action.SetFalse).help("Tests default values for flags.").def(true);
59     parser.addOption(["--set-blue"], "color", Action.StoreConst, "blue").help("Tests default values for consts.").def("green");
60     parser.addOption(["-V", "--value"], ArgType.Integer).help("Stores a single number").argName("NUMBER");
61     parser.addOption(["-l", "--list"], Action.Append, ArgType.Integer).help("Adds numbers to a list.").argName("NUMBER");
62     parser.addOption(["-v", "--verbose"], Action.Count).help("Counts the number of times this option appears.");
63     parser.addOption(["-h", "--help"], Action.Help).help("Displays a help message.");
64     auto options = parser.parse(args);
65
66     print_opts(options);
67
68     return 0;
69 }
Note: See TracBrowser for help on using the browser.