Skip to content

Commit 465963a

Browse files
Simplify OptionParser#parse_arg_to_flag_and_value (#16300)
Since this is a separate helper method now, we can greatly simplify control flow with early returns.
1 parent 67e3ad9 commit 465963a

File tree

1 file changed

+6
-19
lines changed

1 file changed

+6
-19
lines changed

src/option_parser.cr

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -447,27 +447,14 @@ class OptionParser
447447
# Parses a command-line argument into a flag and optional inline value.
448448
private def parse_arg_to_flag_and_value(arg : String) : {String, String?}
449449
if arg.starts_with?("--")
450-
value_index = arg.index('=')
451-
if value_index
452-
flag = arg[0...value_index]
453-
value = arg[value_index + 1..-1]
454-
else
455-
flag = arg
456-
value = nil
450+
name, separator, value = arg.partition("=")
451+
if separator == "="
452+
return {name, value}
457453
end
458-
elsif arg.starts_with?('-')
459-
if arg.size > 2
460-
flag = arg[0..1]
461-
value = arg[2..-1]
462-
else
463-
flag = arg
464-
value = nil
465-
end
466-
else
467-
flag = arg
468-
value = nil
454+
elsif arg.starts_with?('-') && arg.size > 2
455+
return {arg[0..1], arg[2..]}
469456
end
470-
{flag, value}
457+
{arg, nil}
471458
end
472459

473460
# Processes a single flag/subcommand. Matches original behaviour exactly.

0 commit comments

Comments
 (0)