From 7cbc37a61c909b4a5d9ad5e5fb13193dfc19e145 Mon Sep 17 00:00:00 2001 From: Turiiya Date: Mon, 29 Apr 2024 15:46:16 +0200 Subject: [PATCH 1/4] pref: fix version flag handling --- vlib/v/pref/pref.c.v | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/vlib/v/pref/pref.c.v b/vlib/v/pref/pref.c.v index e1298279bd7187..f1f03eefe0401a 100644 --- a/vlib/v/pref/pref.c.v +++ b/vlib/v/pref/pref.c.v @@ -401,7 +401,7 @@ pub fn parse_args_and_show_errors(known_external_commands []string, args []strin } res.is_quiet = true } - '-v' { + '-v', '-V', '--version', '-version' { if command_pos != -1 { // a -v flag after the command, is intended for the command, not for V itself continue @@ -960,11 +960,6 @@ pub fn parse_args_and_show_errors(known_external_commands []string, args []strin } continue } - if arg in ['-V', '-version', '--version'] { - command = 'version' - command_pos = i - continue - } if command != '' && command != 'build-module' { // arguments for e.g. fmt should be checked elsewhere continue From 632be2c3349aaf2b17dcef50117e0fcfceb7e9e0 Mon Sep 17 00:00:00 2001 From: Turiiya Date: Mon, 29 Apr 2024 16:13:38 +0200 Subject: [PATCH 2/4] fix when used with vflags like `v -cc gcc --version` --- vlib/v/pref/pref.c.v | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/vlib/v/pref/pref.c.v b/vlib/v/pref/pref.c.v index f1f03eefe0401a..258c7a6b1acb85 100644 --- a/vlib/v/pref/pref.c.v +++ b/vlib/v/pref/pref.c.v @@ -402,16 +402,15 @@ pub fn parse_args_and_show_errors(known_external_commands []string, args []strin res.is_quiet = true } '-v', '-V', '--version', '-version' { - if command_pos != -1 { - // a -v flag after the command, is intended for the command, not for V itself - continue - } - // `-v` flag is for setting verbosity, but without any args it prints the version, like Clang - if args.len > 1 { - res.is_verbose = true - } else { - command = 'version' - command_pos = i + if command_pos == -1 { + // Version flags after a command are intended for the command, not for V itself. + if args.len > 1 && arg == '-v' { + // With additional args after the `-v` flag, it toggles verbosity, like Clang. + // E.g.: `v -v` VS `v -v run examples/hello_world.v`. + res.is_verbose = true + } else { + command = 'version' + } } } '-progress' { From 2107dd34597f22e597ad57d72f0a1deac5238753 Mon Sep 17 00:00:00 2001 From: Turiiya Date: Mon, 29 Apr 2024 16:45:56 +0200 Subject: [PATCH 3/4] further improve fix to handle `-v` version/verbosity toggle with vflags like `-cc gcc -v` --- vlib/v/pref/pref.c.v | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vlib/v/pref/pref.c.v b/vlib/v/pref/pref.c.v index 258c7a6b1acb85..2a60619f6551b8 100644 --- a/vlib/v/pref/pref.c.v +++ b/vlib/v/pref/pref.c.v @@ -404,7 +404,7 @@ pub fn parse_args_and_show_errors(known_external_commands []string, args []strin '-v', '-V', '--version', '-version' { if command_pos == -1 { // Version flags after a command are intended for the command, not for V itself. - if args.len > 1 && arg == '-v' { + if current_args.len > 1 && arg == '-v' { // With additional args after the `-v` flag, it toggles verbosity, like Clang. // E.g.: `v -v` VS `v -v run examples/hello_world.v`. res.is_verbose = true From f4da164dc5a77edca64d4ef82eb5db955bd804bf Mon Sep 17 00:00:00 2001 From: Turiiya Date: Mon, 29 Apr 2024 17:09:15 +0200 Subject: [PATCH 4/4] add tests, rebase for cleaner PR commit overview --- vlib/v/pref/pref_test.v | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/vlib/v/pref/pref_test.v b/vlib/v/pref/pref_test.v index 5ce05dfc4f0e75..0d97ccee55d347 100644 --- a/vlib/v/pref/pref_test.v +++ b/vlib/v/pref/pref_test.v @@ -1,8 +1,40 @@ module pref +import v.vmod +import os + fn test_check_parametes() { // reproducing issue https://github.com/vlang/v/issues/13983 _, cmd := parse_args_and_show_errors(['help'], [''], true) // no command found from args assert cmd == '' } + +fn test_version_falg() { + // Vars instead of consts to prevent dupl decls in pref. + vexe := @VEXE + vroot := os.dir(vexe) + + v_ver := vmod.from_file(os.join_path(vroot, 'v.mod'))!.version + v_ver_cmd_res := os.execute_opt('${vexe} --version')!.output + assert v_ver_cmd_res.starts_with('V ${v_ver}'), v_ver_cmd_res + + v_retry_ver_cmd_res := os.execute_opt('${vexe} retry --version')!.output + assert v_retry_ver_cmd_res != v_ver_cmd_res + + v_git_ver_subcmd_res := os.execute_opt('${vexe} retry -- git --version')!.output + assert v_git_ver_subcmd_res !in [v_ver_cmd_res, v_retry_ver_cmd_res] + + // Test version / verbosity toggle. + assert os.execute_opt('${vexe} -v')!.output == v_ver_cmd_res + assert os.execute_opt('${vexe} -cc tcc -v')!.output == v_ver_cmd_res + + example_path := os.join_path(vroot, 'examples', 'hello_world.v') + v_verbose_cmd_res := os.execute_opt('${vexe} -v run ${example_path}')!.output + assert v_verbose_cmd_res != v_ver_cmd_res + assert v_verbose_cmd_res.contains('v.pref.lookup_path:') + + v_verbose_cmd_with_additional_args_res := os.execute_opt('${vexe} -cc tcc -v run ${example_path}')!.output + assert v_verbose_cmd_with_additional_args_res != v_ver_cmd_res + assert v_verbose_cmd_with_additional_args_res.contains('v.pref.lookup_path:') +}