2020-03-13 22:52:49 +03:00
|
|
|
module help
|
|
|
|
|
2020-04-26 08:35:59 +03:00
|
|
|
// TODO: move this file outside internal, and merge it with cmd/tools/modules/vhelp/vhelp.v .
|
|
|
|
import os
|
|
|
|
import v.pref
|
2020-03-13 22:52:49 +03:00
|
|
|
|
|
|
|
const (
|
2021-03-02 13:22:10 +03:00
|
|
|
unknown_topic = '`v help`: unknown help topic provided. Use `v help` for usage information.'
|
2020-03-13 22:52:49 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
pub fn print_and_exit(topic string) {
|
|
|
|
vexe := pref.vexe_path()
|
|
|
|
vroot := os.dir(vexe)
|
2021-07-26 09:41:04 +03:00
|
|
|
topicdir := os.join_path(vroot, 'cmd', 'v', 'help')
|
|
|
|
|
2020-03-13 22:52:49 +03:00
|
|
|
for b in topic {
|
|
|
|
if (b >= `a` && b <= `z`) || b == `-` || (b >= `0` && b <= `9`) {
|
|
|
|
continue
|
|
|
|
}
|
2021-03-02 13:22:10 +03:00
|
|
|
eprintln(help.unknown_topic)
|
2020-03-13 22:52:49 +03:00
|
|
|
exit(1)
|
|
|
|
}
|
2021-03-13 09:37:12 +03:00
|
|
|
// `init` has the same help topic as `new`
|
2021-03-25 00:37:10 +03:00
|
|
|
name := if topic == 'init' { 'new' } else { topic }
|
2021-07-26 09:41:04 +03:00
|
|
|
if topic == 'topics' {
|
|
|
|
println(known_topics(topicdir))
|
|
|
|
exit(0)
|
|
|
|
}
|
|
|
|
target_topic := os.join_path(topicdir, '${name}.txt')
|
2020-03-13 22:52:49 +03:00
|
|
|
content := os.read_file(target_topic) or {
|
2021-03-02 13:22:10 +03:00
|
|
|
eprintln(help.unknown_topic)
|
2021-07-26 09:41:04 +03:00
|
|
|
eprintln(known_topics(topicdir))
|
2020-03-13 22:52:49 +03:00
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
println(content)
|
|
|
|
exit(0)
|
|
|
|
}
|
2021-07-26 09:41:04 +03:00
|
|
|
|
|
|
|
fn known_topics(topicdir string) string {
|
|
|
|
mut res := []string{}
|
|
|
|
res << 'Known help topics:'
|
|
|
|
topic_files := os.glob(os.join_path(topicdir, '*.txt')) or { [] }
|
|
|
|
mut topics := topic_files.map(os.file_name(it).replace('.txt', ''))
|
|
|
|
topics.sort()
|
|
|
|
res << topics.join(', ')
|
|
|
|
return res.join('')
|
|
|
|
}
|