Skip to content

Commit

Permalink
Merge pull request #81 from moreal/optionlikecommand-desc
Browse files Browse the repository at this point in the history
Support `OptionLikeCommand`'s description
  • Loading branch information
mayuki authored Dec 8, 2022
2 parents 1829b2c + ee2109f commit a2583c0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
6 changes: 6 additions & 0 deletions samples/Advanced.OptionLikeCommand/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -20,5 +21,10 @@ private void Hello([Argument]string name)
{
Console.WriteLine($"Hello {name}!");
}

private void Bye([Argument]string name)
{
Console.WriteLine($"Bye {name}!");
}
}
}
8 changes: 6 additions & 2 deletions src/Cocona.Core/OptionLikeCommandAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ public class OptionLikeCommandAttribute : Attribute, IOptionLikeCommandMetadata
public IReadOnlyList<char> 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()
Expand All @@ -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());
}
}
}

0 comments on commit a2583c0

Please sign in to comment.