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

pull/9057/head
Delyan Angelov 2021-03-02 12:22:10 +02:00
parent 81dbd72412
commit 31321b68ea
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
4 changed files with 34 additions and 4 deletions

View File

@ -5,7 +5,7 @@ import os
import v.pref import v.pref
const ( 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) { 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`) { if (b >= `a` && b <= `z`) || b == `-` || (b >= `0` && b <= `9`) {
continue continue
} }
eprintln(unknown_topic) eprintln(help.unknown_topic)
exit(1) exit(1)
} }
target_topic := os.join_path(vroot, 'cmd', 'v', 'help', '${topic}.txt') target_topic := os.join_path(vroot, 'cmd', 'v', 'help', '${topic}.txt')
content := os.read_file(target_topic) or { content := os.read_file(target_topic) or {
eprintln(unknown_topic) eprintln(help.unknown_topic)
exit(1) exit(1)
} }
println(content) println(content)

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..] args_and_flags := util.join_env_vflags_and_os_args()[1..]
prefs, command := pref.parse_args(external_tools, args_and_flags) prefs, command := pref.parse_args(external_tools, args_and_flags)
if prefs.is_help {
invoke_help_and_exit(args)
}
if prefs.is_verbose { if prefs.is_verbose {
// println('args= ') // println('args= ')
// println(args) // QTODO // println(args) // QTODO
@ -145,7 +148,7 @@ fn invoke_help_and_exit(remaining []string) {
2 { help.print_and_exit(remaining[1]) } 2 { help.print_and_exit(remaining[1]) }
else {} 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`.') println('For usage information, use `v help`.')
exit(1) exit(1)
} }

View File

@ -143,6 +143,7 @@ pub mut:
cleanup_files []string // list of temporary *.tmp.c and *.tmp.c.rsp files. Cleaned up on successfull builds. cleanup_files []string // list of temporary *.tmp.c and *.tmp.c.rsp files. Cleaned up on successfull builds.
build_options []string // list of options, that should be passed down to `build-module`, if needed for -usecache build_options []string // list of options, that should be passed down to `build-module`, if needed for -usecache
cache_manager vcache.CacheManager cache_manager vcache.CacheManager
is_help bool // -h, -help or --help was passed
} }
pub fn parse_args(known_external_commands []string, args []string) (&Preferences, string) { pub fn parse_args(known_external_commands []string, args []string) (&Preferences, string) {
@ -167,6 +168,10 @@ pub fn parse_args(known_external_commands []string, args []string) (&Preferences
'-check-syntax' { '-check-syntax' {
res.only_check_syntax = true res.only_check_syntax = true
} }
'-h', '-help', '--help' {
// NB: help is *very important*, just respond to all variations:
res.is_help = true
}
'-v' { '-v' {
// `-v` flag is for setting verbosity, but without any args it prints the version, like Clang // `-v` flag is for setting verbosity, but without any args it prints the version, like Clang
if args.len > 1 { if args.len > 1 {