Skip to content

Commit c8bf38c

Browse files
authored
v.pref: error for v file.v --unknown-option (#21391)
1 parent 14d8a97 commit c8bf38c

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

vlib/v/pref/pref.v

+13-1
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,10 @@ pub fn parse_args_and_show_errors(known_external_commands []string, args []strin
924924
if command == 'build' && is_source_file(arg) {
925925
eprintln_exit('Use `v ${arg}` instead.')
926926
}
927+
if is_source_file(arg) && arg.ends_with('.vsh') {
928+
// store for future iterations
929+
res.is_vsh = true
930+
}
927931
if !arg.starts_with('-') {
928932
if command == '' {
929933
command, command_idx = arg, i
@@ -936,10 +940,18 @@ pub fn parse_args_and_show_errors(known_external_commands []string, args []strin
936940
}
937941
continue
938942
}
939-
if command !in ['', 'build-module'] {
943+
if command !in ['', 'build-module'] && !is_source_file(command) {
940944
// arguments for e.g. fmt should be checked elsewhere
941945
continue
942946
}
947+
if res.is_vsh && command_idx < i {
948+
// Allow for `script.vsh abc 123 -option`, because -option is for the .vsh program, not for v
949+
continue
950+
}
951+
if command == 'doc' {
952+
// Allow for `v doc -comments file.v`
953+
continue
954+
}
943955
err_detail := if command == '' { '' } else { ' for command `${command}`' }
944956
eprintln_exit('Unknown argument `${arg}`${err_detail}')
945957
}

vlib/v/pref/unknown_options_test.v

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import os
2+
3+
const tfile = os.join_path(os.vtmp_dir(), 'unknown_options_output.c')
4+
5+
fn test_unknown_option_flags_no_run() {
6+
os.chdir(os.dir(@VEXE))!
7+
os.rm(tfile) or {}
8+
9+
res1 := os.execute('${os.quoted_path(@VEXE)} -o ${os.quoted_path(tfile)} examples/hello_world.v --an-unknown-option')
10+
assert res1.exit_code == 1
11+
assert res1.output.starts_with('Unknown argument')
12+
assert res1.output.contains('--an-unknown-option')
13+
assert !os.exists(tfile)
14+
15+
res2 := os.execute('${os.quoted_path(@VEXE)} -o ${os.quoted_path(tfile)} --an-unknown-option examples/hello_world.v')
16+
assert res2.exit_code == 1
17+
assert res2.output.starts_with('Unknown argument')
18+
assert res2.output.contains('--an-unknown-option')
19+
assert !os.exists(tfile)
20+
}
21+
22+
fn test_unknown_option_flags_with_run() {
23+
res_run_o := os.execute('${os.quoted_path(@VEXE)} -o ${os.quoted_path(tfile)} run examples/hello_world.v --an-unknown-option')
24+
assert res_run_o.exit_code == 0
25+
assert res_run_o.output == '' // because of -o, there should not be an actual run, since compilation stopped after generating the .c file
26+
assert os.exists(tfile)
27+
os.rm(tfile) or {}
28+
29+
res_run_no_o_unknown_before_run := os.execute('${os.quoted_path(@VEXE)} --an-unknown-option run examples/hello_world.v ')
30+
assert res_run_no_o_unknown_before_run.exit_code == 1
31+
assert res_run_no_o_unknown_before_run.output.starts_with('Unknown argument')
32+
assert res_run_no_o_unknown_before_run.output.contains('--an-unknown-option')
33+
assert !os.exists(tfile)
34+
35+
res_run_no_o := os.execute('${os.quoted_path(@VEXE)} run examples/hello_world.v --an-unknown-option')
36+
assert res_run_no_o.exit_code == 0
37+
assert res_run_no_o.output.trim_space() == 'Hello, World!'
38+
assert !os.exists(tfile)
39+
}
File renamed without changes.

0 commit comments

Comments
 (0)