-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.v
73 lines (61 loc) · 1.3 KB
/
app.v
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
module clive
@[noinit]
pub struct App {
pub:
name string @[required]
description string
version string
pub mut:
cmds Cmds
}
// vfmt off
pub fn App.new(attrs struct {
name string
description string
version string
}) App {
// vfmt on
mut app := App{
name: attrs.name
description: attrs.description
version: attrs.version
}
app.cmds.add(Cmd.new(name: 'help'))
app.cmds.add(Cmd.new(name: 'version'))
return app
}
pub fn (self App) parse(os_args []string) !Cmd {
mut input := os_args.clone()
// Remove the app name.
input.drop(1)
// parse the cli input and return the Cmd
// that needs to be executed.
for input.len > 0 {
identifier := input.first()
input.drop(1)
if mut cmd := self.cmds.get(identifier) {
if cmd.name == 'help' {
self.help()!
return self.cmds.get('help') or {}
}
if cmd.name == 'version' {
self.version()
return self.cmds.get('version') or {}
}
return cmd.parse(mut input)!
} else {
error_message := err_msg('cmd_unexpected', cmd: identifier)
return error(error_message)
}
}
// If we get here, there were no tokens to parse.
self.help()!
return self.cmds.get('help') or {}
}
fn (self App) version() {
mut ver := 'undefined'
if !self.version.is_blank() {
ver = self.version
}
println('${self.name} version ${ver}')
}