From 52be7b6cdab1a5d55551ac355034c2f7286ab17d Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Fri, 4 Mar 2022 11:52:46 +0200 Subject: [PATCH] some more cleanup --- vlib/cli/command.v | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/vlib/cli/command.v b/vlib/cli/command.v index b5ba43be98..dac066d2eb 100644 --- a/vlib/cli/command.v +++ b/vlib/cli/command.v @@ -94,8 +94,7 @@ pub fn (mut cmd Command) add_commands(commands []Command) { pub fn (mut cmd Command) add_command(command Command) { mut subcmd := command if cmd.commands.contains(subcmd.name) { - println('Command with the name `$subcmd.name` already exists') - exit(1) + eprintln_exit('Command with the name `$subcmd.name` already exists') } subcmd.parent = unsafe { cmd } cmd.commands << subcmd @@ -121,8 +120,7 @@ pub fn (mut cmd Command) add_flags(flags []Flag) { // add_flag adds `flag` to this `Command`. pub fn (mut cmd Command) add_flag(flag Flag) { if cmd.flags.contains(flag.name) { - println('Flag with the name `$flag.name` already exists') - exit(1) + eprintln_exit('Flag with the name `$flag.name` already exists') } cmd.flags << flag } @@ -183,16 +181,14 @@ fn (mut cmd Command) parse_flags() { found = true flag.found = true cmd.args = flag.parse(cmd.args, cmd.posix_mode) or { - println('Failed to parse flag `${cmd.args[0]}`: $err') - exit(1) + eprintln_exit('Failed to parse flag `${cmd.args[0]}`: $err') } break } } } if !found { - println('Command `$cmd.name` has no flag `${cmd.args[0]}`') - exit(1) + eprintln_exit('Command `$cmd.name` has no flag `${cmd.args[0]}`') } } } @@ -223,18 +219,17 @@ fn (mut cmd Command) parse_commands() { // if no further command was found, execute current command if cmd.required_args > 0 { if cmd.required_args > cmd.args.len { - eprintln('Command `$cmd.name` needs at least $cmd.required_args arguments') - exit(1) + eprintln_exit('Command `$cmd.name` needs at least $cmd.required_args arguments') } } cmd.check_required_flags() - cmd.handle_error('preexecution', cmd.pre_execute) - cmd.handle_error('execution', cmd.execute) - cmd.handle_error('postexecution', cmd.post_execute) + cmd.handle_cb(cmd.pre_execute, 'preexecution') + cmd.handle_cb(cmd.execute, 'execution') + cmd.handle_cb(cmd.post_execute, 'postexecution') } -fn (mut cmd Command) handle_error(label string, cb FnCommandCallback) { +fn (mut cmd Command) handle_cb(cb FnCommandCallback, label string) { if !isnil(cb) { cb(*cmd) or { label_message := if term.can_show_color_on_stderr() { @@ -242,8 +237,7 @@ fn (mut cmd Command) handle_error(label string, cb FnCommandCallback) { } else { 'cli $label error:' } - eprintln('$label_message $err') - exit(1) + eprintln_exit('$label_message $err') } } } @@ -273,8 +267,7 @@ fn (cmd Command) check_required_flags() { for flag in cmd.flags { if flag.required && flag.value.len == 0 { full_name := cmd.full_name() - println('Flag `$flag.name` is required by `$full_name`') - exit(1) + eprintln_exit('Flag `$flag.name` is required by `$full_name`') } } } @@ -307,3 +300,9 @@ fn (cmds []Command) contains(name string) bool { } return false } + +[noreturn] +fn eprintln_exit(message string) { + eprintln(message) + exit(1) +}