-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand.c3
132 lines (117 loc) · 2.81 KB
/
command.c3
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
module serp::command;
import serp::getopt;
import std::collections::list;
import std::io;
def RunFunc = fn void!(Command *c, String[], Option[]);
struct Command
{
String use;
RunFunc run;
String desc;
List(<Flag>) flags;
List(<Command>) cmds;
Allocator allocator;
}
fn Command new(String use, RunFunc run, Allocator allocator = allocator::heap())
{
Command cmd = {.use = use, .run = run, .allocator = allocator};
cmd.flags.new_init(4, allocator);
cmd.cmds.new_init(4, allocator);
return cmd;
}
fn Command Command.init(&self, String use, RunFunc run, Allocator allocator)
{
*self = {.use = use, .run = run, .allocator = allocator};
self.flags.new_init(4, allocator);
self.cmds.new_init(4, allocator);
return *self;
}
fn void Command.free(&self)
{
self.flags.free();
foreach (cmd : self.cmds) cmd.free();
self.cmds.free();
if (self.desc) self.desc.free();
}
fn Command Command.set_desc(&self, String s)
{
self.desc = s.copy(self.allocator);
return *self;
}
fn Command Command.add_flag(&self, Flag f)
{
self.flags.push(f);
return *self;
}
fn Command Command.add_command(&self, Command c)
{
self.cmds.push(c);
return *self;
}
fn void! Command.usage(&self, OutStream out = io::stdout())
{
const PREFIX = " ";
if (self.desc) io::fprintf(out, "\n%s\n", self.desc)!;
io::fprintf(out, "\nUsage:\n%s%s\n", PREFIX, self.use)!;
if (self.cmds.len() > 0)
{
io::fprintf(out, "\nAvailabe Commands:\n")!;
foreach (sub : self.cmds)
{
String cmd = sub.use;
if (try n = cmd.index_of_char(' ')) cmd = cmd[:n];
io::fprintf(out, "%s%s\t%s\n", PREFIX, cmd, self.desc)!;
}
}
if (self.flags.len() > 0)
{
io::fprintf(out, "\nFlags:\n")!;
foreach (f : self.flags)
{
io::fprintf(out, "%s", PREFIX)!;
if (f.shortopt) io::fprintf(out, "-%c", f.shortopt)!;
if (f.shortopt && f.longopt)
{
io::fprintf(out, ", ")!;
}
else
{
io::fprintf(out, " ")!;
}
if (f.longopt) io::fprintf(out, "--%s", f.longopt)!;
if (f.optarg) io::fprintf(out, " string")!;
if (f.desc) io::fprintf(out, "\t%s", f.desc)!;
io::fprintf(out, "\n")!;
}
io::fprintf(out, "\n")!;
}
}
fn void! Command.execute(&self, String[] args)
{
int optind;
@pool(self.allocator)
{
Option[]! opts = getopt::parse_temp(args, self.flags.to_tarray(), &optind);
if (catch err = opts)
{
io::eprintf("Failed to parse option: %s\n", err);
return self.usage();
}
if (optind < args.len) {
String arg = args[optind];
foreach (sub : self.cmds)
{
String cmd = sub.use;
if (try n = cmd.index_of_char(' ')) cmd = cmd[:n];
if (arg == cmd)
{
io::printfn("execute sub-command: %s", cmd);
return sub.execute(args[optind..]);
}
}
io::printfn("positional arguments: %s", args[optind..]);
}
io::printfn("run command: %s", self.use);
return self.run(self, args[optind..], opts);
};
}