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

all: change optional to result in most of the libraries (#16123)

This commit is contained in:
yuyi
2022-10-21 03:14:33 +08:00
committed by GitHub
parent 0d368562f4
commit 51f4d99399
75 changed files with 439 additions and 446 deletions

View File

@ -42,7 +42,7 @@ pub fn (flags []Flag) get_all_found() []Flag {
// get_bool returns `true` if the flag is set.
// get_bool returns an error if the `FlagType` is not boolean.
pub fn (flag Flag) get_bool() ?bool {
pub fn (flag Flag) get_bool() !bool {
if flag.flag != .bool {
return error('$flag.name: Invalid flag type `$flag.flag`, expected `bool`')
}
@ -54,14 +54,14 @@ pub fn (flag Flag) get_bool() ?bool {
// get_bool returns `true` if the flag specified in `name` is set.
// get_bool returns an error if the `FlagType` is not boolean.
pub fn (flags []Flag) get_bool(name string) ?bool {
flag := flags.get(name)?
pub fn (flags []Flag) get_bool(name string) !bool {
flag := flags.get(name)!
return flag.get_bool()
}
// get_int returns the `int` value argument of the flag.
// get_int returns an error if the `FlagType` is not integer.
pub fn (flag Flag) get_int() ?int {
pub fn (flag Flag) get_int() !int {
if flag.flag != .int {
return error('$flag.name: Invalid flag type `$flag.flag`, expected `int`')
}
@ -77,7 +77,7 @@ pub fn (flag Flag) get_int() ?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 {
pub fn (flag Flag) get_ints() ![]int {
if flag.flag != .int_array {
return error('$flag.name: Invalid flag type `$flag.flag`, expected `int_array`')
}
@ -99,21 +99,21 @@ pub fn (flag Flag) get_ints() ?[]int {
// get_int returns the `int` value argument of the flag specified in `name`.
// get_int returns an error if the `FlagType` is not integer.
pub fn (flags []Flag) get_int(name string) ?int {
flag := flags.get(name)?
pub fn (flags []Flag) get_int(name string) !int {
flag := flags.get(name)!
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)?
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 {
pub fn (flag Flag) get_float() !f64 {
if flag.flag != .float {
return error('$flag.name: Invalid flag type `$flag.flag`, expected `float`')
}
@ -129,7 +129,7 @@ pub fn (flag Flag) get_float() ?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 {
pub fn (flag Flag) get_floats() ![]f64 {
if flag.flag != .float_array {
return error('$flag.name: Invalid flag type `$flag.flag`, expected `float_array`')
}
@ -151,21 +151,21 @@ pub fn (flag Flag) get_floats() ?[]f64 {
// get_float returns the `f64` value argument of the flag specified in `name`.
// get_float returns an error if the `FlagType` is not floating point.
pub fn (flags []Flag) get_float(name string) ?f64 {
flag := flags.get(name)?
pub fn (flags []Flag) get_float(name string) !f64 {
flag := flags.get(name)!
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)?
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 {
pub fn (flag Flag) get_string() !string {
if flag.flag != .string {
return error('$flag.name: Invalid flag type `$flag.flag`, expected `string`')
}
@ -181,7 +181,7 @@ pub fn (flag Flag) get_string() ?string {
// 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 {
pub fn (flag Flag) get_strings() ![]string {
if flag.flag != .string_array {
return error('$flag.name: Invalid flag type `$flag.flag`, expected `string_array`')
}
@ -197,24 +197,24 @@ pub fn (flag Flag) get_strings() ?[]string {
// get_string returns the `string` value argument of the flag specified in `name`.
// get_string returns an error if the `FlagType` is not string.
pub fn (flags []Flag) get_string(name string) ?string {
flag := flags.get(name)?
pub fn (flags []Flag) get_string(name string) !string {
flag := flags.get(name)!
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)?
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, posix_mode bool) ?[]string {
fn (mut flag Flag) parse(args []string, posix_mode bool) ![]string {
if flag.matches(args, posix_mode) {
if flag.flag == .bool {
new_args := flag.parse_bool(args)?
new_args := flag.parse_bool(args)!
return new_args
} else {
if flag.value.len > 0 && flag.flag != .int_array && flag.flag != .float_array
@ -222,7 +222,7 @@ fn (mut flag Flag) parse(args []string, posix_mode bool) ?[]string {
return error('The argument `$flag.name` accept only one value!')
}
new_args := flag.parse_raw(args)?
new_args := flag.parse_raw(args)!
return new_args
}
} else {
@ -239,7 +239,7 @@ fn (mut flag Flag) matches(args []string, posix_mode bool) bool {
|| (flag.abbrev != '' && args[0].starts_with('-$flag.abbrev='))
}
fn (mut flag Flag) parse_raw(args []string) ?[]string {
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]
return args[1..]
@ -250,7 +250,7 @@ fn (mut flag Flag) parse_raw(args []string) ?[]string {
return error('Missing argument for `$flag.name`')
}
fn (mut flag Flag) parse_bool(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]]
return args[1..]
@ -267,7 +267,7 @@ fn (mut flag Flag) parse_bool(args []string) ?[]string {
// get returns the `Flag` matching `name` or an error
// if it can't be found.
fn (flags []Flag) get(name string) ?Flag {
fn (flags []Flag) get(name string) !Flag {
for flag in flags {
if flag.name == name {
return flag