Skip to content

Commit

Permalink
v.help: add [noreturn] attribute to print_and_exit (#19706)
Browse files Browse the repository at this point in the history
  • Loading branch information
ttytm authored Oct 30, 2023
1 parent 880ce7a commit 1c4c505
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 24 deletions.
1 change: 0 additions & 1 deletion cmd/tools/vcheck-md.v
Original file line number Diff line number Diff line change
Expand Up @@ -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.')
Expand Down
1 change: 0 additions & 1 deletion cmd/tools/vfmt.v
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 2 additions & 11 deletions cmd/tools/vpm/vpm.v
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,15 @@ 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()
ensure_vmodules_dir_exist()
// println('module names: ') println(module_names)
match vpm_command {
'help' {
vpm_help()
help.print_and_exit('vpm')
}
'search' {
vpm_search(module_names)
Expand Down Expand Up @@ -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.')
Expand Down Expand Up @@ -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.')
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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.')
Expand Down Expand Up @@ -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 {
Expand Down
20 changes: 9 additions & 11 deletions vlib/v/help/help.v
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand All @@ -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) {
Expand Down

0 comments on commit 1c4c505

Please sign in to comment.