diff --git a/cmd/tools/vcheck-md.v b/cmd/tools/vcheck-md.v index 222f90faea5575..177df2294e48fd 100644 --- a/cmd/tools/vcheck-md.v +++ b/cmd/tools/vcheck-md.v @@ -44,7 +44,6 @@ fn (v1 CheckResult) + (v2 CheckResult) CheckResult { fn main() { if non_option_args.len == 0 || '-help' in os.args { help.print_and_exit('check-md') - exit(0) } if '-all' in os.args { println('´-all´ flag is deprecated. Please use ´v check-md .´ instead.') diff --git a/cmd/tools/vfmt.v b/cmd/tools/vfmt.v index 0e9d993d92b530..b850f8c69058b2 100644 --- a/cmd/tools/vfmt.v +++ b/cmd/tools/vfmt.v @@ -89,7 +89,6 @@ fn main() { } if files.len == 0 || '-help' in args || '--help' in args { help.print_and_exit('fmt') - exit(0) } mut cli_args_no_files := []string{} for idx, a in os.args { diff --git a/cmd/tools/vpm/vpm.v b/cmd/tools/vpm/vpm.v index 26b1c08f7aeb05..f1731881f56517 100644 --- a/cmd/tools/vpm/vpm.v +++ b/cmd/tools/vpm/vpm.v @@ -62,8 +62,7 @@ fn main() { options := cmdline.only_options(os.args[1..]) verbose_println('cli params: ${params}') if params.len < 1 { - vpm_help() - exit(5) + help.print_and_exit('vpm', exit_code: 5) } vpm_command := params[0] mut module_names := params[1..].clone() @@ -71,7 +70,7 @@ fn main() { // println('module names: ') println(module_names) match vpm_command { 'help' { - vpm_help() + help.print_and_exit('vpm') } 'search' { vpm_search(module_names) @@ -141,7 +140,6 @@ fn vpm_search(keywords []string) { search_keys := keywords.map(it.replace('_', '-')) if settings.is_help { help.print_and_exit('search') - exit(0) } if search_keys.len == 0 { eprintln('´v search´ requires *at least one* keyword.') @@ -365,7 +363,6 @@ fn vpm_once_filter(module_names []string) []string { fn vpm_install(module_names []string, source Source) { if settings.is_help { help.print_and_exit('install') - exit(0) } if module_names.len == 0 { eprintln('´v install´ requires *at least one* module name.') @@ -427,7 +424,6 @@ fn vpm_update(m []string) { mut module_names := m.clone() if settings.is_help { help.print_and_exit('update') - exit(0) } if module_names.len == 0 { module_names = get_installed_modules() @@ -574,7 +570,6 @@ fn vpm_list() { fn vpm_remove(module_names []string) { if settings.is_help { help.print_and_exit('remove') - exit(0) } if module_names.len == 0 { eprintln('´v remove´ requires *at least one* module name.') @@ -627,10 +622,6 @@ fn ensure_vmodules_dir_exist() { } } -fn vpm_help() { - help.print_and_exit('vpm') -} - fn vcs_used_in_dir(dir string) ?[]string { mut vcs := []string{} for repo_subfolder in supported_vcs_folders { diff --git a/vlib/v/help/help.v b/vlib/v/help/help.v index 6da2e6bb3b0b9b..551ee79f5529a4 100644 --- a/vlib/v/help/help.v +++ b/vlib/v/help/help.v @@ -6,24 +6,23 @@ const help_dir = os.join_path(@VEXEROOT, 'vlib', 'v', 'help') [params] pub struct ExitOptions { - success_code int // The exit code to use after the specified topic was printed successfully. - fail_code int = 1 // The exit code to use after the specified topic could not be printed (e.g., if it is unknown). + exit_code int } // print_and_exit prints the help topic and exits. -pub fn print_and_exit(topic string, exit_opts ExitOptions) { +[noreturn] +pub fn print_and_exit(topic string, opts ExitOptions) { if topic == 'topics' { print_known_topics() - exit(exit_opts.success_code) + exit(opts.exit_code) } - + fail_code := if opts.exit_code != 0 { opts.exit_code } else { 1 } for c in topic { if !c.is_letter() && !c.is_digit() && c != `-` { print_topic_unkown(topic) - exit(exit_opts.fail_code) + exit(fail_code) } } - mut topic_path := '' for path in os.walk_ext(help.help_dir, '.txt') { if topic == os.file_name(path).all_before('.txt') { @@ -34,14 +33,13 @@ pub fn print_and_exit(topic string, exit_opts ExitOptions) { if topic_path == '' { print_topic_unkown(topic) print_known_topics() - exit(exit_opts.fail_code) + exit(fail_code) } - println(os.read_file(topic_path) or { eprintln('error: failed reading topic file: ${err}') - exit(exit_opts.fail_code) + exit(fail_code) }) - exit(exit_opts.success_code) + exit(opts.exit_code) } fn print_topic_unkown(topic string) {