Skip to content

Commit

Permalink
Fix OptionLikeCommandAttribute
Browse files Browse the repository at this point in the history
  • Loading branch information
mayuki committed Jan 2, 2022
1 parent 0b5214a commit db62d61
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
6 changes: 2 additions & 4 deletions src/Cocona.Core/OptionLikeCommandAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using Cocona.Builder;
Expand All @@ -18,15 +19,12 @@ public class OptionLikeCommandAttribute : Attribute, IOptionLikeCommandMetadata
public Type CommandType { get; }
public string CommandMethodName { get; }

public MethodInfo? CommandMethod { get; }

public OptionLikeCommandAttribute(string optionName, char[] shortNames, Type commandType, string commandMethodName)
{
OptionName = optionName ?? throw new ArgumentNullException(nameof(optionName));
ShortNames = shortNames ?? throw new ArgumentNullException(nameof(shortNames));
CommandType = commandType ?? throw new ArgumentNullException(nameof(commandType));
CommandMethodName = commandMethodName ?? throw new ArgumentNullException(nameof(commandMethodName));
CommandMethod = null;
}

public ICommandData GetCommandData()
Expand All @@ -37,7 +35,7 @@ public ICommandData GetCommandData()
throw new InvalidOperationException($"A method of option-like command '{CommandMethodName}' was not found in '{CommandType}'");
}

return new DelegateCommandData(methodInfo, null, new [] { new CommandNameMetadata(OptionName) });
return new DelegateCommandData(methodInfo, null, new [] { new CommandNameMetadata(OptionName) }.Concat(methodInfo.GetCustomAttributes(inherit: true)).ToArray());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,19 @@ void TestMethod() { }
new CommandNameMetadata("TestMethod"),
new OptionLikeCommandMetadata(
new OptionLikeDelegateCommandData(new [] { 'i' }, new Action(TestMethod).Method, null, new object[] { new CommandNameMetadata("info"), })),
new OptionLikeCommandMetadata(
new OptionLikeDelegateCommandData(new [] { 'f' }, new Action(TestMethod).Method, null, new object[] { new CommandNameMetadata("foo"), new CommandDescriptionMetadata("Foo-Description") })),
new OptionLikeCommandMetadata(
new OptionLikeDelegateCommandData(new [] { 'f' }, new Action(TestMethod).Method, null, new object[] { new CommandNameMetadata("hidden"), new HiddenAttribute() })),
})
});
var collection = provider.GetCommandCollection();
collection.All[0].OptionLikeCommands.Should().HaveCount(1);
collection.All[0].OptionLikeCommands.Should().HaveCount(3);
collection.All[0].OptionLikeCommands[0].Name.Should().Be("info");
collection.All[0].OptionLikeCommands[1].Name.Should().Be("foo");
collection.All[0].OptionLikeCommands[1].Description.Should().Be("Foo-Description");
collection.All[0].OptionLikeCommands[2].Name.Should().Be("hidden");
collection.All[0].OptionLikeCommands[2].Flags.Should().HaveFlag(CommandOptionFlags.Hidden);
}

public static class CommandTest_Static
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,8 @@ public void Unnamed_OptionLikeCommand_Index()
x.Add("info", () => Console.WriteLine("Info"));
x.Add("foo", () => Console.WriteLine("Foo"))
.WithDescription("Foo-Description");
x.Add("hidden", () => Console.WriteLine("Hidden"))
.WithMetadata(new HiddenAttribute());
});

app.Run();
Expand All @@ -231,6 +233,7 @@ public void Unnamed_OptionLikeCommand_Index()
stdOut.Should().Contain("--info");
stdOut.Should().Contain("--foo");
stdOut.Should().Contain("Foo-Description");
stdOut.Should().NotContain("--hidden");
exitCode.Should().Be(129);
}
}
Expand Down

0 comments on commit db62d61

Please sign in to comment.