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

104 lines
2.5 KiB
V
Raw Normal View History

2020-02-09 12:08:04 +03: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-03-06 20:53:29 +03:00
import (
internal.flag
os
)
2020-02-09 12:08:04 +03:00
const (
list_of_flags_that_allow_duplicates = ['cc','d','define','cf','cflags']
2020-02-09 12:08:04 +03:00
//list_of_flags contains a list of flags where an argument is expected past it.
list_of_flags_with_param = [
2020-03-06 20:53:29 +03:00
'o', 'output', 'd', 'define', 'b', 'backend', 'cc', 'os', 'target-os', 'arch',
'csource', 'cf', 'cflags', 'path'
2020-02-09 12:08:04 +03:00
]
)
fn join_flags_and_argument() []string {
vosargs := os.getenv('VOSARGS')
if vosargs != '' {
2020-03-06 20:53:29 +03:00
return vosargs.split(' ')
2020-02-09 12:08:04 +03:00
}
2020-03-06 20:53:29 +03:00
// No VOSARGS? Look for VFLAGS and concat it with command-line options.
2020-02-09 12:08:04 +03:00
mut args := []string
vflags := os.getenv('VFLAGS')
if vflags != '' {
args << os.args[0]
args << vflags.split(' ')
if os.args.len > 1 {
args << os.args[1..]
}
2020-03-06 20:53:29 +03:00
return args
2020-02-09 12:08:04 +03:00
}
2020-03-06 20:53:29 +03:00
// No VFLAGS too? Just return command-line args.
return os.args
}
2020-02-09 12:08:04 +03:00
2020-03-06 20:53:29 +03:00
fn parse_flags(flag string, f mut flag.Instance, prefs mut flag.MainCmdPreferences) {
match flag {
'v' {
f.is_equivalent_to(['v', 'vv', 'vvv', 'verbose'])
prefs.verbosity = .level_one
}
'vv' {
f.is_equivalent_to(['v', 'vv', 'vvv', 'verbose'])
prefs.verbosity = .level_two
}
'vvv' {
f.is_equivalent_to(['v', 'vv', 'vvv', 'verbose'])
prefs.verbosity = .level_three
}
'verbose' {
f.is_equivalent_to(['v', 'vv', 'vvv', 'verbose'])
level := f.int() or {
println('V error: Expected `0`, `1`, `2` or `3` as argument to `-verbose` to specify verbosity level.')
exit(1)
}
match level {
0 {} //Zero verbosity is already default.
1 {
prefs.verbosity = .level_one
}
2 {
prefs.verbosity = .level_two
}
3 {
prefs.verbosity = .level_three
}
else {
println('V error: Expected `0`, `1`, `2` or `3` as argument to `-verbose` to specify verbosity level.')
exit(1)
}
}
}
'h', 'help' {
f.is_equivalent_to(['h', 'help'])
prefs.action = .help
}
'version' {
2020-03-06 20:53:29 +03:00
prefs.action = .version
}
'-version', '-help' {
println('V error: `-$flag` has been deprecated. Use `$flag` instead.')
exit(1)
}
else {
if flag in list_of_flags_that_allow_duplicates {
f.allow_duplicate()
}
prefs.unknown_flag = '-$flag'
if !(flag in list_of_flags_with_param) {
2020-03-06 20:53:29 +03:00
return
}
f.string() or {
println('V error: Error parsing flag. Expected value for `-$flag`.')
exit(1)
}
return
2020-03-06 20:53:29 +03:00
}
}
2020-02-09 12:08:04 +03:00
}