Skip to content

Commit e2ca7ac

Browse files
committed
* Revert my illadvised change to GetOrderedOptionAttributeNames, which needs to be able to distinguish null (no attribute) from empty array (attribute with no parameters)
* Remove the pre-C# 4.0 foreach loop fix
1 parent e657c9d commit e2ca7ac

File tree

1 file changed

+11
-17
lines changed

1 file changed

+11
-17
lines changed

Src/CommandLine.cs

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Diagnostics;
1+
using System.Diagnostics;
22
using System.Reflection;
33
using RT.Internal;
44
using RT.PostBuild;
@@ -219,10 +219,8 @@ private static object parseCommandLine(string[] args, Type type, int i, Func<Con
219219
FieldInfo swallowingField = null;
220220
var haveSeenOptionalPositional = false;
221221

222-
foreach (var fieldForeach in type.GetFields())
222+
foreach (var field in type.GetFields())
223223
{
224-
var field = fieldForeach; // This is necessary for the lambda expressions to work
225-
226224
if (field.IsDefined<IgnoreAttribute>())
227225
continue;
228226

@@ -279,7 +277,7 @@ private static object parseCommandLine(string[] args, Type type, int i, Func<Con
279277
var infos = option == null
280278
? field.FieldType.GetFields(BindingFlags.Static | BindingFlags.Public).Select(enumField => new
281279
{
282-
Options = enumField.GetOrderedOptionAttributeNames().ToArray(),
280+
Options = enumField.GetOrderedOptionAttributeNames(),
283281
NeedCommandName = false,
284282
GetEnumValue = Ut.Lambda((string commandName) => enumField.GetRawConstantValue())
285283
})
@@ -299,15 +297,13 @@ private static object parseCommandLine(string[] args, Type type, int i, Func<Con
299297
object prev = null;
300298
string prevOptionOrCommand = null;
301299

302-
foreach (var infForeach in infos)
300+
foreach (var inf in infos)
303301
{
304-
var inf = infForeach;
305302
if (inf.Options == null)
306303
// Assume that this is the default option
307304
continue;
308-
foreach (var oForeach in inf.Options)
305+
foreach (var o in inf.Options)
309306
{
310-
var o = oForeach;
311307
options[o] = () =>
312308
{
313309
i++;
@@ -356,7 +352,7 @@ private static object parseCommandLine(string[] args, Type type, int i, Func<Con
356352
// ### BOOL fields
357353
else if (field.FieldType == typeof(bool))
358354
{
359-
foreach (var o in field.GetOrderedOptionAttributeNames())
355+
foreach (var o in field.GetOrderedOptionAttributeNames() ?? [])
360356
options[o] = () => { field.SetValue(ret, true); i++; missingMandatories.Remove(field); };
361357
}
362358
// ### STRING and INTEGER fields (including nullable)
@@ -385,9 +381,8 @@ private static object parseCommandLine(string[] args, Type type, int i, Func<Con
385381
}
386382
else
387383
{
388-
foreach (var oForeach in field.GetOrderedOptionAttributeNames())
384+
foreach (var o in field.GetOrderedOptionAttributeNames() ?? [])
389385
{
390-
var o = oForeach;
391386
options[o] = () =>
392387
{
393388
i++;
@@ -430,9 +425,8 @@ private static object parseCommandLine(string[] args, Type type, int i, Func<Con
430425
else
431426
{
432427
string[] prev = null;
433-
foreach (var oForeach in field.GetOrderedOptionAttributeNames())
428+
foreach (var o in field.GetOrderedOptionAttributeNames() ?? [])
434429
{
435-
var o = oForeach;
436430
options[o] = () =>
437431
{
438432
i++;
@@ -810,7 +804,7 @@ private static void postBuildStep(IPostBuildReporter rep, Type commandLineType,
810804

811805
// Every field must have one of the following
812806
var positional = field.IsDefined<IsPositionalAttribute>();
813-
var options = field.GetOrderedOptionAttributeNames().ToArray();
807+
var options = field.GetOrderedOptionAttributeNames();
814808
var enumOpt = field.GetCustomAttributes<EnumOptionsAttribute>().FirstOrDefault();
815809

816810
if (!positional && options == null && enumOpt == null)
@@ -1538,10 +1532,10 @@ private static ConsoleColoredString getMessage(FieldInfo field, FieldInfo before
15381532

15391533
static class CmdLineExtensions
15401534
{
1541-
public static IEnumerable<string> GetOrderedOptionAttributeNames(this MemberInfo member)
1535+
public static string[] GetOrderedOptionAttributeNames(this MemberInfo member)
15421536
{
15431537
var attr = member.GetCustomAttributes<OptionAttribute>().FirstOrDefault();
1544-
return attr == null ? [] : attr.Names.OrderBy(compareOptionNames);
1538+
return attr == null ? null : attr.Names.OrderBy(compareOptionNames).ToArray();
15451539
}
15461540

15471541
private static int compareOptionNames(string opt1, string opt2)

0 commit comments

Comments
 (0)