Skip to content

Commit

Permalink
fix(commandlinehelp): Fix loading of commands and parameters when the…
Browse files Browse the repository at this point in the history
… option class is a nested type

Load types recursively to discover all types annotated with [Verb] and/or [Option] attributes instead of just loading top-level types.
  • Loading branch information
ap0llo committed Jun 17, 2020
1 parent d7e51fe commit dad143b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,38 @@ public class MyOptionClass
Assert.IsType<MultiCommandApplicationDocumentation>(application);
}

[Theory]
[CombinatorialData]
public void Option_classes_can_be_inner_classes(ApplicationType applicationType)
{
// ARRANGE
using var assembly = Compile($@"
using System;
using CommandLine;
public class OuterClass
{{
{GetClassAttributes(applicationType)}
public class InnerClass
{{
[Option(""parameter1"")]
public string Option1 {{ get; set; }}
}}
}}
");

var sut = new CommandLineParserLoader(m_Logger);

// ACT
var application = sut.Load(assembly);

// ASSERT
Assert.NotNull(application);
var parameterCollection = GetParameterCollection(application, applicationType);
var parameter = Assert.Single(parameterCollection.NamedParameters);
Assert.Equal("parameter1", parameter.Name);
}

[Fact]
public void Application_name_is_loaded_from_AssemblyTitleAttribute_if_it_exists()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Grynwald.MdDocs.Common;
using Microsoft.Extensions.Logging;
using Mono.Cecil;
using Mono.Cecil.Rocks;

namespace Grynwald.MdDocs.CommandLineHelp.Loaders.CommandLineParser
{
Expand Down Expand Up @@ -33,7 +34,7 @@ public CommandLineParserLoader(ILogger logger)

public ApplicationDocumentation Load(AssemblyDefinition assembly)
{
var types = assembly.MainModule.Types.Where(x => !x.IsAbstract);
var types = assembly.MainModule.GetAllTypes().Where(x => !x.IsAbstract);

ApplicationDocumentation applicationDocumentation;
if (types.Any(x => x.HasAttribute(CommandLineParserTypeNames.VerbAttributeFullName)))
Expand Down Expand Up @@ -76,7 +77,8 @@ bool IsCommandLineParameter(PropertyDefinition property)
}

// get all types with at least one property attributed as either [Option] or [Value]
var optionTypes = assembly.MainModule.Types
var optionTypes = assembly.MainModule
.GetAllTypes()
.Where(x => !x.IsAbstract)
.Where(type => type.Properties.Any(IsCommandLineParameter))
.ToArray();
Expand Down Expand Up @@ -131,7 +133,8 @@ private string GetApplicationName(AssemblyDefinition assembly)

private void LoadCommands(MultiCommandApplicationDocumentation applicationDocumentation, AssemblyDefinition assembly)
{
var commandTypes = assembly.MainModule.Types
var commandTypes = assembly.MainModule
.GetAllTypes()
.Where(x => !x.IsAbstract)
.WithAttribute(CommandLineParserTypeNames.VerbAttributeFullName)
.Where(x => !x.GetAttribute(CommandLineParserTypeNames.VerbAttributeFullName).GetPropertyValueOrDefault<bool>(s_Hidden));
Expand Down

0 comments on commit dad143b

Please sign in to comment.