From 05dcdfd267c2335b0cc455c082e63cc3ce4fb87b Mon Sep 17 00:00:00 2001 From: Nick Treleaven Date: Tue, 29 Sep 2020 02:13:54 +0100 Subject: [PATCH] pref: error if unknown argument passed to `v` (#6487) --- cmd/v/v.v | 10 ---------- vlib/v/pref/pref.v | 36 ++++++++++++++++++++++++++---------- 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/cmd/v/v.v b/cmd/v/v.v index c22cf5d83a..c4d9915de0 100644 --- a/cmd/v/v.v +++ b/cmd/v/v.v @@ -35,16 +35,6 @@ fn main() { } args_and_flags := util.join_env_vflags_and_os_args()[1..] prefs, command := pref.parse_args(args_and_flags) - // if prefs.is_verbose { - // println('command = "$command"') - // println(util.full_v_version(prefs.is_verbose)) - // } - if args.len > 0 && (args[0] in ['version', '-V', '-version', '--version'] || (args[0] == - '-v' && args.len == 1)) { - // `-v` flag is for setting verbosity, but without any args it prints the version, like Clang - println(util.full_v_version(prefs.is_verbose)) - return - } if prefs.is_verbose { // println('args= ') // println(args) // QTODO diff --git a/vlib/v/pref/pref.v b/vlib/v/pref/pref.v index 4e4cd2fc09..e683edafc5 100644 --- a/vlib/v/pref/pref.v +++ b/vlib/v/pref/pref.v @@ -147,7 +147,13 @@ pub fn parse_args(args []string) (&Preferences, string) { res.only_check_syntax = true } '-v' { - res.is_verbose = true + // `-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 + } } '-silent' { res.output_mode = .silent @@ -313,21 +319,31 @@ pub fn parse_args(args []string) (&Preferences, string) { i++ } else { - mut should_continue := false - for flag_with_param in list_of_flags_with_param { - if '-$flag_with_param' == arg { - should_continue = true + if arg[0] == `-` { + if arg[1..] in list_of_flags_with_param { + // skip parameter i++ - break + continue + } + } else { + if command == '' { + command = arg + command_pos = i } - } - if should_continue { continue } - if !arg.starts_with('-') && command == '' { - command = arg + if arg in ['-V', '-version', '--version'] { + command = 'version' command_pos = i + continue } + if command !in ['', 'run', 'build', 'build-module'] { + // arguments for e.g. fmt are checked elsewhere + continue + } + eprint('Unknown argument `$arg`') + eprintln(if command.len == 0 {''} else {' for command `$command`'}) + exit(1) } } }