From 473617f0617b2419599a8a5ede570b28d906b8a4 Mon Sep 17 00:00:00 2001 From: Tim Basel Date: Thu, 30 Apr 2020 12:18:46 +0200 Subject: [PATCH] cli: add option with default --- vlib/cli/flag.v | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/vlib/cli/flag.v b/vlib/cli/flag.v index 5f0bba5c0a..3c9eb6b198 100644 --- a/vlib/cli/flag.v +++ b/vlib/cli/flag.v @@ -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) {