Add command description

This commit is contained in:
Alexey Golub
2019-06-15 21:26:56 +03:00
parent 1543076bf4
commit 7a5a32d27b
6 changed files with 31 additions and 14 deletions

View File

@@ -7,10 +7,10 @@ using CliFx.Models;
namespace CliFx.Tests.Dummy.Commands
{
[Command("add")]
[Command("add", Description = "Calculate the sum of all input values.")]
public class AddCommand : Command
{
[CommandOption("values", 'v', IsRequired = true, Description = "Values.")]
[CommandOption("values", 'v', IsRequired = true, Description = "Input values.")]
public IReadOnlyList<double> Values { get; set; }
public override ExitCode Execute()

View File

@@ -2,10 +2,11 @@
using System.Globalization;
using CliFx.Attributes;
using CliFx.Models;
using CliFx.Services;
namespace CliFx.Tests.Dummy.Commands
{
[Command("log")]
[Command("log", Description = "Calculate the logarithm of a value.")]
public class LogCommand : Command
{
[CommandOption("value", 'v', IsRequired = true, Description = "Value whose logarithm is to be found.")]

View File

@@ -7,6 +7,8 @@ namespace CliFx.Attributes
{
public string Name { get; }
public string Description { get; set; }
public CommandAttribute(string name)
{
Name = name;

View File

@@ -3,7 +3,11 @@
namespace CliFx.Attributes
{
[AttributeUsage(AttributeTargets.Class, Inherited = false)]
public class DefaultCommandAttribute : Attribute
public class DefaultCommandAttribute : CommandAttribute
{
public DefaultCommandAttribute()
: base(null)
{
}
}
}

View File

@@ -14,17 +14,19 @@ namespace CliFx.Internal
public bool IsDefault { get; }
public CommandType(Type type, string name, bool isDefault)
public string Description { get; }
public IReadOnlyList<CommandOptionProperty> Options { get; }
public CommandType(Type type, string name, bool isDefault, string description, IReadOnlyList<CommandOptionProperty> options)
{
_type = type;
Name = name;
IsDefault = isDefault;
Description = description;
Options = options;
}
public IEnumerable<CommandOptionProperty> GetOptionProperties() => _type.GetProperties()
.Where(CommandOptionProperty.IsValid)
.Select(CommandOptionProperty.Initialize);
public Command Activate() => (Command) Activator.CreateInstance(_type);
}
@@ -41,10 +43,18 @@ namespace CliFx.Internal
if (!IsValid(type))
throw new InvalidOperationException($"[{type.Name}] is not a valid command type.");
var name = type.GetCustomAttribute<CommandAttribute>()?.Name;
var isDefault = type.IsDefined(typeof(DefaultCommandAttribute));
var attribute = type.GetCustomAttribute<CommandAttribute>();
return new CommandType(type, name, isDefault);
var name = attribute.Name;
var isDefault = attribute is DefaultCommandAttribute;
var description = attribute.Description;
var options = type.GetProperties()
.Where(CommandOptionProperty.IsValid)
.Select(CommandOptionProperty.Initialize)
.ToArray();
return new CommandType(type, name, isDefault, description, options);
}
public static IEnumerable<CommandType> GetCommandTypes(IEnumerable<Type> types) => types.Where(IsValid).Select(Initialize);

View File

@@ -84,7 +84,7 @@ namespace CliFx.Services
var command = commandType.Activate();
// Set command options
foreach (var property in commandType.GetOptionProperties())
foreach (var property in commandType.Options)
{
// Get option for this property
var option = optionSet.GetOptionOrDefault(property.Name, property.ShortName);
@@ -95,7 +95,7 @@ namespace CliFx.Services
var convertedValue = _commandOptionConverter.ConvertOption(option, property.Type);
property.SetValue(command, convertedValue);
}
// If the property is missing but it's required - throw
// If the property is required but it's missing - throw
else if (property.IsRequired)
{
throw new CommandResolveException($"Can't resolve command because required property [{property.Name}] is not set.");