examples: cli: more readable and informative program output (#6168)
parent
217f04e311
commit
e6e1011e47
|
@ -5,57 +5,65 @@ import os
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
mut cmd := cli.Command{
|
mut cmd := cli.Command{
|
||||||
name: 'cli',
|
name: 'cli'
|
||||||
description: 'An example of the cli library',
|
description: 'An example of the cli library.'
|
||||||
version: '1.0.0',
|
version: '1.0.0'
|
||||||
}
|
}
|
||||||
|
|
||||||
mut greet_cmd := cli.Command{
|
mut greet_cmd := cli.Command{
|
||||||
name: 'greet',
|
name: 'greet'
|
||||||
description: 'Prints greeting in different languages',
|
description: 'Prints greeting in different languages.'
|
||||||
pre_execute: greet_pre_func,
|
pre_execute: greet_pre_func
|
||||||
execute: greet_func,
|
execute: greet_func
|
||||||
post_execute: greet_post_func,
|
post_execute: greet_post_func
|
||||||
}
|
}
|
||||||
greet_cmd.add_flag(cli.Flag{
|
greet_cmd.add_flag(cli.Flag{
|
||||||
flag: .string,
|
flag: .string
|
||||||
required: true,
|
required: true
|
||||||
name: 'language',
|
name: 'language'
|
||||||
abbrev: 'l',
|
abbrev: 'l'
|
||||||
description: 'Language of the message'
|
description: 'Language of the message.'
|
||||||
})
|
})
|
||||||
greet_cmd.add_flag(cli.Flag{
|
greet_cmd.add_flag(cli.Flag{
|
||||||
flag: .int,
|
flag: .int
|
||||||
name: 'times',
|
name: 'times'
|
||||||
value: '3',
|
value: '3'
|
||||||
description: 'Number of times the message gets printed'
|
description: 'Number of times the message gets printed.'
|
||||||
})
|
})
|
||||||
|
|
||||||
cmd.add_command(greet_cmd)
|
cmd.add_command(greet_cmd)
|
||||||
cmd.parse(os.args)
|
cmd.parse(os.args)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn greet_func(cmd cli.Command) {
|
fn greet_func(cmd cli.Command) {
|
||||||
language := cmd.flags.get_string('language') or { panic('failed to get \'language\' flag: $err') }
|
language := cmd.flags.get_string('language') or {
|
||||||
times := cmd.flags.get_int('times') or { panic('failed to get \'times\' flag: $err') }
|
panic("Failed to get \'language\' flag: $err")
|
||||||
|
}
|
||||||
for _ in 0..times {
|
times := cmd.flags.get_int('times') or {
|
||||||
|
panic("Failed to get \'times\' flag: $err")
|
||||||
|
}
|
||||||
|
for _ in 0 .. times {
|
||||||
match language {
|
match language {
|
||||||
'english' { println('Hello World') }
|
'english' {
|
||||||
'german' { println('Hallo Welt') }
|
println('Hello World')
|
||||||
'dutch' { println('Hallo Wereld') }
|
}
|
||||||
else {
|
'german' {
|
||||||
println('unsupported language')
|
println('Hallo Welt')
|
||||||
break
|
}
|
||||||
|
'dutch' {
|
||||||
|
println('Hallo Wereld')
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
println('Unsupported language')
|
||||||
|
println('Supported are `englisch`, `german` and `dutch`.')
|
||||||
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn greet_pre_func(cmd cli.Command) {
|
fn greet_pre_func(cmd cli.Command) {
|
||||||
println('This is a function running before the main function')
|
println('This is a function running before the main function.\n')
|
||||||
}
|
}
|
||||||
|
|
||||||
fn greet_post_func(cmd cli.Command) {
|
fn greet_post_func(cmd cli.Command) {
|
||||||
println('This is a function running after the main function')
|
println('\nThis is a function running after the main function.')
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue