-
Notifications
You must be signed in to change notification settings - Fork 0
/
tester.kit
155 lines (133 loc) · 4.51 KB
/
tester.kit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import src.helperfunctions;
import src.choice;
import src.argument;
import src.arguments;
import src.command;
import src.handler;
// import kitten.arguments;
// import kitten._kitten_packages.arguments.src._import_all;
// importfrom("kitten.arguments");
// import kitten.arguments.*;
// function demoFunc(val: Choice, arg: Argument, unused2: Arguments): Void {
// printf("%s: ", arg.longName);
// val.printLn();
// }
struct Res1 {
var values: Vector[CString];
var valueCount: Int = 0;
static function new(): Res1 {
return struct Self {
values: Vector.new(10),
valueCount: 0
};
}
}
implement Handler for Res1 {
function nothing(cmd: Command): Void {
printf("empty input\n");
}
function extra(choice: Choice, args: Arguments): Void {
// printf("extra: `");
// choice.print();
// printf("`\n");
var capacity: Int = 256;
var str: Ptr[Char] = *(mallocator.calloc(capacity)): Ptr[Char];
match choice.which {
INT => snprintf(str, capacity, "%d\n", choice.intVal);
STR => snprintf(str, capacity, "%s\n", choice.strVal);
BOOL => snprintf(str, capacity, "%s\n", if choice.boolVal then "true" else "false");
FLOAT => snprintf(str, capacity, "%f\n", choice.floatVal);
NONE => snprintf(str, capacity, "No value\n");
}
this.valueCount++;
this.values.push(str);
}
function end(cmd: Command): Void {
printf("1\n");
for n in 0 ... this.valueCount {
printf("%s", this.values[n]);
}
}
function arg(choice: Choice, arg: Argument, args: Arguments): Void {
match arg.shortName {
c'h' => {
args.printAll();
}
default => {
printf("%s: ", arg.longName);
choice.printLn();
}
}
}
}
struct Res2 {
var values: Vector[CString];
var valueCount: Int = 0;
static function new(): Res2 {
return struct Self {
values: Vector.new(10),
valueCount: 0
};
}
}
implement Handler for Res2 {
function nothing(cmd: Command): Void {
printf("empty input\n");
}
function extra(choice: Choice, args: Arguments): Void {
// printf("extra: `");
// choice.print();
// printf("`\n");
var capacity: Int = 256;
var str: Ptr[Char] = *(mallocator.calloc(capacity)): Ptr[Char];
match choice.which {
INT => snprintf(str, capacity, "%d\n", choice.intVal);
STR => snprintf(str, capacity, "%s\n", choice.strVal);
BOOL => snprintf(str, capacity, "%s\n", if choice.boolVal then "true" else "false");
FLOAT => snprintf(str, capacity, "%f\n", choice.floatVal);
NONE => snprintf(str, capacity, "No value\n");
}
this.valueCount++;
this.values.push(str);
}
function end(cmd: Command): Void {
printf("2\n");
for n in 0 ... this.valueCount {
printf("%s", this.values[n]);
}
}
function arg(choice: Choice, arg: Argument, args: Arguments): Void {
match arg.shortName {
c'h' => {
args.printAll();
}
default => {
printf("%s: ", arg.longName);
choice.printLn();
}
}
}
}
// function helpHandler(unused: Choice, one: Argument, all: Arguments): Void {
// if one.func != defaultHandler {
// one.printAll();
// } else {
// all.printAll();
// }
// }
function main(argc: Int, argv: Ptr[Ptr[Char]]) {
var results1: Res1 = Res1.new();
var results2: Res2 = Res2.new();
var test: Arguments = Arguments.new(results1);
test.add(c'v', "val", Choice.newStr("abc"), "This is a test", required_argument);
test.add(c'a', "aaaa", Choice.new(), "This is the second cli argument", no_argument);
test.add(c'n', "new", Choice.newInt(0), "This is the third cli argument", optional_argument);
test.add(c'h', "help", Choice.new(), "This gets help", no_argument);
var test2: Arguments = Arguments.new(results2);
test2.add(c'b', "blah", Choice.newInt(0), "foo > first arg", optional_argument);
test2.add(c'd', "do", Choice.newInt(0), "foo > second arg", required_argument);
test2.add(c'h', "help", Choice.new(), "This gets help", no_argument);
var command: Command = Command.new(test);
command.addSubcommand("foo", test2);
command.process(argc, argv);
}