Skip to content

Commit

Permalink
v.help: add ExitOptions params struct to print_and_exit (#19698)
Browse files Browse the repository at this point in the history
  • Loading branch information
ttytm committed Oct 30, 2023
1 parent aa28efc commit c6d3a23
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions vlib/v/help/help.v
Expand Up @@ -4,17 +4,23 @@ import os

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).
}

// print_and_exit prints the help topic and exits.
pub fn print_and_exit(topic string) {
pub fn print_and_exit(topic string, exit_opts ExitOptions) {
if topic == 'topics' {
print_known_topics()
exit(0)
exit(exit_opts.success_code)
}

for c in topic {
if !c.is_letter() && !c.is_digit() && c != `-` {
print_topic_unkown(topic)
exit(1)
exit(exit_opts.fail_code)
}
}

Expand All @@ -28,14 +34,14 @@ pub fn print_and_exit(topic string) {
if topic_path == '' {
print_topic_unkown(topic)
print_known_topics()
exit(1)
exit(exit_opts.fail_code)
}

println(os.read_file(topic_path) or {
eprintln('error: failed reading topic file: ${err}')
exit(1)
exit(exit_opts.fail_code)
})
exit(0)
exit(exit_opts.success_code)
}

fn print_topic_unkown(topic string) {
Expand Down

0 comments on commit c6d3a23

Please sign in to comment.