blob: 9232aac5e7d1d0deb1cc160b745e5833780768df (
plain)
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
|
module main
import cli { Command, Flag }
import os
fn main() {
mut cmd := Command{
name: 'cli'
description: 'An example of the cli library.'
version: '1.0.0'
}
mut greet_cmd := Command{
name: 'greet'
description: 'Prints greeting in different languages.'
usage: '<name>'
required_args: 1
pre_execute: greet_pre_func
execute: greet_func
post_execute: greet_post_func
}
greet_cmd.add_flag(Flag{
flag: .string
required: true
name: 'language'
abbrev: 'l'
description: 'Language of the message.'
})
greet_cmd.add_flag(Flag{
flag: .int
name: 'times'
default_value: ['3']
description: 'Number of times the message gets printed.'
})
greet_cmd.add_flag(Flag{
flag: .string_array
name: 'fun'
description: 'Just a dumby flags to show multiple.'
})
cmd.add_command(greet_cmd)
cmd.setup()
cmd.parse(os.args)
}
fn greet_func(cmd Command) ? {
language := cmd.flags.get_string('language') or { panic('Failed to get `language` flag: $err') }
times := cmd.flags.get_int('times') or { panic('Failed to get `times` flag: $err') }
name := cmd.args[0]
for _ in 0 .. times {
match language {
'english', 'en' {
println('Welcome $name')
}
'german', 'de' {
println('Willkommen $name')
}
'dutch', 'nl' {
println('Welkom $name')
}
else {
println('Unsupported language')
println('Supported languages are `english`, `german` and `dutch`.')
break
}
}
}
fun := cmd.flags.get_strings('fun') or { panic('Failed to get `fun` flag: $err') }
for f in fun {
println('fun: $f')
}
}
fn greet_pre_func(cmd Command) ? {
println('This is a function running before the main function.\n')
}
fn greet_post_func(cmd Command) ? {
println('\nThis is a function running after the main function.')
}
|