diff --git a/CommandDotNet.Tests/NestedCommandsTest.cs b/CommandDotNet.Tests/NestedCommandsTest.cs index 5809493ae..4b7c14aa6 100644 --- a/CommandDotNet.Tests/NestedCommandsTest.cs +++ b/CommandDotNet.Tests/NestedCommandsTest.cs @@ -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; } } } \ No newline at end of file diff --git a/CommandDotNet/Attributes/SubCommandAttribute.cs b/CommandDotNet/Attributes/SubCommandAttribute.cs new file mode 100644 index 000000000..2f4ff3959 --- /dev/null +++ b/CommandDotNet/Attributes/SubCommandAttribute.cs @@ -0,0 +1,10 @@ +using System; + +namespace CommandDotNet.Attributes +{ + [AttributeUsage(AttributeTargets.Property)] + public class SubCommandAttribute : Attribute + { + + } +} \ No newline at end of file diff --git a/CommandDotNet/Extensions.cs b/CommandDotNet/Extensions.cs index f331b6015..6a5870854 100644 --- a/CommandDotNet/Extensions.cs +++ b/CommandDotNet/Extensions.cs @@ -24,10 +24,16 @@ public static void CreateSubApplications(this Type type, CommandLineApplication parentApplication, IDependencyResolver dependencyResolver) { + IEnumerable propertySubmodules = type + .GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly) + .Where(p => !p.PropertyType.IsValueType) + .Where(p => p.GetCustomAttribute() != null) + .Select(p => p.PropertyType); + IEnumerable 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);