1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

error on an incorrect V command

This commit is contained in:
Alexander Medvednikov 2019-12-15 04:51:18 +03:00
parent 5971aa7fef
commit 12ebed0589
2 changed files with 79 additions and 60 deletions

135
v.v
View File

@ -12,6 +12,12 @@ import (
//time //time
) )
const (
known_commands = ['run', 'build', 'version', 'doc']
simple_tools = ['up', 'create', 'test', 'test-compiler', 'build-tools',
'build-examples', 'build-vbinaries']
)
fn main() { fn main() {
//t := time.ticks() //t := time.ticks()
//defer { println(time.ticks() - t) } //defer { println(time.ticks() - t) }
@ -22,81 +28,37 @@ fn main() {
// NOT passed through VFLAGS, otherwise the naked `v` invocation for // NOT passed through VFLAGS, otherwise the naked `v` invocation for
// the repl does not work when you have VFLAGS with -cc or -cflags set // the repl does not work when you have VFLAGS with -cc or -cflags set
// which may be surprising to v users. // which may be surprising to v users.
stuff_after_executable := os.args[1..] command := if os.args.len > 1 { os.args[1] } else { '' }
commands := stuff_after_executable.filter(!it.starts_with('-')) // external tool
if command in simple_tools {
simple_tools := ['up', 'create', 'test', 'test-compiler', 'build-tools', 'build-examples', 'build-vbinaries'] compiler.launch_tool('v' + command)
for tool in simple_tools { return
if tool in commands { }
compiler.launch_tool('v$tool') // v run, v doc, etc
return if !command.starts_with('-') && !os.exists(command) {
} v_command(command, args)
} }
// Print the version and exit. // Print the version and exit.
if '-v' in options || '--version' in options || 'version' in commands { if '-v' in options || '--version' in options {
version_hash := compiler.vhash() version_hash := compiler.vhash()
println('V $compiler.Version $version_hash') println('V $compiler.Version $version_hash')
return return
} }
else if '-h' in options || '--help' in options || 'help' in commands { else if '-h' in options || '--help' in options {
println(compiler.help_text) println(compiler.help_text)
return return
} }
else if 'translate' in commands {
println('Translating C to V will be available in V 0.3 (January)')
return
}
else if 'search' in commands || 'install' in commands || 'update' in commands || 'remove' in commands {
compiler.launch_tool('vpm')
return
}
else if ('get' in commands) { // obsoleted
println('use `v install` to install modules from vpm.vlang.io ')
return
}
else if 'symlink' in commands {
compiler.create_symlink()
return
}
// TODO quit if the v compiler is too old
// u := os.file_last_mod_unix('v')
// If there's no tmp path with current version yet, the user must be using a pre-built package
//
// Just fmt and exit
else if 'fmt' in commands {
compiler.vfmt(args)
return
}
// No args? REPL // No args? REPL
else if 'runrepl' in commands || commands.len == 0 || (args.len == 2 && args[1] == '-') { else if command == '' || (args.len == 2 && args[1] == '-') {
compiler.launch_tool('vrepl') compiler.launch_tool('vrepl')
return return
} }
// Generate the docs and exit
else if 'doc' in commands {
vexe := os.executable()
vdir := os.dir(os.executable())
os.chdir(vdir)
mod := args.last()
os.system('$vexe build module vlib$os.path_separator' + args.last())
txt := os.read_file(filepath.join(compiler.v_modules_path, 'vlib', '${mod}.vh')) or {
panic(err)
}
println(txt)
exit(0)
// v.gen_doc_html_for_module(args.last())
}
else {
//println('unknown command/argument\n')
//println(compiler.help_text)
}
// Construct the V object from command line arguments // Construct the V object from command line arguments
mut v := compiler.new_v(args) mut v := compiler.new_v(args)
if v.pref.is_verbose { if v.pref.is_verbose {
println(args) println(args)
} }
if 'run' in args { if command == 'run' {
// always recompile for now, too error prone to skip recompilation otherwise // always recompile for now, too error prone to skip recompilation otherwise
// for example for -repl usage, especially when piping lines to v // for example for -repl usage, especially when piping lines to v
v.compile() v.compile()
@ -110,7 +72,7 @@ fn main() {
} }
if v.pref.is_stats { if v.pref.is_stats {
tmark.stop() tmark.stop()
println( 'compilation took: ' + tmark.total_duration().str() + 'ms') println('compilation took: ' + tmark.total_duration().str() + 'ms')
} }
if v.pref.is_test { if v.pref.is_test {
v.run_compiled_executable_and_exit() v.run_compiled_executable_and_exit()
@ -118,3 +80,60 @@ fn main() {
v.finalize_compilation() v.finalize_compilation()
} }
fn v_command(command string, args []string) {
match command {
'', '.', 'run' {
}
'version' {
println('V $compiler.Version $compiler.vhash()')
return
}
'help' {
println(compiler.help_text)
return
}
'translate' {
println('Translating C to V will be available in V 0.3 (January)')
return
}
'search', 'install', 'update' {
compiler.launch_tool('vpm')
}
'get' {
println('use `v install` to install modules from vpm.vlang.io ')
return
}
'symlink' {
compiler.create_symlink()
return
}
'fmt' {
compiler.vfmt(args)
return
}
'runrepl' {
compiler.launch_tool('vrepl')
return
}
'doc' {
vexe := os.executable()
vdir := os.dir(os.executable())
os.chdir(vdir)
mod := args.last()
os.system('$vexe build module vlib$os.path_separator' + args.last())
txt := os.read_file(filepath.join(compiler.v_modules_path, 'vlib', '${mod}.vh')) or {
panic(err)
}
println(txt)
exit(0)
// v.gen_doc_html_for_module(args.last())
}
else {
println('v $command: unknown command')
println('Run "v help" for usage.')
return
}
}
}

View File

@ -510,7 +510,7 @@ pub fn (v mut V) generate_main() {
if test_fn_names.len == 0 { if test_fn_names.len == 0 {
verror('test files need to have at least one test function') verror('test files need to have at least one test function')
} }
// Generate a C `main`, which calls every single test function // Generate a C `main`, which calls every single test function
v.gen_main_start(false) v.gen_main_start(false)
@ -782,7 +782,7 @@ pub fn (v &V) get_user_files() []string {
user_files << single_test_v_file user_files << single_test_v_file
dir = os.basedir( single_test_v_file ) dir = os.basedir( single_test_v_file )
} }
if dir.ends_with('.v') || dir.ends_with('.vsh') { if dir.ends_with('.v') || dir.ends_with('.vsh') {
single_v_file := dir single_v_file := dir
// Just compile one file and get parent dir // Just compile one file and get parent dir