Skip to content

Commit

Permalink
v.help: fix print_help_and_exit, for sub topics like v fmt --help
Browse files Browse the repository at this point in the history
…, rewrite help.v, and add more tests (vlang#19672)
  • Loading branch information
ttytm authored Oct 28, 2023
1 parent 491558a commit 573188a
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 88 deletions.
92 changes: 33 additions & 59 deletions vlib/v/help/help.v
Original file line number Diff line number Diff line change
Expand Up @@ -2,80 +2,54 @@ module help

import os

const (
unknown_topic = '`v help`: unknown help topic provided. Use `v help` for usage information.'
)
const help_dir = os.join_path(@VEXEROOT, 'vlib', 'v', 'help')

// print_and_exit Prints the help topic and exits
// print_and_exit prints the help topic and exits.
pub fn print_and_exit(topic string) {
vexe := os.executable()
vroot := os.dir(vexe)
topicdir := os.join_path(vroot, 'vlib', 'v', 'help')

for b in topic {
if (b >= `a` && b <= `z`) || b == `-` || (b >= `0` && b <= `9`) {
continue
}
eprintln(help.unknown_topic)
exit(1)
if topic == 'topics' {
print_known_topics()
exit(0)
}

mut path_to := topic
mut topics := os.walk_ext(topicdir, '.txt')
mut items := [][]string{}

mut item_rev := []string{}
mut delim := ''

// Getting the directory, splitting at `/`, trimming to only indexes 0 and 1,
// and reversing that into the items array
for mut item in topics {
$if windows {
delim = '\\'
} $else {
delim = '/'
for c in topic {
if !c.is_letter() && !c.is_digit() && c != `-` {
print_topic_unkown(topic)
exit(1)
}
item_rev = item.split(delim).reverse()
item_rev.trim(2)
items << item_rev.reverse()
}

// Getting the path to the help topic text file
for cmds in items {
if '${topic}.txt' in cmds {
path_to = '${cmds[0]}/${cmds[1].replace('.txt', '')}'
mut topic_path := ''
for path in os.walk_ext(help.help_dir, '.txt') {
if topic == os.file_name(path).all_before('.txt') {
topic_path = path
break
}
}

topic_dir := if topic == 'default' {
os.join_path(topicdir, 'default.txt')
} else {
os.join_path(topicdir, '${path_to}.txt')
}

if topic == 'topics' {
println(known_topics(topicdir))
exit(0)
if topic_path == '' {
print_topic_unkown(topic)
print_known_topics()
exit(1)
}

content := os.read_file(topic_dir) or {
eprintln(help.unknown_topic)
eprintln(known_topics(topicdir))
println(os.read_file(topic_path) or {
eprintln('error: failed reading topic file: ${err}')
exit(1)
}
println(content)
})
exit(0)
}

// known_topics Getting topics known to V
fn known_topics(topicdir string) string {
mut res := []string{}
res << 'Known help topics: '
fn print_topic_unkown(topic string) {
eprintln('error: unknown help topic "${topic}". Use `v help` for usage information.')
}

mut topics := os.walk_ext(topicdir, '.txt').map(os.file_name(it).replace('.txt', ''))
topics.sort()
res << topics.join(', ')
res << '.'
return res.join('').replace('default, ', '')
fn print_known_topics() {
mut res := 'Known help topics: '
topic_paths := os.walk_ext(help.help_dir, '.txt')
for i, path in topic_paths {
topic := os.file_name(path).all_before('.txt')
if topic != 'default' {
res += topic + if i != topic_paths.len - 1 { ', ' } else { '.' }
}
}
println(res)
}
57 changes: 28 additions & 29 deletions vlib/v/help/help_test.v
Original file line number Diff line number Diff line change
@@ -1,52 +1,51 @@
import os

const vexe = os.getenv('VEXE')
const vexe = os.quoted_path(@VEXE)

fn test_help() {
res := os.execute('${os.quoted_path(vexe)} help')
res := os.execute(vexe + ' help')
assert res.exit_code == 0
assert res.output.starts_with('V is a tool for managing V source code.')
}

fn test_help_as_short_option() {
res := os.execute('${os.quoted_path(vexe)} -h')
res := os.execute(vexe + ' -h')
assert res.exit_code == 0
assert res.output.starts_with('V is a tool for managing V source code.')
}

fn test_help_as_long_option() {
res := os.execute('${os.quoted_path(vexe)} --help')
res := os.execute(vexe + ' --help')
assert res.exit_code == 0
assert res.output.starts_with('V is a tool for managing V source code.')
}

fn test_all_help() {
vroot := os.dir(vexe)
topicdir := os.join_path(vroot, 'vlib', 'v', 'help')
mut topics := os.walk_ext(topicdir, '.txt')

mut items := []string{}
mut delim := ''

for mut item in topics {
$if windows {
delim = '\\'
} $else {
delim = '/'
}
mut item_rev := item.replace('.txt', '').split(delim).reverse()
item_rev.trim(2)
items << item_rev.reverse()
fn test_all_topics() {
help_dir := os.join_path(@VEXEROOT, 'vlib', 'v', 'help')
topic_paths := os.walk_ext(help_dir, '.txt')
topics := topic_paths.map(os.file_name(it).replace('.txt', ''))
for t in topics {
res := os.execute(vexe + ' help ${t}')
assert res.exit_code == 0, res.output
assert res.output != ''
}
}

for topic in items {
res := os.execute('${os.quoted_path(vexe)} help ${topic}')
fn test_uknown_topic() {
res := os.execute(vexe + ' help abc')
assert res.exit_code == 1, res.output
assert res.output.starts_with('error: unknown help topic "abc".')
}

if topic == 'help' {
continue
}
fn test_topics_output() {
res := os.execute(vexe + ' help topics')
assert res.exit_code == 0, res.output
assert res.output != '', res.output
assert !res.output.contains('default')
}

assert res.exit_code == 0
assert res.output != ''
}
fn test_topic_sub_help() {
res := os.execute(vexe + ' fmt --help')
assert res.exit_code == 0, res.output
assert res.output != ''
}

0 comments on commit 573188a

Please sign in to comment.