2019-11-21 13:03:12 +01:00
|
|
|
module main
|
|
|
|
|
2020-08-20 23:14:53 +02:00
|
|
|
import cli { Command, Flag }
|
2020-04-26 13:49:31 +02:00
|
|
|
import os
|
2019-11-21 13:03:12 +01:00
|
|
|
|
|
|
|
fn main() {
|
2020-08-20 23:14:53 +02:00
|
|
|
mut cmd := 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
|
|
|
}
|
2020-08-20 23:14:53 +02:00
|
|
|
mut greet_cmd := Command{
|
2020-08-19 14:38:45 +02:00
|
|
|
name: 'greet'
|
|
|
|
description: 'Prints greeting in different languages.'
|
2020-09-16 22:01:44 +02:00
|
|
|
usage: '<name>'
|
2020-08-20 23:14:53 +02:00
|
|
|
required_args: 1
|
2020-08-19 14:38:45 +02:00
|
|
|
pre_execute: greet_pre_func
|
|
|
|
execute: greet_func
|
|
|
|
post_execute: greet_post_func
|
2019-11-21 13:03:12 +01:00
|
|
|
}
|
2020-08-20 23:14:53 +02:00
|
|
|
greet_cmd.add_flag(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
|
|
|
})
|
2020-08-20 23:14:53 +02:00
|
|
|
greet_cmd.add_flag(Flag{
|
2020-08-19 14:38:45 +02:00
|
|
|
flag: .int
|
|
|
|
name: 'times'
|
2021-01-22 18:03:02 +01:00
|
|
|
value: ['3']
|
2020-08-19 14:38:45 +02:00
|
|
|
description: 'Number of times the message gets printed.'
|
2019-11-21 13:03:12 +01:00
|
|
|
})
|
2021-01-22 18:03:02 +01:00
|
|
|
greet_cmd.add_flag(Flag{
|
2021-02-19 11:43:18 +01:00
|
|
|
flag: .string
|
|
|
|
name: 'fun'
|
|
|
|
multiple: true
|
|
|
|
description: 'Just a dumby flags to show multiple.'
|
2021-01-22 18:03:02 +01:00
|
|
|
})
|
2019-11-21 13:03:12 +01:00
|
|
|
cmd.add_command(greet_cmd)
|
2021-01-22 18:03:02 +01:00
|
|
|
cmd.setup()
|
2019-11-21 13:03:12 +01:00
|
|
|
cmd.parse(os.args)
|
|
|
|
}
|
|
|
|
|
2021-02-19 11:43:18 +01:00
|
|
|
fn greet_func(cmd Command) ? {
|
2020-08-19 14:38:45 +02:00
|
|
|
language := cmd.flags.get_string('language') or {
|
2020-08-20 23:14:53 +02:00
|
|
|
panic('Failed to get `language` flag: $err')
|
2020-08-19 14:38:45 +02:00
|
|
|
}
|
|
|
|
times := cmd.flags.get_int('times') or {
|
2020-08-20 23:14:53 +02:00
|
|
|
panic('Failed to get `times` flag: $err')
|
2020-08-19 14:38:45 +02:00
|
|
|
}
|
2020-08-20 23:14:53 +02:00
|
|
|
name := cmd.args[0]
|
2020-08-19 14:38:45 +02:00
|
|
|
for _ in 0 .. times {
|
2019-11-21 13:03:12 +01:00
|
|
|
match language {
|
2020-09-16 22:01:44 +02:00
|
|
|
'english', 'en' {
|
2020-08-20 23:14:53 +02:00
|
|
|
println('Welcome $name')
|
2020-08-19 14:38:45 +02:00
|
|
|
}
|
2020-09-16 22:01:44 +02:00
|
|
|
'german', 'de' {
|
2020-08-20 23:14:53 +02:00
|
|
|
println('Willkommen $name')
|
2020-08-19 14:38:45 +02:00
|
|
|
}
|
2020-09-16 22:01:44 +02:00
|
|
|
'dutch', 'nl' {
|
2020-08-20 23:14:53 +02:00
|
|
|
println('Welkom $name')
|
2020-08-19 14:38:45 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
println('Unsupported language')
|
2020-08-27 22:15:49 +02:00
|
|
|
println('Supported languages are `english`, `german` and `dutch`.')
|
2020-08-19 14:38:45 +02:00
|
|
|
break
|
2020-05-28 13:32:43 +02:00
|
|
|
}
|
2019-11-21 13:03:12 +01:00
|
|
|
}
|
|
|
|
}
|
2021-01-22 18:03:02 +01:00
|
|
|
fun := cmd.flags.get_strings('fun') or {
|
|
|
|
panic('Failed to get `fun` flag: $err')
|
|
|
|
}
|
|
|
|
for f in fun {
|
|
|
|
println('fun: ${f}')
|
|
|
|
}
|
2019-11-21 13:03:12 +01:00
|
|
|
}
|
2020-03-10 16:11:17 +01:00
|
|
|
|
2021-02-19 11:43:18 +01:00
|
|
|
fn greet_pre_func(cmd 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
|
|
|
}
|
|
|
|
|
2021-02-19 11:43:18 +01:00
|
|
|
fn greet_post_func(cmd 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
|
|
|
}
|