1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

v: show help for -h, -help, and --help, in addition to v help

This commit is contained in:
Delyan Angelov
2021-03-02 12:22:10 +02:00
parent 81dbd72412
commit 31321b68ea
4 changed files with 34 additions and 4 deletions

View File

@@ -5,7 +5,7 @@ import os
import v.pref
const (
unknown_topic = 'V Error: Unknown help topic provided. Use `v help` for usage information.'
unknown_topic = '`v help`: unknown help topic provided. Use `v help` for usage information.'
)
pub fn print_and_exit(topic string) {
@@ -15,12 +15,12 @@ pub fn print_and_exit(topic string) {
if (b >= `a` && b <= `z`) || b == `-` || (b >= `0` && b <= `9`) {
continue
}
eprintln(unknown_topic)
eprintln(help.unknown_topic)
exit(1)
}
target_topic := os.join_path(vroot, 'cmd', 'v', 'help', '${topic}.txt')
content := os.read_file(target_topic) or {
eprintln(unknown_topic)
eprintln(help.unknown_topic)
exit(1)
}
println(content)

22
cmd/v/help/help_test.v Normal file
View File

@@ -0,0 +1,22 @@
import os
fn test_help() {
vexe := os.getenv('VEXE')
res := os.exec('"$vexe" help') or { panic(err) }
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() {
vexe := os.getenv('VEXE')
res := os.exec('"$vexe" -h') or { panic(err) }
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() {
vexe := os.getenv('VEXE')
res := os.exec('"$vexe" --help') or { panic(err) }
assert res.exit_code == 0
assert res.output.starts_with('V is a tool for managing V source code.')
}

View File

@@ -71,6 +71,9 @@ fn main() {
}
args_and_flags := util.join_env_vflags_and_os_args()[1..]
prefs, command := pref.parse_args(external_tools, args_and_flags)
if prefs.is_help {
invoke_help_and_exit(args)
}
if prefs.is_verbose {
// println('args= ')
// println(args) // QTODO
@@ -145,7 +148,7 @@ fn invoke_help_and_exit(remaining []string) {
2 { help.print_and_exit(remaining[1]) }
else {}
}
println('V Error: Expected only one help topic to be provided.')
println('`v help`: provide only one help topic.')
println('For usage information, use `v help`.')
exit(1)
}