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

cli: allow flag to be set multi time (#8256)

This commit is contained in:
Emeric MARTINEAU
2021-01-22 18:03:02 +01:00
committed by GitHub
parent f2c6735d92
commit 081e3c46b4
5 changed files with 286 additions and 28 deletions

View File

@ -18,9 +18,16 @@ pub mut:
description string
global bool
required bool
value string
value []string = []
// If allow multiple value.
// If bool, multiple has no impact, bool can only set once.
// If not multiple, and multiple value set at command args, raise an error.
multipe bool
mut:
// Set true if flag found.
found bool
// Set true at first init value.
init bool
}
// get_all_found returns an array of all `Flag`s found in the command parameters
@ -34,7 +41,7 @@ pub fn (flag Flag) get_bool() ?bool {
if flag.flag != .bool {
return error('$flag.name: Invalid flag type `$flag.flag`, expected `bool`')
}
return flag.value == 'true'
return flag.value.len > 0 && flag.value[0] == 'true'
}
// get_bool returns `true` if the flag specified in `name` is set.
@ -50,7 +57,32 @@ pub fn (flag Flag) get_int() ?int {
if flag.flag != .int {
return error('$flag.name: Invalid flag type `$flag.flag`, expected `int`')
}
return flag.value.int()
if flag.value.len == 0 {
return 0
} else {
return flag.value[0].int()
}
}
// get_ints returns the array of `int` value argument of the flag specified in `name`.
// get_ints returns an error if the `FlagType` is not integer.
pub fn (flag Flag) get_ints() ?[]int {
if flag.flag != .int {
return error('$flag.name: Invalid flag type `$flag.flag`, expected `int`')
}
if flag.value.len == 0 {
return []int{}
} else {
mut val := []int{}
for f in flag.value {
val << f.int()
}
return val
}
}
// get_int returns the `int` value argument of the flag specified in `name`.
@ -60,13 +92,45 @@ pub fn (flags []Flag) get_int(name string) ?int {
return flag.get_int()
}
// get_ints returns the array of `int` value argument of the flag specified in `name`.
// get_ints returns an error if the `FlagType` is not integer.
pub fn (flags []Flag) get_ints(name string) ?[]int {
flag := flags.get(name) ?
return flag.get_ints()
}
// get_float returns the `f64` value argument of the flag.
// get_float returns an error if the `FlagType` is not floating point.
pub fn (flag Flag) get_float() ?f64 {
if flag.flag != .float {
return error('$flag.name: Invalid flag type `$flag.flag`, expected `float`')
}
return flag.value.f64()
if flag.value.len == 0 {
return 0.0
} else {
return flag.value[0].f64()
}
}
// get_floats returns the `f64` value argument of the flag.
// get_floats returns an error if the `FlagType` is not floating point.
pub fn (flag Flag) get_floats() ?[]f64 {
if flag.flag != .float {
return error('$flag.name: Invalid flag type `$flag.flag`, expected `float`')
}
if flag.value.len == 0 {
return []f64{}
} else {
mut val := []f64{}
for f in flag.value {
val << f.f64()
}
return val
}
}
// get_float returns the `f64` value argument of the flag specified in `name`.
@ -76,13 +140,39 @@ pub fn (flags []Flag) get_float(name string) ?f64 {
return flag.get_float()
}
// get_floats returns the array of `f64` value argument of the flag specified in `name`.
// get_floats returns an error if the `FlagType` is not floating point.
pub fn (flags []Flag) get_floats(name string) ?[]f64 {
flag := flags.get(name) ?
return flag.get_floats()
}
// get_string returns the `string` value argument of the flag.
// get_string returns an error if the `FlagType` is not string.
pub fn (flag Flag) get_string() ?string {
if flag.flag != .string {
return error('$flag.name: Invalid flag type `$flag.flag`, expected `string`')
}
return flag.value
if flag.value.len == 0 {
return ''
} else {
return flag.value[0]
}
}
// get_strings returns the array of `string` value argument of the flag.
// get_strings returns an error if the `FlagType` is not string.
pub fn (flag Flag) get_strings() ?[]string {
if flag.flag != .string {
return error('$flag.name: Invalid flag type `$flag.flag`, expected `string`')
}
if flag.value.len == 0 {
return []string{}
} else {
return flag.value
}
}
// get_string returns the `string` value argument of the flag specified in `name`.
@ -92,14 +182,34 @@ pub fn (flags []Flag) get_string(name string) ?string {
return flag.get_string()
}
// get_strings returns the `string` value argument of the flag specified in `name`.
// get_strings returns an error if the `FlagType` is not string.
pub fn (flags []Flag) get_strings(name string) ?[]string {
flag := flags.get(name) ?
return flag.get_strings()
}
// parse parses flag values from arguments and return
// an array of arguments with all consumed elements removed.
fn (mut flag Flag) parse(args []string, with_abbrev bool) ?[]string {
if flag.matches(args, with_abbrev) {
// TODO
// Si pas multiple generer une erreur
// Permettre de récupérer plusieurs valeur
if flag.init == false {
flag.init = true
// Clear defaut value if set
flag.value = []
}
if flag.flag == .bool {
new_args := flag.parse_bool(args) ?
return new_args
} else {
if flag.value.len > 0 && !flag.multipe {
return error('The argument `$flag.name` accept only one value!')
}
new_args := flag.parse_raw(args) ?
return new_args
}
@ -123,10 +233,10 @@ fn (mut flag Flag) matches(args []string, with_abbrev bool) bool {
fn (mut flag Flag) parse_raw(args []string) ?[]string {
if args[0].len > flag.name.len && args[0].contains('=') {
flag.value = args[0].split('=')[1]
flag.value << args[0].split('=')[1]
return args[1..]
} else if args.len >= 2 {
flag.value = args[1]
flag.value << args[1]
return args[2..]
}
return error('Missing argument for `$flag.name`')
@ -134,15 +244,16 @@ fn (mut flag Flag) parse_raw(args []string) ?[]string {
fn (mut flag Flag) parse_bool(args []string) ?[]string {
if args[0].len > flag.name.len && args[0].contains('=') {
flag.value = args[0].split('=')[1]
flag.value = [args[0].split('=')[1]]
return args[1..]
} else if args.len >= 2 {
if args[1] in ['true', 'false'] {
flag.value = args[1]
flag.value = [args[1]]
return args[2..]
}
}
flag.value = 'true'
// In fact bool cannot be multiple
flag.value = ['true']
return args[1..]
}