diff --git a/vlib/cli/cli_flag_test.v b/vlib/cli/cli_flag_test.v index a9cc918263..9e933da934 100644 --- a/vlib/cli/cli_flag_test.v +++ b/vlib/cli/cli_flag_test.v @@ -5,13 +5,9 @@ fn test_if_string_flag_parses() { flag: .string name: 'flag' } - flag.parse(['-flag', 'value'], false) or { - panic(err) - } + flag.parse(['-flag', 'value'], false) or { panic(err) } assert flag.value == 'value' - flag.parse(['-flag=value'], false) or { - panic(err) - } + flag.parse(['-flag=value'], false) or { panic(err) } assert flag.value == 'value' } @@ -21,40 +17,20 @@ fn test_if_bool_flag_parses() { name: 'flag' } mut value := false - flag.parse(['-flag'], false) or { - panic(err) - } - value = flag.get_bool() or { - panic(err) - } + flag.parse(['-flag'], false) or { panic(err) } + value = flag.get_bool() or { panic(err) } assert value == true - flag.parse(['-flag', 'true'], false) or { - panic(err) - } - value = flag.get_bool() or { - panic(err) - } + flag.parse(['-flag', 'true'], false) or { panic(err) } + value = flag.get_bool() or { panic(err) } assert value == true - flag.parse(['-flag=true'], false) or { - panic(err) - } - value = flag.get_bool() or { - panic(err) - } + flag.parse(['-flag=true'], false) or { panic(err) } + value = flag.get_bool() or { panic(err) } assert value == true - flag.parse(['-flag', 'false'], false) or { - panic(err) - } - value = flag.get_bool() or { - panic(err) - } + flag.parse(['-flag', 'false'], false) or { panic(err) } + value = flag.get_bool() or { panic(err) } assert value == false - flag.parse(['-flag=false'], false) or { - panic(err) - } - value = flag.get_bool() or { - panic(err) - } + flag.parse(['-flag=false'], false) or { panic(err) } + value = flag.get_bool() or { panic(err) } assert value == false } @@ -64,19 +40,11 @@ fn test_if_int_flag_parses() { name: 'flag' } mut value := 0 - flag.parse(['-flag', '42'], false) or { - panic(err) - } - value = flag.get_int() or { - panic(err) - } + flag.parse(['-flag', '42'], false) or { panic(err) } + value = flag.get_int() or { panic(err) } assert value == 42 - flag.parse(['-flag=42'], false) or { - panic(err) - } - value = flag.get_int() or { - panic(err) - } + flag.parse(['-flag=42'], false) or { panic(err) } + value = flag.get_int() or { panic(err) } assert value == 42 } @@ -86,20 +54,12 @@ fn test_if_float_flag_parses() { name: 'flag' } mut value := f64(0) - flag.parse(['-flag', '3.14159'], false) or { - panic(err) - } - value = flag.get_float() or { - panic(err) - } + flag.parse(['-flag', '3.14159'], false) or { panic(err) } + value = flag.get_float() or { panic(err) } assert value == 3.14159 - flag.parse(['-flag=3.14159'], false) or { - panic(err) - } + flag.parse(['-flag=3.14159'], false) or { panic(err) } assert flag.value.f64() == 3.14159 - value = flag.get_float() or { - panic(err) - } + value = flag.get_float() or { panic(err) } assert value == 3.14159 } @@ -110,18 +70,10 @@ fn test_if_flag_parses_with_abbrev() { abbrev: 'f' } mut value := false - flag.parse(['--flag'], true) or { - panic(err) - } - value = flag.get_bool() or { - panic(err) - } + flag.parse(['--flag'], true) or { panic(err) } + value = flag.get_bool() or { panic(err) } assert value == true - flag.parse(['-flag'], true) or { - panic(err) - } - value = flag.get_bool() or { - panic(err) - } + flag.parse(['-flag'], true) or { panic(err) } + value = flag.get_bool() or { panic(err) } assert value == true } diff --git a/vlib/cli/command.v b/vlib/cli/command.v index 268fceb3ea..ca7d829ba8 100644 --- a/vlib/cli/command.v +++ b/vlib/cli/command.v @@ -149,17 +149,17 @@ fn (mut cmd Command) parse_flags() { mut found := false for i in 0 .. cmd.flags.len { unsafe { - mut flag := &cmd.flags[i] - if flag.matches(cmd.args, cmd.flags.have_abbrev()) { - found = true - flag.found = true - cmd.args = flag.parse(cmd.args, cmd.flags.have_abbrev()) or { - println('Failed to parse flag `${cmd.args[0]}`: $err') - exit(1) + mut flag := &cmd.flags[i] + if flag.matches(cmd.args, cmd.flags.have_abbrev()) { + found = true + flag.found = true + cmd.args = flag.parse(cmd.args, cmd.flags.have_abbrev()) or { + println('Failed to parse flag `${cmd.args[0]}`: $err') + exit(1) + } + break } - break } - } } if !found { println('Command `$cmd.name` has no flag `${cmd.args[0]}`') @@ -221,9 +221,7 @@ fn (mut cmd Command) parse_commands() { fn (cmd Command) check_help_flag() { if !cmd.disable_help && cmd.flags.contains('help') { - help_flag := cmd.flags.get_bool('help') or { - return - } // ignore error and handle command normally + help_flag := cmd.flags.get_bool('help') or { return } // ignore error and handle command normally if help_flag { cmd.execute_help() exit(0) @@ -233,13 +231,9 @@ fn (cmd Command) check_help_flag() { fn (cmd Command) check_version_flag() { if !cmd.disable_version && cmd.version != '' && cmd.flags.contains('version') { - version_flag := cmd.flags.get_bool('version') or { - return - } // ignore error and handle command normally + version_flag := cmd.flags.get_bool('version') or { return } // ignore error and handle command normally if version_flag { - version_cmd := cmd.commands.get('version') or { - return - } // ignore error and handle command normally + version_cmd := cmd.commands.get('version') or { return } // ignore error and handle command normally version_cmd.execute(version_cmd) exit(0) } @@ -258,9 +252,7 @@ fn (cmd Command) check_required_flags() { fn (cmd Command) execute_help() { if cmd.commands.contains('help') { - help_cmd := cmd.commands.get('help') or { - return - } // ignore error and handle command normally + help_cmd := cmd.commands.get('help') or { return } // ignore error and handle command normally help_cmd.execute(help_cmd) } else { print(cmd.help_message()) diff --git a/vlib/cli/command_test.v b/vlib/cli/command_test.v index fde3f7abef..a9f4e28cac 100644 --- a/vlib/cli/command_test.v +++ b/vlib/cli/command_test.v @@ -106,9 +106,7 @@ fn test_if_multiple_flags_get_set() { } fn flag_is_set_in_subcommand(cmd cli.Command) ? { - flag := cmd.flags.get_string('flag') or { - panic(err) - } + flag := cmd.flags.get_string('flag') or { panic(err) } assert flag == 'value' } diff --git a/vlib/cli/flag.v b/vlib/cli/flag.v index cff8e5e160..351fc0c8d5 100644 --- a/vlib/cli/flag.v +++ b/vlib/cli/flag.v @@ -37,9 +37,7 @@ pub fn (flags []Flag) get_bool(name string) ?bool { } pub fn (flags []Flag) get_bool_or(name string, or_value bool) bool { - value := flags.get_bool(name) or { - return or_value - } + value := flags.get_bool(name) or { return or_value } return value } @@ -56,9 +54,7 @@ pub fn (flags []Flag) get_int(name string) ?int { } pub fn (flags []Flag) get_int_or(name string, or_value int) int { - value := flags.get_int(name) or { - return or_value - } + value := flags.get_int(name) or { return or_value } return value } @@ -75,9 +71,7 @@ pub fn (flags []Flag) get_float(name string) ?f64 { } pub fn (flags []Flag) get_float_or(name string, or_value f64) f64 { - value := flags.get_float(name) or { - return or_value - } + value := flags.get_float(name) or { return or_value } return value } @@ -94,9 +88,7 @@ pub fn (flags []Flag) get_string(name string) ?string { } pub fn (flags []Flag) get_string_or(name string, or_value string) string { - value := flags.get_string(name) or { - return or_value - } + value := flags.get_string(name) or { return or_value } return value }