1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cli: various improvements (#6180)

This commit is contained in:
Lukas Neubert
2020-08-20 23:14:53 +02:00
committed by GitHub
parent b88569c845
commit 93e6c3df6a
6 changed files with 71 additions and 73 deletions

View File

@ -1,29 +1,30 @@
module main
import cli
import cli { Command, Flag }
import os
fn main() {
mut cmd := cli.Command{
mut cmd := Command{
name: 'cli'
description: 'An example of the cli library.'
version: '1.0.0'
}
mut greet_cmd := cli.Command{
mut greet_cmd := Command{
name: 'greet'
description: 'Prints greeting in different languages.'
required_args: 1
pre_execute: greet_pre_func
execute: greet_func
post_execute: greet_post_func
}
greet_cmd.add_flag(cli.Flag{
greet_cmd.add_flag(Flag{
flag: .string
required: true
name: 'language'
abbrev: 'l'
description: 'Language of the message.'
})
greet_cmd.add_flag(cli.Flag{
greet_cmd.add_flag(Flag{
flag: .int
name: 'times'
value: '3'
@ -33,23 +34,24 @@ fn main() {
cmd.parse(os.args)
}
fn greet_func(cmd cli.Command) {
fn greet_func(cmd Command) {
language := cmd.flags.get_string('language') or {
panic("Failed to get \'language\' flag: $err")
panic('Failed to get `language` flag: $err')
}
times := cmd.flags.get_int('times') or {
panic("Failed to get \'times\' flag: $err")
panic('Failed to get `times` flag: $err')
}
name := cmd.args[0]
for _ in 0 .. times {
match language {
'english' {
println('Hello World')
println('Welcome $name')
}
'german' {
println('Hallo Welt')
println('Willkommen $name')
}
'dutch' {
println('Hallo Wereld')
println('Welkom $name')
}
else {
println('Unsupported language')
@ -60,10 +62,10 @@ fn greet_func(cmd cli.Command) {
}
}
fn greet_pre_func(cmd cli.Command) {
fn greet_pre_func(cmd Command) {
println('This is a function running before the main function.\n')
}
fn greet_post_func(cmd cli.Command) {
fn greet_post_func(cmd Command) {
println('\nThis is a function running after the main function.')
}