-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserp.c3
57 lines (49 loc) · 1.4 KB
/
serp.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
module serp;
import std::io;
fn void! root_cmd(Command *c, String[] args, Option[] opts)
{
return show_args_and_opts("root", c, args, opts);
}
fn void! init_cmd(Command *c, String[] args, Option[] opts)
{
return show_args_and_opts("init", c, args, opts);
}
fn void! add_cmd(Command *c, String[] args, Option[] opts)
{
return show_args_and_opts("add", c, args, opts);
}
fn void! show_args_and_opts(String cmd, Command *c, String[] args, Option[] opts)
{
io::printfn("Command: %s", cmd);
io::printn("--------");
io::printfn("Positional arguments:\n\t%s", args);
if (opts.len)
{
io::printfn("\nParsed flags:");
foreach (opt : opts) io::printf("\t%s", opt);
}
io::printf("\nUsage message:\n");
io::printf("--------------");
return c.usage();
}
fn void! main(String[] args)
{
mem::@report_heap_allocs_in_scope()
{
Command root = command::new(use: "app [commands] [flags]", run: &root_cmd)
.set_desc("describe your command here")
.add_command(
command::new(use: "init [flags]", run: &init_cmd)
.add_flag({.shortopt = 'p', .desc = "path", .optarg = true})
.add_flag({.shortopt = 'n', .desc = "name", .optarg = true})
.set_desc("'init' your app with this command")
)
.add_command(
command::new(use: "add [flags] <name>", run: &add_cmd)
.add_flag({.shortopt = 'f', .longopt = "force"})
.set_desc("'add' something to your app")
);
defer root.free();
return root.execute(args);
};
}