diff --git a/Src/CmdLineExtensions.cs b/Src/CmdLineExtensions.cs index 3470f9c..63f688e 100644 --- a/Src/CmdLineExtensions.cs +++ b/Src/CmdLineExtensions.cs @@ -71,5 +71,6 @@ public static ConsoleColoredString FormatParameterUsage(this FieldInfo field, bo public static IEnumerable GetCommandLineFields(this Type type) => type.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public) .Where(f => f.IsDefined() || f.IsDefined() || f.IsDefined()) - .OrderBy(f => f.DeclaringType.SelectChain(t => t.BaseType).Count()); + .OrderBy(f => f.GetCustomAttribute()?.Order) + .ThenBy(f => f.DeclaringType.SelectChain(t => t.BaseType).Count()); } diff --git a/Src/CommandLineParser.cs b/Src/CommandLineParser.cs index 3ab7c21..541a14c 100644 --- a/Src/CommandLineParser.cs +++ b/Src/CommandLineParser.cs @@ -476,7 +476,6 @@ private static object parseCommandLine(string[] args, Type type, int i, Func p.Order).ToList(); // Don’t use List.Sort because it’s not a stable sort for (var pIx = 0; pIx < positionals.Count - 1; pIx++) if (positionals[pIx + 1].IsMandatory && !positionals[pIx].IsMandatory) throw new InvalidOrderOfPositionalParametersException(positionals[pIx].Field, positionals[pIx + 1].Field, getHelpGenerator(type, helpProcessor)); @@ -770,10 +769,6 @@ private static void getFieldsForHelp(Type type, out List optionalOpti : (field.IsDefined() ? optionalPositional : optionalOptions); fieldInfos.Add(field); } - - // Don’t use List.Sort because it’s not a stable sort - mandatoryPositional = mandatoryPositional.OrderBy(f => f.GetCustomAttribute().Order).ToList(); - optionalPositional = optionalPositional.OrderBy(f => f.GetCustomAttribute().Order).ToList(); } private static ConsoleColoredString getDocumentation(MemberInfo member, Func helpProcessor) =>