Frank Ray 714cf179cb
Command line improvements (#1103)
Closes #187
Closes #203
Closes #1059
2023-04-02 22:43:21 +02:00

66 lines
2.1 KiB
C#

namespace Spectre.Console.Cli;
internal sealed class CommandInfo : ICommandContainer
{
public string Name { get; }
public HashSet<string> Aliases { get; }
public string? Description { get; }
public object? Data { get; }
public Type? CommandType { get; }
public Type SettingsType { get; }
public Func<CommandContext, CommandSettings, int>? Delegate { get; }
public bool IsDefaultCommand { get; }
public CommandInfo? Parent { get; }
public IList<CommandInfo> Children { get; }
public IList<CommandParameter> Parameters { get; }
public IList<string[]> Examples { get; }
public bool IsBranch => CommandType == null && Delegate == null;
IList<CommandInfo> ICommandContainer.Commands => Children;
// only branches can have a default command
public CommandInfo? DefaultCommand => IsBranch ? Children.FirstOrDefault(c => c.IsDefaultCommand) : null;
public bool IsHidden { get; }
public CommandInfo(CommandInfo? parent, ConfiguredCommand prototype)
{
Parent = parent;
Name = prototype.Name;
Aliases = new HashSet<string>(prototype.Aliases);
Description = prototype.Description;
Data = prototype.Data;
CommandType = prototype.CommandType;
SettingsType = prototype.SettingsType;
Delegate = prototype.Delegate;
IsDefaultCommand = prototype.IsDefaultCommand;
IsHidden = prototype.IsHidden;
Children = new List<CommandInfo>();
Parameters = new List<CommandParameter>();
Examples = prototype.Examples ?? new List<string[]>();
if (CommandType != null && string.IsNullOrWhiteSpace(Description))
{
var description = CommandType.GetCustomAttribute<DescriptionAttribute>();
if (description != null)
{
Description = description.Description;
}
}
}
public List<CommandInfo> Flatten()
{
var result = new Stack<CommandInfo>();
var current = this;
while (current != null)
{
result.Push(current);
current = current.Parent;
}
return result.ToList();
}
}