cli: add option with default

pull/4696/head
Tim Basel 2020-04-30 12:18:46 +02:00 committed by GitHub
parent 5481c0fc84
commit 473617f061
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 20 additions and 0 deletions

View File

@ -25,24 +25,44 @@ pub fn (flags []Flag) get_bool(name string) ?bool {
return flag.value == 'true'
}
pub fn (flags []Flag) get_bool_or(name string, or_value bool) bool {
value := flags.get_bool(name) or { return or_value }
return value
}
pub fn (flags []Flag) get_int(name string) ?int {
flag := flags.get(name) or { return error(err) }
if flag.flag != .int { return error('invalid flag type') }
return flag.value.int()
}
pub fn (flags []Flag) get_int_or(name string, or_value int) int {
value := flags.get_int(name) or { return or_value }
return value
}
pub fn (flags []Flag) get_float(name string) ?f32 {
flag := flags.get(name) or { return error(err) }
if flag.flag != .float { return error('invalid flag type') }
return flag.value.f32()
}
pub fn (flags []Flag) get_float_or(name string, or_value f32) f32 {
value := flags.get_float(name) or { return or_value }
return value
}
pub fn (flags []Flag) get_string(name string) ?string {
flag := flags.get(name) or { return error(err) }
if flag.flag != .string { return error('invalid flag type') }
return flag.value
}
pub fn (flags []Flag) get_string_or(name string, or_value string) string {
value := flags.get_string(name) or { return or_value }
return value
}
// parse flag value from arguments and return arguments with all consumed element removed
fn (flag mut Flag) parse(args []string) ?[]string {
if flag.matches(args) {