Commit ef2200684118cddc8b84c69e04bf3fb41d9bb274
1 parent
7af04aca
Let example.nim double as unit test
Showing
1 changed file
with
46 additions
and
26 deletions
example.nim
1 | import lapp, tables | 1 | import lapp, tables |
2 | +from os import paramStr, paramCount | ||
2 | 3 | ||
3 | # This is a trivial example trying to cover all features of lapp. | 4 | # This is a trivial example trying to cover all features of lapp. |
4 | 5 | ||
@@ -6,7 +7,7 @@ let help = """ | @@ -6,7 +7,7 @@ let help = """ | ||
6 | example [options] command filenames | 7 | example [options] command filenames |
7 | 8 | ||
8 | -r Just an optional single character flag. Show the args using repr. | 9 | -r Just an optional single character flag. Show the args using repr. |
9 | - -a,--alpha Or with additional long variant, no whitespace there. | 10 | + -t,--test Or with additional long variant, no whitespace there. |
10 | -n: A colon can be added, but has no meaning, instead () specifies further. | 11 | -n: A colon can be added, but has no meaning, instead () specifies further. |
11 | -N (default 10) And it can take a value, and have a default value. | 12 | -N (default 10) And it can take a value, and have a default value. |
12 | -f (default 0.04) And be a float | 13 | -f (default 0.04) And be a float |
@@ -28,30 +29,49 @@ let help = """ | @@ -28,30 +29,49 @@ let help = """ | ||
28 | ranges. The lexer in lapp does it, but there is no more support. | 29 | ranges. The lexer in lapp does it, but there is no more support. |
29 | """ | 30 | """ |
30 | 31 | ||
31 | -# We call `parse` in lapp with our help text as argument. | ||
32 | -# This will both parse the help text above and then, parse the | ||
33 | -# arguments passed to this program. A table is returned | ||
34 | -# with all command line elements in it, keyed by their name | ||
35 | -# and with a PValue as value. | ||
36 | -var args = parse(help) | ||
37 | - | ||
38 | -# Let's examine what we got using repr | ||
39 | -for k,v in args: | ||
40 | - echo "Parameter: " & k & " PValue: " & repr(v) | ||
41 | - | ||
42 | -# Or print out a bit cleaner | ||
43 | -echo "Parameters and their values:" | ||
44 | -echo "r == " & $args["r"].asBool | ||
45 | -echo "alpha == " & $args["alpha"].asBool # Long name is used as key, if its specified | ||
46 | -echo "n == " & $args["n"].asBool | ||
47 | -echo "N == " & $args["N"].asInt | ||
48 | -echo "f == " & $args["f"].asFloat | ||
49 | -echo "s == " & $args["s"].asString | ||
50 | -echo "x == " & $args["x"].asInt | ||
51 | -echo "X == " & $args["X"].asSeq.map(proc(x:PValue):int = x.asInt) | ||
52 | -echo "verbose == " & $args["verbose"].asSeq.len | ||
53 | -echo "out == " & $args["out"].filename | ||
54 | -echo "command == " & $args["command"].asString | ||
55 | -echo "file == " & $args["file"].asSeq.map(proc(x:PValue):string = x.filename) | ||
56 | 32 | ||
33 | +# This part is for build testing lapp, we run a bunch of asserts | ||
34 | +if paramCount() == 1 and paramStr(1) == "test": | ||
35 | + var args = parseArguments(help, @["example", "-x1", "-X4", "cmd"]) | ||
36 | + assert(args["r"].asBool == false) | ||
37 | + assert(args["test"].asBool == false) | ||
38 | + assert(args["n"].asBool == false) | ||
39 | + assert(args["N"].asInt == 10) | ||
40 | + assert(args["f"].asFloat == 0.04) | ||
41 | + assert(args["s"].asString == "banana") | ||
42 | + assert(args["x"].asInt == 1) | ||
43 | + assert(args["X"].asSeq[0].asInt == 4) | ||
44 | + assert(args["verbose"].asSeq.len == 1) | ||
45 | + assert(args["verbose"].asSeq[0].asBool == false) | ||
46 | + assert(args["out"].filename == "stdout") | ||
47 | + assert(args["command"].asString == "cmd") | ||
48 | + assert(args["file"].asSeq.len == 1) | ||
49 | + assert(args["file"].asSeq[0].filename == "stdin") | ||
50 | +else: | ||
57 | 51 | ||
52 | + # We call `parse` in lapp with our help text as argument. | ||
53 | + # This will both parse the help text above and then, parse the | ||
54 | + # arguments passed to this program. A table is returned | ||
55 | + # with all command line elements in it, keyed by their name | ||
56 | + # and with a PValue as value. | ||
57 | + var args = parse(help) | ||
58 | + | ||
59 | + # Let's examine what we got using repr | ||
60 | + echo "A bit messy, but shows exactly what we got:\n\n" | ||
61 | + for k,v in args: | ||
62 | + echo "Parameter: " & k & " PValue: " & repr(v) | ||
63 | + | ||
64 | + # Or print out a bit cleaner | ||
65 | + echo "Parameters and their values more readable:" | ||
66 | + echo "r == " & $args["r"].asBool | ||
67 | + echo "test == " & $args["test"].asBool # Long name is used as key, if its specified | ||
68 | + echo "n == " & $args["n"].asBool | ||
69 | + echo "N == " & $args["N"].asInt | ||
70 | + echo "f == " & $args["f"].asFloat | ||
71 | + echo "s == " & $args["s"].asString | ||
72 | + echo "x == " & $args["x"].asInt | ||
73 | + echo "X == " & $args["X"].asSeq.map(proc(x:PValue):int = x.asInt) | ||
74 | + echo "verbose == " & $args["verbose"].asSeq.len | ||
75 | + echo "out == " & $args["out"].filename | ||
76 | + echo "command == " & $args["command"].asString | ||
77 | + echo "file == " & $args["file"].asSeq.map(proc(x:PValue):string = x.filename) |