From e6e1011e478f529eadbfacac19977a7199eef247 Mon Sep 17 00:00:00 2001 From: Lukas Neubert Date: Wed, 19 Aug 2020 14:38:45 +0200 Subject: [PATCH] examples: cli: more readable and informative program output (#6168) --- examples/cli.v | 70 ++++++++++++++++++++++++++++---------------------- 1 file changed, 39 insertions(+), 31 deletions(-) diff --git a/examples/cli.v b/examples/cli.v index 1d61c9df9a..f69cf0c0b8 100644 --- a/examples/cli.v +++ b/examples/cli.v @@ -5,57 +5,65 @@ import os fn main() { mut cmd := cli.Command{ - name: 'cli', - description: 'An example of the cli library', - version: '1.0.0', + 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', - pre_execute: greet_pre_func, - execute: greet_func, - post_execute: greet_post_func, + name: 'greet' + description: 'Prints greeting in different languages.' + pre_execute: greet_pre_func + execute: greet_func + post_execute: greet_post_func } greet_cmd.add_flag(cli.Flag{ - flag: .string, - required: true, - name: 'language', - abbrev: 'l', - description: 'Language of the message' + 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' + 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 _ in 0..times { + 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 { match language { - 'english' { println('Hello World') } - 'german' { println('Hallo Welt') } - 'dutch' { println('Hallo Wereld') } - else { - println('unsupported language') - break + 'english' { + println('Hello World') + } + 'german' { + println('Hallo Welt') + } + 'dutch' { + println('Hallo Wereld') + } + else { + println('Unsupported language') + println('Supported are `englisch`, `german` and `dutch`.') + break } } } } 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) { - println('This is a function running after the main function') + println('\nThis is a function running after the main function.') }