v / cmd / v /
1// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
2// Use of this source code is governed by an MIT license
3// that can be found in the LICENSE file.
4module main
5
6import help
7import os
8import v.pref
9import v.util
10import v.builder
11
12const (
13 external_tools = [
14 'bin2v',
15 'bug',
16 'build-examples',
17 'build-tools',
18 'build-vbinaries',
19 'check-md',
20 'complete',
21 'doc',
22 'doctor',
23 'fmt',
24 'repl',
25 'self',
26 'setup-freetype',
27 'symlink',
28 'test',
29 'test-all', /* runs most of the tests and other checking tools, that will be run by the CI */
30 'test-cleancode',
31 'test-fmt',
32 'test-parser',
33 'test-self',
34 'tracev',
35 'up',
36 'vet',
37 'wipe-cache',
38 'watch',
39 ]
40 list_of_flags_that_allow_duplicates = ['cc', 'd', 'define', 'cf', 'cflags']
41)
42
43fn main() {
44 mut timers_should_print := false
45 $if time_v ? {
46 timers_should_print = true
47 }
48 mut timers := util.new_timers(timers_should_print)
49 timers.start('v total')
50 defer {
51 timers.show('v total')
52 }
53 timers.start('v start')
54 timers.show('v start')
55 args := os.args[1..]
56 // args = 123
57 if args.len == 0 || args[0] in ['-', 'repl'] {
58 // Running `./v` without args launches repl
59 if args.len == 0 {
60 if os.is_atty(0) != 0 {
61 println('Welcome to the V REPL (for help with V itself, type `exit`, then run `v help`).')
62 } else {
63 mut args_and_flags := util.join_env_vflags_and_os_args()[1..].clone()
64 args_and_flags << ['run', '-']
65 pref.parse_args(external_tools, args_and_flags)
66 }
67 }
68 util.launch_tool(false, 'vrepl', os.args[1..])
69 return
70 }
71 args_and_flags := util.join_env_vflags_and_os_args()[1..]
72 prefs, command := pref.parse_args(external_tools, args_and_flags)
73 if prefs.is_verbose {
74 // println('args= ')
75 // println(args) // QTODO
76 // println('prefs= ')
77 // println(prefs) // QTODO
78 }
79 if prefs.use_cache && os.user_os() == 'windows' {
80 eprintln('-usecache is currently disabled on windows')
81 exit(1)
82 }
83 // Start calling the correct functions/external tools
84 // Note for future contributors: Please add new subcommands in the `match` block below.
85 if command in external_tools {
86 // External tools
87 util.launch_tool(prefs.is_verbose, 'v' + command, os.args[1..])
88 return
89 }
90 match command {
91 'help' {
92 invoke_help_and_exit(args)
93 }
94 'new', 'init' {
95 util.launch_tool(prefs.is_verbose, 'vcreate', os.args[1..])
96 return
97 }
98 'translate' {
99 eprintln('Translating C to V will be available in V 0.3')
100 exit(1)
101 }
102 'search', 'install', 'update', 'upgrade', 'outdated', 'list', 'remove' {
103 util.launch_tool(prefs.is_verbose, 'vpm', os.args[1..])
104 return
105 }
106 'vlib-docs' {
107 util.launch_tool(prefs.is_verbose, 'vdoc', ['doc', 'vlib'])
108 }
109 'get' {
110 eprintln('V Error: Use `v install` to install modules from vpm.vlang.io')
111 exit(1)
112 }
113 'version' {
114 println(util.full_v_version(prefs.is_verbose))
115 return
116 }
117 else {}
118 }
119 if command in ['run', 'build', 'build-module'] || command.ends_with('.v') || os.exists(command) {
120 // println('command')
121 // println(prefs.path)
122 builder.compile(command, prefs)
123 return
124 }
125 if prefs.is_help {
126 invoke_help_and_exit(args)
127 }
128 eprintln('v $command: unknown command\nRun "v help" for usage.')
129 exit(1)
130}
131
132fn invoke_help_and_exit(remaining []string) {
133 match remaining.len {
134 0, 1 { help.print_and_exit('default') }
135 2 { help.print_and_exit(remaining[1]) }
136 else {}
137 }
138 println('`v help`: provide only one help topic.')
139 println('For usage information, use `v help`.')
140 exit(1)
141}