gsscoder / commandline

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

"help" crashes when mixing UsageAttribute with other Attributes #443

Open ghost opened 7 years ago

ghost commented 7 years ago

When using the "Usage" attribute together with a 2nd one (e.g. ExcludeFromCoverage) like this:

[Usage]
[ExcludeFromCodeCoverage]
public static IEnumerable<Example> Examples ...

In this situation displaying "help" will cause an exception. The cause for that is likely fixed by this change:

src/CommandLine/Core/ReflectionExtensions.cs: L45

    public static Maybe<Tuple<PropertyInfo, UsageAttribute>> GetUsageData(this Type type)
    {
        return
            (from pi in type.FlattenHierarchy().SelectMany(x => x.GetTypeInfo().GetProperties())
                let attrs = pi.GetCustomAttributes(true)
                where attrs.OfType<UsageAttribute>().Any()
                select Tuple.Create(pi, (UsageAttribute)attrs.OfType<UsageAttribute>().First()))
                    .SingleOrDefault()
                    .ToMaybe();
    }