gsscoder / commandline

Terse syntax C# command line parser for .NET with F# support
1.63k stars 293 forks source link

Inverse order of FlatternHierarchy #506

Closed rlsf closed 6 years ago

rlsf commented 6 years ago

Hello, The current implementation of FlatternHierarchy function returns the current type first. This in turn causes the help routine to print the options/values attributes of the current type before inherited types. From my point of view, however, the current type should be returned last, so common options/values which are declared in the inherited class will appear first in the help message.

example:

public class CliBase
{
  [Option("opt1")]
  public string Opt1 { get; set; }
}

public class CliCommand : CliBase
{
  [Option("opt2")]
  public string Opt2 { get; set; }
}

printing the help information for CliCommand, will result in opt2 being printed before opt1, while i expect the reverse order, so any new command inheriting from CliBase will have opt1 printed first.

the actual code change is in FlattenHierarchy method, instead of current implementation, use this one:

private static IEnumerable<Type> FlattenHierarchy(this Type type)
{
    if (type == null)
    {
        yield break;
    }

    foreach (var @interface in type.SafeGetInterfaces())
    {
        yield return @interface;
    }
    foreach (var @interface in FlattenHierarchy(type.BaseType))
    {
        yield return @interface;
    }
    yield return type;
}
nemec commented 6 years ago

501