Skip to content

Commit

Permalink
v.pref: support a -q option, silencing many repeated messages like …
Browse files Browse the repository at this point in the history
…the one for `v -prod run file.v`
  • Loading branch information
spytheman committed Oct 7, 2023
1 parent 1e89b0b commit 49b8c2f
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 55 deletions.
4 changes: 4 additions & 0 deletions vlib/v/builder/cc.v
Expand Up @@ -65,6 +65,10 @@ fn (mut v Builder) post_process_c_compiler_output(res os.Result) {
}
}
if os.getenv('V_NO_C_ERROR_INFO') != '' {
eprintln('> V_NO_C_ERROR_INFO is obsoleted by either setting VQUIET to 1, or by passing `-q` on the command line')
exit(1)
}
if v.pref.is_quiet {
exit(1)
}
verror('
Expand Down
8 changes: 8 additions & 0 deletions vlib/v/help/build/build.txt
Expand Up @@ -166,6 +166,14 @@ NB: the build flags are shared with the run command too:
-translated
Enable features that are discouraged in regular V code but required for translated V code.

-q
Tell V to be quieter, and to not show many otherwise helpful messages, that only get in your
way once you have read them a few dozen times. One example is the notice message, usually
shown for `v -prod run file.v`, that says that -prod should not be used with run .
Note, this option *does not affect* errors/warnings/notices, just messages that V prints in
order to be more user friendly, in common situations, that are error prone for new users, and
that just needlessly take vertical space, once you are well aware of them.

-v
Enable verbosity in the V compiler while compiling

Expand Down
99 changes: 44 additions & 55 deletions vlib/v/pref/pref.v
Expand Up @@ -131,6 +131,7 @@ pub mut:
is_ios_simulator bool
is_apk bool // build as Android .apk format
is_help bool // -h, -help or --help was passed
is_quiet bool // do not show the repetitive explanatory messages like the one for `v -prod run file.v` .
is_cstrict bool // turn on more C warnings; slightly slower
eval_argument string // `println(2+2)` on `v -e "println(2+2)"`. Note that this souce code, will be evaluated in vsh mode, so 'v -e 'println(ls(".")!)' is valid.
test_runner string // can be 'simple' (fastest, but much less detailed), 'tap', 'normal'
Expand Down Expand Up @@ -309,6 +310,9 @@ pub fn parse_args_and_show_errors(known_external_commands []string, args []strin
res.m64 = true // follow V model by default
}
res.run_only = os.getenv('VTEST_ONLY_FN').split_any(',')
if os.getenv('VQUIET') != '' {
res.is_quiet = true
}
mut command := ''
mut command_pos := -1

Expand Down Expand Up @@ -339,8 +343,7 @@ pub fn parse_args_and_show_errors(known_external_commands []string, args []strin
target_arch := cmdline.option(current_args, '-arch', '')
i++
target_arch_kind := arch_from_string(target_arch) or {
eprintln('unknown architecture target `${target_arch}`')
exit(1)
eprintln_exit('unknown architecture target `${target_arch}`')
}
res.arch = target_arch_kind
res.build_options << '${arg} ${target_arch}'
Expand Down Expand Up @@ -380,6 +383,13 @@ pub fn parse_args_and_show_errors(known_external_commands []string, args []strin
// Note: help is *very important*, just respond to all variations:
res.is_help = true
}
'-q' {
if command_pos != -1 {
// a -q flag after a command is for the command, not for v
continue
}
res.is_quiet = true
}
'-v' {
if command_pos != -1 {
// a -v flag after the command, is intended for the command, not for V itself
Expand All @@ -395,6 +405,7 @@ pub fn parse_args_and_show_errors(known_external_commands []string, args []strin
}
'-progress' {
// processed by testing tools in cmd/tools/modules/testing/common.v
continue
}
'-Wimpure-v' {
res.warn_impure_v = true
Expand Down Expand Up @@ -513,7 +524,7 @@ pub fn parse_args_and_show_errors(known_external_commands []string, args []strin
res.is_shared = true
}
'--enable-globals' {
eprintln_cond(show_output, '`--enable-globals` flag is deprecated, please use `-enable-globals` instead')
eprintln_cond(show_output && !res.is_quiet, '`--enable-globals` flag is deprecated, please use `-enable-globals` instead')
res.enable_globals = true
}
'-enable-globals' {
Expand Down Expand Up @@ -704,8 +715,7 @@ pub fn parse_args_and_show_errors(known_external_commands []string, args []strin
res.skip_warnings = true
}
'-watch' {
eprintln('The -watch option is deprecated. Please use the watch command `v watch file.v` instead.')
exit(1)
eprintln_exit('The -watch option is deprecated. Please use the watch command `v watch file.v` instead.')
}
'-print-v-files' {
res.print_v_files = true
Expand All @@ -721,8 +731,7 @@ pub fn parse_args_and_show_errors(known_external_commands []string, args []strin
res.output_cross_c = true
continue
}
eprintln('unknown operating system target `${target_os}`')
exit(1)
eprintln_exit('unknown operating system target `${target_os}`')
}
if target_os_kind == .wasm32 {
res.is_bare = true
Expand Down Expand Up @@ -757,11 +766,6 @@ pub fn parse_args_and_show_errors(known_external_commands []string, args []strin
}
i++
}
'-error-limit' {
eprintln('Note: the -error-limit option is deprecated, and will be removed in 2023/01/15. Use `-message-limit N` instead.')
res.message_limit = cmdline.option(current_args, arg, '5').int()
i++
}
'-message-limit' {
res.message_limit = cmdline.option(current_args, arg, '5').int()
i++
Expand Down Expand Up @@ -804,9 +808,7 @@ pub fn parse_args_and_show_errors(known_external_commands []string, args []strin
sbackend := cmdline.option(current_args, arg, 'c')
res.build_options << '${arg} ${sbackend}'
b := backend_from_string(sbackend) or {
eprintln('Unknown V backend: ${sbackend}')
eprintln('Valid -backend choices are: c, go, interpret, js, js_node, js_browser, js_freestanding, native, wasm')
exit(1)
eprintln_exit('Unknown V backend: ${sbackend}\nValid -backend choices are: c, go, interpret, js, js_node, js_browser, js_freestanding, native, wasm')
}
if b.is_js() {
res.output_cross_c = true
Expand All @@ -833,8 +835,7 @@ pub fn parse_args_and_show_errors(known_external_commands []string, args []strin
path := cmdline.option(current_args, '-custom-prelude', '')
res.build_options << '${arg} ${path}'
prelude := os.read_file(path) or {
eprintln('cannot open custom prelude file: ${err}')
exit(1)
eprintln_exit('cannot open custom prelude file: ${err}')
}
res.custom_prelude = prelude
i++
Expand Down Expand Up @@ -874,8 +875,7 @@ pub fn parse_args_and_show_errors(known_external_commands []string, args []strin
}
else {
if command == 'build' && is_source_file(arg) {
eprintln('Use `v ${arg}` instead.')
exit(1)
eprintln_exit('Use `v ${arg}` instead.')
}
if arg.len != 0 && arg[0] == `-` {
if arg[1..] in pref.list_of_flags_with_param {
Expand All @@ -892,8 +892,7 @@ pub fn parse_args_and_show_errors(known_external_commands []string, args []strin
}
} else if is_source_file(command) && is_source_file(arg)
&& command !in known_external_commands && res.raw_vsh_tmp_prefix == '' {
eprintln('Too many targets. Specify just one target: <target.v|target_directory>.')
exit(1)
eprintln_exit('Too many targets. Specify just one target: <target.v|target_directory>.')
}
continue
}
Expand All @@ -907,8 +906,7 @@ pub fn parse_args_and_show_errors(known_external_commands []string, args []strin
continue
}
extension := if command.len == 0 { '' } else { ' for command `${command}`' }
eprintln('Unknown argument `${arg}`${extension}')
exit(1)
eprintln_exit('Unknown argument `${arg}`${extension}')
}
}
}
Expand All @@ -919,21 +917,18 @@ pub fn parse_args_and_show_errors(known_external_commands []string, args []strin
res.is_run = true
}
if command == 'run' && res.is_prod && os.is_atty(1) > 0 {
eprintln_cond(show_output, "Note: building an optimized binary takes much longer. It shouldn't be used with `v run`.")
eprintln_cond(show_output, 'Use `v run` without optimization, or build an optimized binary with -prod first, then run it separately.')
eprintln_cond(show_output && !res.is_quiet, "Note: building an optimized binary takes much longer. It shouldn't be used with `v run`.")
eprintln_cond(show_output && !res.is_quiet, 'Use `v run` without optimization, or build an optimized binary with -prod first, then run it separately.')
}
if res.os in [.browser, .wasi] && res.backend != .wasm {
eprintln('OS `${res.os}` forbidden for backends other than wasm')
exit(1)
eprintln_exit('OS `${res.os}` forbidden for backends other than wasm')
}
if res.backend == .wasm && res.os !in [.browser, .wasi, ._auto] {
eprintln('Native WebAssembly backend OS must be `browser` or `wasi`')
exit(1)
eprintln_exit('Native WebAssembly backend OS must be `browser` or `wasi`')
}

if command != 'doc' && res.out_name.ends_with('.v') {
eprintln('Cannot save output binary in a .v file.')
exit(1)
eprintln_exit('Cannot save output binary in a .v file.')
}
if res.fast_math {
if res.ccompiler_type == .msvc {
Expand All @@ -949,8 +944,7 @@ pub fn parse_args_and_show_errors(known_external_commands []string, args []strin

if res.is_run || res.is_crun {
if command_pos + 2 > args.len {
eprintln('v run: no v files listed')
exit(1)
eprintln_exit('v run: no v files listed')
}
res.path = args[command_pos + 1]
res.run_args = args[command_pos + 2..]
Expand All @@ -962,14 +956,14 @@ pub fn parse_args_and_show_errors(known_external_commands []string, args []strin
must_exist(res.path)
if !res.path.ends_with('.v') && os.is_executable(res.path) && os.is_file(res.path)
&& os.is_file(res.path + '.v') {
eprintln_cond(show_output, 'It looks like you wanted to run "${res.path}.v", so we went ahead and did that since "${res.path}" is an executable.')
eprintln_cond(show_output && !res.is_quiet, 'It looks like you wanted to run "${res.path}.v", so we went ahead and did that since "${res.path}" is an executable.')
res.path += '.v'
}
} else if is_source_file(command) {
res.path = command
}
if !res.is_bare && res.bare_builtin_dir != '' {
eprintln_cond(show_output, '`-bare-builtin-dir` must be used with `-freestanding`')
eprintln_cond(show_output && !res.is_quiet, '`-bare-builtin-dir` must be used with `-freestanding`')
}
if command.ends_with('.vsh') || (res.raw_vsh_tmp_prefix != '' && !res.is_run) {
// `v build.vsh gcc` is the same as `v run build.vsh gcc`,
Expand All @@ -981,8 +975,7 @@ pub fn parse_args_and_show_errors(known_external_commands []string, args []strin
} else if command == 'interpret' {
res.backend = .interpret
if command_pos + 2 > args.len {
eprintln('v interpret: no v files listed')
exit(1)
eprintln_exit('v interpret: no v files listed')
}
res.path = args[command_pos + 1]
res.run_args = args[command_pos + 2..]
Expand All @@ -991,16 +984,15 @@ pub fn parse_args_and_show_errors(known_external_commands []string, args []strin
must_exist(res.path)
if !res.path.ends_with('.v') && os.is_executable(res.path) && os.is_file(res.path)
&& os.is_file(res.path + '.v') {
eprintln('It looks like you wanted to run "${res.path}.v", so we went ahead and did that since "${res.path}" is an executable.')
eprintln_cond(show_output && !res.is_quiet, 'It looks like you wanted to run "${res.path}.v", so we went ahead and did that since "${res.path}" is an executable.')
res.path += '.v'
}
}
}
if command == 'build-module' {
res.build_mode = .build_module
if command_pos + 1 >= args.len {
eprintln('v build-module: no module specified')
exit(1)
eprintln_exit('v build-module: no module specified')
}
res.path = args[command_pos + 1]
}
Expand All @@ -1024,6 +1016,12 @@ pub fn parse_args_and_show_errors(known_external_commands []string, args []strin
return res, command
}

[noreturn]
pub fn eprintln_exit(s string) {
eprintln(s)
exit(1)
}

pub fn eprintln_cond(condition bool, s string) {
if !condition {
return
Expand Down Expand Up @@ -1090,8 +1088,7 @@ pub fn arch_from_string(arch_str string) !Arch {

fn must_exist(path string) {
if !os.exists(path) {
eprintln('v expects that `${path}` exists, but it does not')
exit(1)
eprintln_exit('v expects that `${path}` exists, but it does not')
}
}

Expand Down Expand Up @@ -1167,9 +1164,6 @@ fn (mut prefs Preferences) parse_define(define string) {
if define_parts.len == 1 {
prefs.compile_defines << define
prefs.compile_defines_all << define
if define == 'no_bounds_checking' {
prefs.no_bounds_checking = true
}
return
}
if define_parts.len == 2 {
Expand All @@ -1180,25 +1174,20 @@ fn (mut prefs Preferences) parse_define(define string) {
prefs.compile_defines << define_parts[0]
}
else {
println(
eprintln_exit(
'V error: Unknown define argument value `${define_parts[1]}` for ${define_parts[0]}.' +
' Expected `0` or `1`.')
exit(1)
}
}
return
}
println('V error: Unknown define argument: ${define}. Expected at most one `=`.')
exit(1)
eprintln_exit('V error: Unknown define argument: ${define}. Expected at most one `=`.')
}

fn (mut prefs Preferences) diagnose_deprecated_defines(define_parts []string) {
if define_parts[0] == 'force_embed_file' {
eprintln('`-d force_embed_file` was deprecated in 2022/06/01. Now \$embed_file(file) always embeds the file, unless you pass `-d embed_only_metadata`.')
}
if define_parts[0] == 'no_bounds_checking' {
eprintln('`-d no_bounds_checking` was deprecated in 2022/10/30. Use `-no-bounds-checking` instead.')
}
// if define_parts[0] == 'no_bounds_checking' {
// eprintln('`-d no_bounds_checking` was deprecated in 2022/10/30. Use `-no-bounds-checking` instead.')
// }
}

pub fn supported_test_runners_list() string {
Expand Down

0 comments on commit 49b8c2f

Please sign in to comment.