From 15bf8222b9f9203113edaced35324e3c70ef6e17 Mon Sep 17 00:00:00 2001 From: yuyi Date: Wed, 18 Sep 2024 01:07:23 +0800 Subject: [PATCH] flag: fix parse_bool_value() with different order short args (fix #22176) (#22242) --- vlib/flag/flag.v | 2 +- vlib/flag/flag_test.v | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/vlib/flag/flag.v b/vlib/flag/flag.v index 61194a8e9dbe72..0f445f6c6d4826 100644 --- a/vlib/flag/flag.v +++ b/vlib/flag/flag.v @@ -304,7 +304,7 @@ fn (mut fs FlagParser) parse_bool_value(longhand string, shorthand u8) !string { } if arg.len > 1 && arg[0] == `-` && arg[1] != `-` { mut found := false - for j in 1 .. arg.len - 1 { + for j in 1 .. arg.len { if arg[j].is_space() { break } else if arg[j] == shorthand { diff --git a/vlib/flag/flag_test.v b/vlib/flag/flag_test.v index 6be3840a2a389e..6329bdac912f13 100644 --- a/vlib/flag/flag_test.v +++ b/vlib/flag/flag_test.v @@ -501,3 +501,16 @@ fn test_finalize_with_multi_shortargs() { println(additional_args.join_lines()) assert additional_args == [] } + +fn test_finalize_with_multi_shortargs_different_order() { + mut fp := flag.new_flag_parser(['-ba', '-c']) + a_bool := fp.bool('a_bool', `a`, false, '') + assert a_bool + b_bool := fp.bool('b_bool', `b`, false, '') + assert b_bool + c_bool := fp.bool('c_bool', `c`, false, '') + assert c_bool + additional_args := fp.finalize()! + println(additional_args.join_lines()) + assert additional_args == [] +}