From 3ee218b4025d1de49c8e85fe0e7417d983435834 Mon Sep 17 00:00:00 2001 From: Moreal Date: Mon, 21 Nov 2022 00:10:58 +0900 Subject: [PATCH] Support `OptionLikeCommand`'s description --- samples/Advanced.OptionLikeCommand/Program.cs | 6 ++++++ src/Cocona.Core/OptionLikeCommandAttribute.cs | 8 ++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/samples/Advanced.OptionLikeCommand/Program.cs b/samples/Advanced.OptionLikeCommand/Program.cs index 4dcd3a7..cd04b2c 100644 --- a/samples/Advanced.OptionLikeCommand/Program.cs +++ b/samples/Advanced.OptionLikeCommand/Program.cs @@ -11,6 +11,7 @@ static void Main(string[] args) } [OptionLikeCommand("hello", new[] {'f'}, typeof(Program), nameof(Hello))] + [OptionLikeCommand("bye", new[] {'b'}, typeof(Program), nameof(Bye), "Show bye message")] public void Execute() { Console.WriteLine("Execute"); @@ -20,5 +21,10 @@ private void Hello([Argument]string name) { Console.WriteLine($"Hello {name}!"); } + + private void Bye([Argument]string name) + { + Console.WriteLine($"Bye {name}!"); + } } } diff --git a/src/Cocona.Core/OptionLikeCommandAttribute.cs b/src/Cocona.Core/OptionLikeCommandAttribute.cs index 061c6db..000acf7 100644 --- a/src/Cocona.Core/OptionLikeCommandAttribute.cs +++ b/src/Cocona.Core/OptionLikeCommandAttribute.cs @@ -19,13 +19,15 @@ public class OptionLikeCommandAttribute : Attribute, IOptionLikeCommandMetadata public IReadOnlyList ShortNames { get; } public Type CommandType { get; } public string CommandMethodName { get; } + public string? Description { get; } - public OptionLikeCommandAttribute(string optionName, char[] shortNames, Type commandType, string commandMethodName) + public OptionLikeCommandAttribute(string optionName, char[] shortNames, Type commandType, string commandMethodName, string? description = null) { 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)); + Description = description; } public ICommandData GetCommandData() @@ -36,7 +38,9 @@ public ICommandData GetCommandData() throw new InvalidOperationException(string.Format(Strings.OptionLikeCommand_MethodNotFound, CommandMethodName, CommandType)); } - return new DelegateCommandData(methodInfo, null, new [] { new CommandNameMetadata(OptionName) }.Concat(methodInfo.GetCustomAttributes(inherit: true)).ToArray()); + return new DelegateCommandData(methodInfo, null, new [] { new CommandNameMetadata(OptionName) } + .Concat(Description is { } description ? new object[] { new CommandDescriptionMetadata(description) } : new object[0]) + .Concat(methodInfo.GetCustomAttributes(inherit: true)).ToArray()); } } }