From 31321b68eaab5f27cc628028ca2e0ba67ff63f91 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Tue, 2 Mar 2021 12:22:10 +0200 Subject: [PATCH] v: show help for -h, -help, and --help, in addition to `v help` --- cmd/v/help/help.v | 6 +++--- cmd/v/help/help_test.v | 22 ++++++++++++++++++++++ cmd/v/v.v | 5 ++++- vlib/v/pref/pref.v | 5 +++++ 4 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 cmd/v/help/help_test.v diff --git a/cmd/v/help/help.v b/cmd/v/help/help.v index df6c4e0033..0a69b7f181 100644 --- a/cmd/v/help/help.v +++ b/cmd/v/help/help.v @@ -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) diff --git a/cmd/v/help/help_test.v b/cmd/v/help/help_test.v new file mode 100644 index 0000000000..78cf6d6e4b --- /dev/null +++ b/cmd/v/help/help_test.v @@ -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.') +} diff --git a/cmd/v/v.v b/cmd/v/v.v index d5f9459b4b..5387a8aaea 100644 --- a/cmd/v/v.v +++ b/cmd/v/v.v @@ -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) } diff --git a/vlib/v/pref/pref.v b/vlib/v/pref/pref.v index 86513a5e54..ec5d628768 100644 --- a/vlib/v/pref/pref.v +++ b/vlib/v/pref/pref.v @@ -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 {