cli: add option with default
parent
5481c0fc84
commit
473617f061
|
@ -25,24 +25,44 @@ pub fn (flags []Flag) get_bool(name string) ?bool {
|
||||||
return flag.value == 'true'
|
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 {
|
pub fn (flags []Flag) get_int(name string) ?int {
|
||||||
flag := flags.get(name) or { return error(err) }
|
flag := flags.get(name) or { return error(err) }
|
||||||
if flag.flag != .int { return error('invalid flag type') }
|
if flag.flag != .int { return error('invalid flag type') }
|
||||||
return flag.value.int()
|
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 {
|
pub fn (flags []Flag) get_float(name string) ?f32 {
|
||||||
flag := flags.get(name) or { return error(err) }
|
flag := flags.get(name) or { return error(err) }
|
||||||
if flag.flag != .float { return error('invalid flag type') }
|
if flag.flag != .float { return error('invalid flag type') }
|
||||||
return flag.value.f32()
|
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 {
|
pub fn (flags []Flag) get_string(name string) ?string {
|
||||||
flag := flags.get(name) or { return error(err) }
|
flag := flags.get(name) or { return error(err) }
|
||||||
if flag.flag != .string { return error('invalid flag type') }
|
if flag.flag != .string { return error('invalid flag type') }
|
||||||
return flag.value
|
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
|
// parse flag value from arguments and return arguments with all consumed element removed
|
||||||
fn (flag mut Flag) parse(args []string) ?[]string {
|
fn (flag mut Flag) parse(args []string) ?[]string {
|
||||||
if flag.matches(args) {
|
if flag.matches(args) {
|
||||||
|
|
Loading…
Reference in New Issue