v/cmd/v/v.v

124 lines
3.1 KiB
V
Raw Normal View History

2020-02-09 10:08:04 +01:00
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module main
2020-05-04 14:21:32 +02:00
import help
import os
import v.table
import v.doc
import v.pref
import v.util
import v.builder
2020-02-09 10:08:04 +01:00
const (
simple_cmd = [
'fmt', 'up',
'self', 'symlink', 'bin2v',
'test', 'test-fmt', 'test-compiler', 'test-fixed',
'repl',
'build-tools', 'build-examples',
'build-vbinaries',
'setup-freetype'
2020-04-07 00:44:19 +02:00
]
list_of_flags_that_allow_duplicates = ['cc', 'd', 'define', 'cf', 'cflags']
2020-02-09 10:08:04 +01:00
)
fn main() {
main_v()
}
fn main_v() {
2020-04-16 19:41:24 +02:00
args := os.args[1..]
// args = 123
if args.len == 0 || args[0] in ['-', 'repl'] {
// Running `./v` without args launches repl
if args.len == 0 {
println('For usage information, quit V REPL using `exit` and use `v help`')
}
2020-05-28 18:40:09 +02:00
util.launch_tool(false, 'vrepl', os.args[1..])
return
}
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
}
2020-04-07 00:44:19 +02:00
if prefs.is_verbose {
// println('args= ')
// println(args) // QTODO
// println('prefs= ')
// println(prefs) // QTODO
2020-02-09 10:08:04 +01:00
}
2020-03-06 18:53:29 +01:00
// Start calling the correct functions/external tools
// Note for future contributors: Please add new subcommands in the `match` block below.
2020-02-09 10:08:04 +01:00
if command in simple_cmd {
2020-02-10 14:42:57 +01:00
// External tools
2020-05-28 18:40:09 +02:00
util.launch_tool(prefs.is_verbose, 'v' + command, os.args[1..])
2020-02-09 10:08:04 +01:00
return
}
match command {
2020-04-02 17:46:39 +02:00
'help' {
invoke_help_and_exit(args)
}
2020-04-07 19:37:15 +02:00
'new', 'init' {
2020-05-28 18:40:09 +02:00
util.launch_tool(prefs.is_verbose, 'vcreate', os.args[1..])
2020-03-15 11:20:12 +01:00
return
}
2020-02-09 10:08:04 +01:00
'translate' {
2020-02-19 16:12:39 +01:00
println('Translating C to V will be available in V 0.3')
2020-03-06 18:53:29 +01:00
return
2020-02-09 10:08:04 +01:00
}
'search', 'install', 'update', 'remove' {
2020-05-28 18:40:09 +02:00
util.launch_tool(prefs.is_verbose, 'vpm', os.args[1..])
2020-03-06 18:53:29 +01:00
return
2020-02-09 10:08:04 +01:00
}
'get' {
2020-03-06 18:53:29 +01:00
println('V Error: Use `v install` to install modules from vpm.vlang.io')
exit(1)
2020-02-09 10:08:04 +01:00
}
'version' {
println(util.full_v_version(prefs.is_verbose))
return
}
2020-03-06 18:53:29 +01:00
'doc' {
2020-05-15 23:09:38 +02:00
mut mod := ''
2020-04-02 17:46:39 +02:00
if args.len == 1 {
println('v doc [module]')
} else if args.len > 1 {
2020-05-15 23:09:38 +02:00
mod = args[1]
2020-03-06 18:53:29 +01:00
}
table := table.new_table()
2020-05-15 23:09:38 +02:00
println(doc.doc(mod, table, prefs))
2020-03-06 18:53:29 +01:00
return
}
else {}
}
if command in ['run', 'build-module'] || command.ends_with('.v') || os.exists(command) {
// println('command')
// println(prefs.path)
2020-04-07 00:44:19 +02:00
builder.compile(command, prefs)
return
}
2020-03-06 18:53:29 +01:00
eprintln('v $command: unknown command\nRun "v help" for usage.')
exit(1)
}
2020-03-13 20:52:49 +01:00
fn invoke_help_and_exit(remaining []string) {
match remaining.len {
0, 1 { help.print_and_exit('default') }
2 { help.print_and_exit(remaining[1]) }
2020-03-13 20:52:49 +01:00
else {}
}
println('V Error: Expected only one help topic to be provided.')
println('For usage information, use `v help`.')
exit(1)
}