using System.Collections.Generic; using Microsoft.CodeAnalysis; using System.Linq; using CliFx.Analyzers.Utils.Extensions; namespace CliFx.Analyzers.ObjectModel { internal partial class CommandOptionSymbol { public string? Name { get; } public char? ShortName { get; } public ITypeSymbol? ConverterType { get; } public IReadOnlyList ValidatorTypes { get; } public CommandOptionSymbol( string? name, char? shortName, ITypeSymbol? converterType, IReadOnlyList validatorTypes) { Name = name; ShortName = shortName; ConverterType = converterType; ValidatorTypes = validatorTypes; } } internal partial class CommandOptionSymbol { private static AttributeData? TryGetOptionAttribute(IPropertySymbol property) => property .GetAttributes() .FirstOrDefault(a => a.AttributeClass.DisplayNameMatches(SymbolNames.CliFxCommandOptionAttribute)); private static CommandOptionSymbol FromAttribute(AttributeData attribute) { var name = attribute .ConstructorArguments .Where(a => a.Type.DisplayNameMatches("string") || a.Type.DisplayNameMatches("System.String")) .Select(a => a.Value) .FirstOrDefault() as string; var shortName = attribute .ConstructorArguments .Where(a => a.Type.DisplayNameMatches("char") || a.Type.DisplayNameMatches("System.Char")) .Select(a => a.Value) .FirstOrDefault() as char?; var converter = attribute .NamedArguments .Where(a => a.Key == "Converter") .Select(a => a.Value.Value) .Cast() .FirstOrDefault(); var validators = attribute .NamedArguments .Where(a => a.Key == "Validators") .SelectMany(a => a.Value.Values) .Select(c => c.Value) .Cast() .ToArray(); return new CommandOptionSymbol(name, shortName, converter, validators); } public static CommandOptionSymbol? TryResolve(IPropertySymbol property) { var attribute = TryGetOptionAttribute(property); if (attribute is null) return null; return FromAttribute(attribute); } public static bool IsOptionProperty(IPropertySymbol property) => TryGetOptionAttribute(property) is not null; } }