Skip to content

Commit

Permalink
Added support for [SubCommand] attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
Bilal Fazlani committed Jan 10, 2018
1 parent e730053 commit 1b9dd57
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
25 changes: 14 additions & 11 deletions CommandDotNet.Tests/NestedCommandsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,22 @@ public int Commit([Option(ShortName = "m")]string message)
return 5;
}

public class Submodule
[SubCommand]
public Submodule Submodule { get; set; }
}

public class Submodule
{
public int Add(string url)
{
public int Add(string url)
{
Console.WriteLine($"Submodule {url} added");
return 6;
}
Console.WriteLine($"Submodule {url} added");
return 6;
}

public int Remove(string path)
{
Console.WriteLine($"Submodule {path} removed");
return 7;
}
public int Remove(string path)
{
Console.WriteLine($"Submodule {path} removed");
return 7;
}
}
}
10 changes: 10 additions & 0 deletions CommandDotNet/Attributes/SubCommandAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;

namespace CommandDotNet.Attributes
{
[AttributeUsage(AttributeTargets.Property)]
public class SubCommandAttribute : Attribute
{

}
}
10 changes: 8 additions & 2 deletions CommandDotNet/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,16 @@ public static void CreateSubApplications(this Type type,
CommandLineApplication parentApplication,
IDependencyResolver dependencyResolver)
{
IEnumerable<Type> propertySubmodules = type
.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly)
.Where(p => !p.PropertyType.IsValueType)
.Where(p => p.GetCustomAttribute<SubCommandAttribute>() != null)
.Select(p => p.PropertyType);

IEnumerable<Type> inlineClassSubmodules = type
.GetNestedTypes(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
.GetNestedTypes(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly);

foreach (Type submoduleType in inlineClassSubmodules)
foreach (Type submoduleType in propertySubmodules.Union(inlineClassSubmodules))
{
AppCreator appCreator = new AppCreator(settings);
appCreator.CreateApplication(submoduleType, dependencyResolver, parentApplication);
Expand Down

0 comments on commit 1b9dd57

Please sign in to comment.