Skip to content

Commit

Permalink
Fix IsPositional ordering properly
Browse files Browse the repository at this point in the history
  • Loading branch information
Timwi committed Oct 12, 2024
1 parent c0333af commit 948d659
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 6 deletions.
3 changes: 2 additions & 1 deletion Src/CmdLineExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,6 @@ public static ConsoleColoredString FormatParameterUsage(this FieldInfo field, bo
public static IEnumerable<FieldInfo> GetCommandLineFields(this Type type) =>
type.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)
.Where(f => f.IsDefined<OptionAttribute>() || f.IsDefined<EnumOptionsAttribute>() || f.IsDefined<IsPositionalAttribute>())
.OrderBy(f => f.DeclaringType.SelectChain(t => t.BaseType).Count());
.OrderBy(f => f.GetCustomAttribute<IsPositionalAttribute>()?.Order)
.ThenBy(f => f.DeclaringType.SelectChain(t => t.BaseType).Count());
}
5 changes: 0 additions & 5 deletions Src/CommandLineParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,6 @@ private static object parseCommandLine(string[] args, Type type, int i, Func<Con
throw new UnsupportedTypeException($"{type.Name}.{field.Name}", getHelpGenerator(type, helpProcessor));
}

positionals = positionals.OrderBy(p => p.Order).ToList(); // Don’t use List<T>.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));
Expand Down Expand Up @@ -770,10 +769,6 @@ private static void getFieldsForHelp(Type type, out List<FieldInfo> optionalOpti
: (field.IsDefined<IsPositionalAttribute>() ? optionalPositional : optionalOptions);
fieldInfos.Add(field);
}

// Don’t use List<T>.Sort because it’s not a stable sort
mandatoryPositional = mandatoryPositional.OrderBy(f => f.GetCustomAttribute<IsPositionalAttribute>().Order).ToList();
optionalPositional = optionalPositional.OrderBy(f => f.GetCustomAttribute<IsPositionalAttribute>().Order).ToList();
}

private static ConsoleColoredString getDocumentation(MemberInfo member, Func<ConsoleColoredString, ConsoleColoredString> helpProcessor) =>
Expand Down

0 comments on commit 948d659

Please sign in to comment.