Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v.pref: error for v file.v --unknown-option #21391

Merged
merged 8 commits into from
May 18, 2024
14 changes: 13 additions & 1 deletion vlib/v/pref/pref.v
Original file line number Diff line number Diff line change
Expand Up @@ -924,6 +924,10 @@ pub fn parse_args_and_show_errors(known_external_commands []string, args []strin
if command == 'build' && is_source_file(arg) {
eprintln_exit('Use `v ${arg}` instead.')
}
if is_source_file(arg) && arg.ends_with('.vsh') {
// store for future iterations
res.is_vsh = true
}
if !arg.starts_with('-') {
if command == '' {
command, command_idx = arg, i
Expand All @@ -936,10 +940,18 @@ pub fn parse_args_and_show_errors(known_external_commands []string, args []strin
}
continue
}
if command !in ['', 'build-module'] {
if command !in ['', 'build-module'] && !is_source_file(command) {
// arguments for e.g. fmt should be checked elsewhere
continue
}
if res.is_vsh && command_idx < i {
// Allow for `script.vsh abc 123 -option`, because -option is for the .vsh program, not for v
continue
}
if command == 'doc' {
// Allow for `v doc -comments file.v`
continue
}
err_detail := if command == '' { '' } else { ' for command `${command}`' }
eprintln_exit('Unknown argument `${arg}`${err_detail}')
}
Expand Down
39 changes: 39 additions & 0 deletions vlib/v/pref/unknown_options_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import os

const tfile = os.join_path(os.vtmp_dir(), 'unknown_options_output.c')

fn test_unknown_option_flags_no_run() {
os.chdir(os.dir(@VEXE))!
os.rm(tfile) or {}

res1 := os.execute('${os.quoted_path(@VEXE)} -o ${os.quoted_path(tfile)} examples/hello_world.v --an-unknown-option')
assert res1.exit_code == 1
assert res1.output.starts_with('Unknown argument')
assert res1.output.contains('--an-unknown-option')
assert !os.exists(tfile)

res2 := os.execute('${os.quoted_path(@VEXE)} -o ${os.quoted_path(tfile)} --an-unknown-option examples/hello_world.v')
assert res2.exit_code == 1
assert res2.output.starts_with('Unknown argument')
assert res2.output.contains('--an-unknown-option')
assert !os.exists(tfile)
}

fn test_unknown_option_flags_with_run() {
res_run_o := os.execute('${os.quoted_path(@VEXE)} -o ${os.quoted_path(tfile)} run examples/hello_world.v --an-unknown-option')
assert res_run_o.exit_code == 0
assert res_run_o.output == '' // because of -o, there should not be an actual run, since compilation stopped after generating the .c file
assert os.exists(tfile)
os.rm(tfile) or {}

res_run_no_o_unknown_before_run := os.execute('${os.quoted_path(@VEXE)} --an-unknown-option run examples/hello_world.v ')
assert res_run_no_o_unknown_before_run.exit_code == 1
assert res_run_no_o_unknown_before_run.output.starts_with('Unknown argument')
assert res_run_no_o_unknown_before_run.output.contains('--an-unknown-option')
assert !os.exists(tfile)

res_run_no_o := os.execute('${os.quoted_path(@VEXE)} run examples/hello_world.v --an-unknown-option')
assert res_run_no_o.exit_code == 0
assert res_run_no_o.output.trim_space() == 'Hello, World!'
assert !os.exists(tfile)
}
File renamed without changes.
Loading