From acea63a9c1e845f66253a79835b5fb10086910a1 Mon Sep 17 00:00:00 2001 From: Egor Bogatov Date: Wed, 17 Jan 2018 17:30:14 +0300 Subject: [PATCH] Improve GetArgumentName in MonoOptions. Fixes bug-60904 (#6506) * Rewrite Options.GetArgumentName to use Regex * code cleanup * code cleanup --- mcs/class/Mono.Options/Mono.Options/Options.cs | 41 +++++++++++----------- .../Test/Mono.Options/OptionSetTest.cs | 4 +++ 2 files changed, 24 insertions(+), 21 deletions(-) diff --git a/mcs/class/Mono.Options/Mono.Options/Options.cs b/mcs/class/Mono.Options/Mono.Options/Options.cs index a68c1608b7c..af7eea850c4 100644 --- a/mcs/class/Mono.Options/Mono.Options/Options.cs +++ b/mcs/class/Mono.Options/Mono.Options/Options.cs @@ -1390,28 +1390,27 @@ namespace Mono.Options o.Write (s); } - private static string GetArgumentName (int index, int maxIndex, string description) - { - if (description == null) - return maxIndex == 1 ? "VALUE" : "VALUE" + (index + 1); - string[] nameStart; - if (maxIndex == 1) - nameStart = new string[]{"{0:", "{"}; - else - nameStart = new string[]{"{" + index + ":"}; - for (int i = 0; i < nameStart.Length; ++i) { - int start, j = 0; - do { - start = description.IndexOf (nameStart [i], j); - } while (start >= 0 && j != 0 ? description [j++ - 1] == '{' : false); - if (start == -1) - continue; - int end = description.IndexOf ("}", start); - if (end == -1) - continue; - return description.Substring (start + nameStart [i].Length, end - start - nameStart [i].Length); + static string GetArgumentName (int index, int maxIndex, string description) + { + var matches = Regex.Matches (description ?? "", @"(?<=(? 1 + if (maxIndex > 1 && parts.Length == 2 && + parts[0] == index.ToString (CultureInfo.InvariantCulture)) { + argName = parts[1]; + } + } + + if (string.IsNullOrEmpty (argName)) { + argName = maxIndex == 1 ? "VALUE" : "VALUE" + (index + 1); } - return maxIndex == 1 ? "VALUE" : "VALUE" + (index + 1); + return argName; } private static string GetDescription (string description) diff --git a/mcs/class/Mono.Options/Test/Mono.Options/OptionSetTest.cs b/mcs/class/Mono.Options/Test/Mono.Options/OptionSetTest.cs index 790d55dea2d..1b8ca0a5885 100644 --- a/mcs/class/Mono.Options/Test/Mono.Options/OptionSetTest.cs +++ b/mcs/class/Mono.Options/Test/Mono.Options/OptionSetTest.cs @@ -413,6 +413,8 @@ namespace MonoTests.Mono.Options { "color2:", "set {color}", v => {} }, { "rk=", "required key/value option", (k, v) => {} }, { "rk2=", "required {{foo}} {0:key}/{1:value} option", (k, v) => {} }, + { "rk3=", "required {{foo}} {}", k => {} }, + { "rk4=", "required {{foo}} {0:val}", k => {} }, { "ok:", "optional key/value option", (k, v) => {} }, { "long-desc", "This has a really\nlong, multi-line description that also\ntests\n" + @@ -456,6 +458,8 @@ namespace MonoTests.Mono.Options expected.WriteLine (" --color2[=color] set color"); expected.WriteLine (" --rk=VALUE1:VALUE2 required key/value option"); expected.WriteLine (" --rk2=key:value required {foo} key/value option"); + expected.WriteLine (" --rk3=VALUE required {foo}"); + expected.WriteLine (" --rk4=val required {foo} val"); expected.WriteLine (" --ok[=VALUE1:VALUE2] optional key/value option"); expected.WriteLine (" --long-desc This has a really"); expected.WriteLine (" long, multi-line description that also"); -- 2.11.4.GIT