public IArgument GetArgument(string propertyName) { PropertyInfo pi = _t.GetProperty(propertyName); if (pi == null) { throw new Exception($"{_t} has no property {propertyName}"); } ArgumentAttribute aa = pi.GetCustomAttribute <ArgumentAttribute>( ); FirstArgumentAttribute fa = pi.GetCustomAttribute <FirstArgumentAttribute>( ); OptionAttribute oa = pi.GetCustomAttribute <OptionAttribute>( ); string argName; if (aa != null) { argName = !string.IsNullOrEmpty(aa.Name) ? aa.Name : string.IsNullOrEmpty(aa.Of) ? propertyName : propertyName.Replace(aa.Of, string.Empty); } else if (fa != null) { argName = !string.IsNullOrEmpty(fa.Name) ? fa.Name : $"{propertyName}{CliSpecDeriver.ImplicitFirstArg}"; } else if (oa != null) { argName = $"{propertyName}{CliSpecDeriver.ImplicitFirstArg}"; } else { throw new Exception($"no argument mapped to property {propertyName}"); } IArgument arg = _spec.Arguments.Union( _spec.Options.SelectMany(o => o.Arguments)) .Union( _spec.Flags.SelectMany(f => f.Arguments)) .FirstOrDefault(a => a.Name == argName); if (arg == null) { throw new Exception($"no argument mapped to property {propertyName}"); } return(arg); }
private IArgument GetDynamicArgument(CliSpecification spec, Type t, string parent) { foreach (PropertyInfo pi in t.GetProperties( )) { ArgumentAttribute argAttrib = pi.GetCustomAttribute <ArgumentAttribute>( ); FirstArgumentAttribute firstArgAttrib = pi.GetCustomAttribute <FirstArgumentAttribute>( ); if (argAttrib == null && firstArgAttrib == null) { continue; } if (argAttrib != null && (string.Compare(parent, argAttrib.Of, StringComparison.InvariantCulture) != 0 || t.Name.Contains(parent))) { continue; } if (firstArgAttrib != null && string.Compare(parent, pi.Name, StringComparison.InvariantCulture) != 0) { continue; } if (!argAttrib?.Dynamic ?? !firstArgAttrib.Dynamic) { continue; } if (argAttrib != null) { return(_argTypeMapper.Map( argAttrib, pi, _config.CliConfig, parent)); } return(_argTypeMapper.Map( firstArgAttrib, $"{pi.Name}{ImplicitFirstArg}", pi.PropertyType, spec.Config)); } return(null); }
public IArgument Map(FirstArgumentAttribute argAttrib, string name, Type t, ICliConfig config) { if (argAttrib?.Dynamic ?? false) { if (!typeof(IEnumerable).IsAssignableFrom(t)) { throw new ArgumentException( $"dynamic property type must be assignable to {typeof( IEnumerable<> )} but was {t} for {name}"); } t = t.GenericTypeArguments[0]; } Func <ICliConfig, string, bool, Argument> map = GetMap(t); Argument arg = map( config, argAttrib?.Name ?? name, true); arg.Description = argAttrib?.Description; return(arg); }