v: show help for -h, -help, and --help, in addition to `v help`
parent
81dbd72412
commit
31321b68ea
|
@ -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)
|
||||
|
|
|
@ -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.')
|
||||
}
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -143,6 +143,7 @@ pub mut:
|
|||
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
|
||||
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) {
|
||||
|
@ -167,6 +168,10 @@ pub fn parse_args(known_external_commands []string, args []string) (&Preferences
|
|||
'-check-syntax' {
|
||||
res.only_check_syntax = true
|
||||
}
|
||||
'-h', '-help', '--help' {
|
||||
// NB: help is *very important*, just respond to all variations:
|
||||
res.is_help = true
|
||||
}
|
||||
'-v' {
|
||||
// `-v` flag is for setting verbosity, but without any args it prints the version, like Clang
|
||||
if args.len > 1 {
|
||||
|
|
Loading…
Reference in New Issue