2019-11-21 13:03:12 +01:00
|
|
|
module main
|
|
|
|
|
2020-04-26 13:49:31 +02:00
|
|
|
import cli
|
|
|
|
import os
|
2019-11-21 13:03:12 +01:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
mut cmd := cli.Command{
|
2020-08-19 14:38:45 +02:00
|
|
|
name: 'cli'
|
|
|
|
description: 'An example of the cli library.'
|
|
|
|
version: '1.0.0'
|
2019-11-21 13:03:12 +01:00
|
|
|
}
|
|
|
|
mut greet_cmd := cli.Command{
|
2020-08-19 14:38:45 +02:00
|
|
|
name: 'greet'
|
|
|
|
description: 'Prints greeting in different languages.'
|
|
|
|
pre_execute: greet_pre_func
|
|
|
|
execute: greet_func
|
|
|
|
post_execute: greet_post_func
|
2019-11-21 13:03:12 +01:00
|
|
|
}
|
|
|
|
greet_cmd.add_flag(cli.Flag{
|
2020-08-19 14:38:45 +02:00
|
|
|
flag: .string
|
|
|
|
required: true
|
|
|
|
name: 'language'
|
|
|
|
abbrev: 'l'
|
|
|
|
description: 'Language of the message.'
|
2019-11-21 13:03:12 +01:00
|
|
|
})
|
|
|
|
greet_cmd.add_flag(cli.Flag{
|
2020-08-19 14:38:45 +02:00
|
|
|
flag: .int
|
|
|
|
name: 'times'
|
|
|
|
value: '3'
|
|
|
|
description: 'Number of times the message gets printed.'
|
2019-11-21 13:03:12 +01:00
|
|
|
})
|
|
|
|
cmd.add_command(greet_cmd)
|
|
|
|
cmd.parse(os.args)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn greet_func(cmd cli.Command) {
|
2020-08-19 14:38:45 +02:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
for _ in 0 .. times {
|
2019-11-21 13:03:12 +01:00
|
|
|
match language {
|
2020-08-19 14:38:45 +02:00
|
|
|
'english' {
|
|
|
|
println('Hello World')
|
|
|
|
}
|
|
|
|
'german' {
|
|
|
|
println('Hallo Welt')
|
|
|
|
}
|
|
|
|
'dutch' {
|
|
|
|
println('Hallo Wereld')
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
println('Unsupported language')
|
|
|
|
println('Supported are `englisch`, `german` and `dutch`.')
|
|
|
|
break
|
2020-05-28 13:32:43 +02:00
|
|
|
}
|
2019-11-21 13:03:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-10 16:11:17 +01:00
|
|
|
|
|
|
|
fn greet_pre_func(cmd cli.Command) {
|
2020-08-19 14:38:45 +02:00
|
|
|
println('This is a function running before the main function.\n')
|
2020-03-10 16:11:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn greet_post_func(cmd cli.Command) {
|
2020-08-19 14:38:45 +02:00
|
|
|
println('\nThis is a function running after the main function.')
|
2020-03-27 18:01:46 +01:00
|
|
|
}
|