Merge: doc: fixed some typos and other misc. corrections
[nit.git] / tests / test_opts.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import opts
16
17 var ctx = new OptionContext
18 var ot = new OptionText("List of options")
19 var ob = new OptionBool("Test of OptionBool", "-b", "--bool")
20 var oc = new OptionCount("Test of OptionCount", "-c", "--count")
21 var os = new OptionString("Test of OptionString", "-s", "--string")
22 var oi = new OptionInt("Test of OptionInt", 10, "-i", "--int")
23 var oa = new OptionArray("Test of OptionArray", "-a", "--array")
24 var oe = new OptionEnum(["zero", "one", "two", "tree"], "Test of OptionEnum", 1, "-e", "--enum")
25 ctx.add_option(ot, ob, oc, os, oi, oa, oe)
26
27 ctx.parse(args)
28
29 print "Arguments: {args.length}"
30 for x in args do
31 print x
32 end
33
34 print "Rest: {ctx.rest.length}"
35 for x in ctx.rest do
36 print x
37 end
38
39 var errors = ctx.errors
40 if errors.not_empty then
41 print "Errors: {errors.length}"
42 print ctx.errors.join("\n")
43 end
44
45 print "OptionBool: {ob.value}"
46 print "OptionCount: {oc.value}"
47 if os.value == null then os.value = ""
48 print "OptionString: {os.value.to_s}"
49 print "OptionInt: {oi.value}"
50 print "OptionArray: {oa.value}"
51 print "OptionEnum: {oe.value}"