mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
vlib: cli module
This commit is contained in:

committed by
Alexander Medvednikov

parent
8c7f5d5cd8
commit
597a6fead2
50
examples/cli.v
Normal file
50
examples/cli.v
Normal file
@ -0,0 +1,50 @@
|
||||
module main
|
||||
|
||||
import (
|
||||
cli
|
||||
os
|
||||
)
|
||||
|
||||
fn main() {
|
||||
mut cmd := cli.Command{
|
||||
name: 'cli',
|
||||
description: 'An example of the cli library',
|
||||
version: '1.0.0',
|
||||
}
|
||||
|
||||
mut greet_cmd := cli.Command{
|
||||
name: 'greet',
|
||||
description: 'Prints greeting in different languages',
|
||||
execute: greet_func,
|
||||
}
|
||||
greet_cmd.add_flag(cli.Flag{
|
||||
flag: .string,
|
||||
required: true,
|
||||
name: 'language',
|
||||
abbrev: 'l',
|
||||
description: 'Language of the message'
|
||||
})
|
||||
greet_cmd.add_flag(cli.Flag{
|
||||
flag: .int,
|
||||
name: 'times',
|
||||
value: '3',
|
||||
description: 'Number of times the message gets printed'
|
||||
})
|
||||
|
||||
cmd.add_command(greet_cmd)
|
||||
cmd.parse(os.args)
|
||||
}
|
||||
|
||||
fn greet_func(cmd cli.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') }
|
||||
|
||||
for i := 0; i < times; i++ {
|
||||
match language {
|
||||
'english' { println('Hello World') }
|
||||
'german' { println('Hallo Welt') }
|
||||
'dutch' { println('Hallo Wereld') }
|
||||
else { println('unsupported language') }
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user