From 517260a1eb2c85adc92113a1198a4f0154b87716 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Mon, 26 Jul 2021 09:41:04 +0300 Subject: [PATCH] v help: support `v help topics` to list all help .txt file names --- cmd/v/help/default.txt | 1 + cmd/v/help/help.v | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/cmd/v/help/default.txt b/cmd/v/help/default.txt index dfa5c12ecf..77d3b57571 100644 --- a/cmd/v/help/default.txt +++ b/cmd/v/help/default.txt @@ -54,6 +54,7 @@ V supports the following commands: Use "v help " for more information about a command, example: `v help build`, `v help build-c`, `v help build-native` Use "v help other" to see less frequently used commands. +Use "v help topics" to see a list of all known help topics. Note: Help is required to write more help topics. Only build, new, init, doc, fmt, vet, run, test, watch, search, install, remove, update, bin2v, check-md are properly documented currently. diff --git a/cmd/v/help/help.v b/cmd/v/help/help.v index 80b17bc5da..d54bf064e7 100644 --- a/cmd/v/help/help.v +++ b/cmd/v/help/help.v @@ -11,6 +11,8 @@ const ( pub fn print_and_exit(topic string) { vexe := pref.vexe_path() vroot := os.dir(vexe) + topicdir := os.join_path(vroot, 'cmd', 'v', 'help') + for b in topic { if (b >= `a` && b <= `z`) || b == `-` || (b >= `0` && b <= `9`) { continue @@ -20,11 +22,26 @@ pub fn print_and_exit(topic string) { } // `init` has the same help topic as `new` name := if topic == 'init' { 'new' } else { topic } - target_topic := os.join_path(vroot, 'cmd', 'v', 'help', '${name}.txt') + if topic == 'topics' { + println(known_topics(topicdir)) + exit(0) + } + target_topic := os.join_path(topicdir, '${name}.txt') content := os.read_file(target_topic) or { eprintln(help.unknown_topic) + eprintln(known_topics(topicdir)) exit(1) } println(content) exit(0) } + +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('') +}