v/cmd/v/v.v

145 lines
3.6 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
import (
compiler
2020-03-06 18:53:29 +01:00
internal.compile
internal.flag
2020-02-16 12:50:25 +01:00
internal.help
2020-02-09 10:08:04 +01:00
os
2020-02-19 16:12:39 +01:00
v.table
v.doc
2020-02-09 10:08:04 +01:00
)
const (
2020-02-10 14:42:57 +01:00
simple_cmd = ['fmt',
2020-02-28 13:02:56 +01:00
'up', 'self',
2020-02-10 14:42:57 +01:00
'create',
'test', 'test-fmt', 'test-compiler',
'bin2v',
'repl',
2020-02-24 18:01:35 +01:00
'build-tools', 'build-examples', 'build-vbinaries',
'setup-freetype']
2020-02-09 10:08:04 +01:00
)
fn main() {
2020-03-06 18:53:29 +01:00
prefs := flag.MainCmdPreferences{}
values := flag.parse_main_cmd(os.args, parse_flags, &prefs) or {
2020-03-13 09:45:40 +01:00
println('V Error: An error has occurred while parsing flags: ')
2020-03-06 18:53:29 +01:00
println(err)
exit(1)
2020-02-09 10:08:04 +01:00
}
2020-03-06 18:53:29 +01:00
if prefs.verbosity.is_higher_or_equal(.level_two) {
println('V $compiler.Version $compiler.vhash()')
2020-02-09 10:08:04 +01:00
}
2020-03-06 18:53:29 +01:00
if prefs.verbosity.is_higher_or_equal(.level_three) {
println('Parsed preferences: ')
println(prefs)
println('Remaining: $values')
2020-02-09 10:08:04 +01:00
}
2020-03-06 18:53:29 +01:00
// Do a quick check for `v -v`. Too much error has been made this way.
if prefs.verbosity == .level_one && values.len == 0 {
println("`v -v` now runs V with verbose mode set to level one which doesn't do anything.")
println('Did you mean `v -version` instead?')
exit(1)
}
// Start calling the correct functions/external tools
// Note for future contributors: Please add new subcommands in the `match` block below.
if prefs.action == .version {
disallow_unknown_flags(prefs)
print_version_and_exit()
}
if values.len == 0 && prefs.action == .help {
println('Use `v help` for usage information.')
exit(1)
}
if values.len == 0 || values[0] == '-' || values[0] == 'repl' {
// Check for REPL.
if values.len == 0 {
println('Running REPL as no arguments are provided.')
println('For usage information, quit V REPL using `exit` and use `v help`.')
}
launch_tool(prefs.verbosity, 'vrepl')
2020-02-19 16:12:39 +01:00
}
2020-03-06 18:53:29 +01:00
command := values[0]
2020-02-09 10:08:04 +01:00
if command in simple_cmd {
2020-02-10 14:42:57 +01:00
// External tools
2020-03-06 18:53:29 +01:00
launch_tool(prefs.verbosity, 'v' + command)
2020-02-09 10:08:04 +01:00
return
}
match command {
'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-03-06 18:53:29 +01:00
launch_tool(prefs.verbosity, 'vpm')
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
}
'symlink' {
2020-03-06 18:53:29 +01:00
disallow_unknown_flags(prefs)
2020-02-09 10:08:04 +01:00
create_symlink()
2020-03-06 18:53:29 +01:00
return
2020-02-09 10:08:04 +01:00
}
2020-03-06 18:53:29 +01:00
'doc' {
disallow_unknown_flags(prefs)
if values.len == 1 {
println('V Error: Expected argument: Module name to output documentations for')
exit(1)
}
table := table.new_table()
println(doc.doc(values[1], table))
return
}
'help' {
// We check if the arguments are empty as we don't want to steal it from tools
// TODO Call actual help tool
disallow_unknown_flags(prefs)
if prefs.verbosity.is_higher_or_equal(.level_one) {
println(help.verbose_help_text)
}
else {
println(help.help_text)
}
if values.len > 1 {
println('Note: Actual help module is coming soon. Feel free to ask on the official channels for clarification.')
}
return
}
'version' {
disallow_unknown_flags(prefs)
print_version_and_exit()
return
2020-02-09 10:08:04 +01:00
}
2020-03-06 18:53:29 +01:00
else {}
}
if command == 'run' || command == 'build' || command.ends_with('.v') || os.exists(command) {
arg := join_flags_and_argument()
compile.compile(command, arg)
return
}
eprintln('v $command: unknown command\nRun "v help" for usage.')
exit(1)
}
fn print_version_and_exit() {
version_hash := compiler.vhash()
println('V $compiler.Version $version_hash')
exit(0)
}
[inline]
fn disallow_unknown_flags(prefs flag.MainCmdPreferences) {
if prefs.unknown_flag == '' {
return
2020-02-09 10:08:04 +01:00
}
2020-03-06 18:53:29 +01:00
println('V Error: Unexpected flag found: $prefs.unknown_flag')
exit(1)
2020-02-09 10:08:04 +01:00
}