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

cmd/v: further simplifications

This commit is contained in:
Alexander Medvednikov 2020-04-02 17:46:39 +02:00
parent 83289d74a7
commit d228b3916b

View File

@ -39,33 +39,41 @@ fn main() {
println(util.full_v_version()) println(util.full_v_version())
return return
} }
prefs2 := parse_args(args) prefs2, command := parse_args(args)
//println('command = $command')
//command := if values.len > 0 { values[0] } else { '' }
/*
prefs := flag.MainCmdPreferences{} prefs := flag.MainCmdPreferences{}
values := flag.parse_main_cmd(os.args, parse_flags, prefs) or { values := flag.parse_main_cmd(os.args, parse_flags, prefs) or {
println('V Error: An error has occurred while parsing flags: ') println('V Error: An error has occurred while parsing flags: ')
println(err) println(err)
exit(1) exit(1)
} }
*/
if prefs2.is_verbose { if prefs2.is_verbose {
println(util.full_v_version()) println(util.full_v_version())
} }
if prefs2.is_verbose { if prefs2.is_verbose {
println('Parsed preferences: ') //println('Parsed preferences: ')
//println(prefs) // QTODO //println(prefs) // QTODO
println('Remaining: $values') //println('Remaining: $values')
} }
// Start calling the correct functions/external tools // Start calling the correct functions/external tools
// Note for future contributors: Please add new subcommands in the `match` block below. // Note for future contributors: Please add new subcommands in the `match` block below.
if values.len == 0 && prefs.action == .help {
invoke_help_and_exit(values)
}
command := if values.len > 0 { values[0] } else { '' }
if command in simple_cmd { if command in simple_cmd {
// External tools // External tools
launch_tool(prefs2.is_verbose, 'v' + command) launch_tool(prefs2.is_verbose, 'v' + command)
return return
} }
if command in ['run', 'build'] || command.ends_with('.v') || os.exists(command) {
arg := join_flags_and_argument()
compile.compile(command, arg)
return
}
match command { match command {
'help' {
invoke_help_and_exit(args)
}
'create', 'init' { 'create', 'init' {
launch_tool(prefs2.is_verbose, 'vcreate') launch_tool(prefs2.is_verbose, 'vcreate')
return return
@ -83,46 +91,52 @@ fn main() {
exit(1) exit(1)
} }
'symlink' { 'symlink' {
disallow_unknown_flags(prefs)
create_symlink() create_symlink()
return return
} }
'doc' { 'doc' {
disallow_unknown_flags(prefs) if args.len == 1 {
if values.len == 1 { println('v doc [module]')
println('V Error: Expected argument: Module name to output documentations for')
exit(1) exit(1)
} }
table := table.new_table() table := table.new_table()
println(doc.doc(values[1], table)) println(doc.doc(args[1], table))
return return
} }
'help' { 'help' {
disallow_unknown_flags(prefs) invoke_help_and_exit(args)
invoke_help_and_exit(values)
return return
} }
else {} 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.') eprintln('v $command: unknown command\nRun "v help" for usage.')
exit(1) exit(1)
} }
fn parse_args(args []string) &pref.Preferences{ fn parse_args(args []string) (&pref.Preferences, string) {
mut res := &pref.Preferences{} mut res := &pref.Preferences{}
for i, arg in args { mut command := ''
//for i, arg in args {
for i := 0 ; i < args.len; i ++ {
arg := args[i]
match arg { match arg {
'-v' { res.is_verbose = true } '-v' { res.is_verbose = true }
'-cg' { res.ccompiler = cmdline.option(args, '-cc', 'cc') } '-cg' {
else { } res.ccompiler = cmdline.option(args, '-cc', 'cc')
i++
}
'-o' {
res.out_name = cmdline.option(args, '-o', '')
i++
}
else {
if !arg.starts_with('-') && command == '' {
command = arg
}
}
} }
} }
return res return res, command
} }
fn invoke_help_and_exit(remaining []string) { fn invoke_help_and_exit(remaining []string) {