/// <summary> /// Handler for single param arguments /// </summary> /// <param name="argument"><see cref="ArgumentData"/></param> /// <param name="value">Value of argument</param> /// <returns>True if it was successfully handled</returns> private bool HandleParam(ArgumentData argument, string value) { // Param is specified as key:value or key=value if (!string.IsNullOrWhiteSpace(value)) { return(Result.TrySetPropertyValue(argument.Property, value)); } // Value should be the next argumentKey if (++Position < Arguments.Length) { Tuple <string, string> nextParam = ArgumentParserUtils.ParseParam(Arguments[Position]); if (nextParam == null) { return(Result.TrySetPropertyValue(argument.Property, Arguments[Position])); } } Position--; LogHelper.LogError("Value for parameter '{0}' not specified", argument.Key); return(false); }
internal static ArgumentData Parse(PropertyInfo propertyInfo) { DetailsAttribute detailsAttr = propertyInfo.GetCustomAttribute <DetailsAttribute>(); OptionalAttribute optionalAttr = propertyInfo.GetCustomAttribute <OptionalAttribute>(); ParamAttribute paramAttr = propertyInfo.GetCustomAttribute <ParamAttribute>(); if (paramAttr == null) { return(null); } if (!IsTypeSupported(propertyInfo.PropertyType, paramAttr.Type)) { throw new InvalidOperationException( string.Format("Incompatible type specified for '{0}'", propertyInfo.Name)); } if (string.IsNullOrWhiteSpace(paramAttr.Key) && string.IsNullOrWhiteSpace(paramAttr.LongKey)) { throw new InvalidOperationException( string.Format("Both Key & LongKey cannot be null for property '{0}'.", propertyInfo.Name)); } string details = detailsAttr != null ? detailsAttr.Details : null; string defaultValue = optionalAttr != null ? optionalAttr.DefaultValue : null; ArgumentData data = new ArgumentData { DefaultValue = defaultValue, Details = details, Key = paramAttr.Key, LongKey = paramAttr.LongKey, Optional = optionalAttr != null, Property = propertyInfo, Type = paramAttr.Type, }; return(data); }
/// <summary> /// Mapping between argument type and it's default value handler /// </summary> /// <param name="argumentData">Argument data</param> /// <returns>True if it was successfully handled</returns> private bool HandleDefaultValue(ArgumentData argumentData) { string value = argumentData.DefaultValue; if (string.IsNullOrWhiteSpace(value)) { return(true); } switch (argumentData.Type) { case ArgumentType.Flag: case ArgumentType.Param: return(Result.TrySetPropertyValue(argumentData.Property, value)); case ArgumentType.ParamArray: string[] values = value.Split(SplitChars); return(Result.TrySetPropertyValue(argumentData.Property, values)); default: throw new NotImplementedException( string.Format("No handler found for ArgumentType '{0}'", argumentData.Type)); } }
internal static ArgumentData Parse(PropertyInfo propertyInfo) { DetailsAttribute detailsAttr = propertyInfo.GetCustomAttribute<DetailsAttribute>(); OptionalAttribute optionalAttr = propertyInfo.GetCustomAttribute<OptionalAttribute>(); ParamAttribute paramAttr = propertyInfo.GetCustomAttribute<ParamAttribute>(); if (paramAttr == null) { return null; } if (!IsTypeSupported(propertyInfo.PropertyType, paramAttr.Type)) { throw new InvalidOperationException( string.Format("Incompatible type specified for '{0}'", propertyInfo.Name)); } if (string.IsNullOrWhiteSpace(paramAttr.Key) && string.IsNullOrWhiteSpace(paramAttr.LongKey)) { throw new InvalidOperationException( string.Format("Both Key & LongKey cannot be null for property '{0}'.", propertyInfo.Name)); } string details = detailsAttr != null ? detailsAttr.Details : null; string defaultValue = optionalAttr != null ? optionalAttr.DefaultValue : null; ArgumentData data = new ArgumentData { DefaultValue = defaultValue, Details = details, Key = paramAttr.Key, LongKey = paramAttr.LongKey, Optional = optionalAttr != null, Property = propertyInfo, Type = paramAttr.Type, }; return data; }
/// <summary> /// Adds a key & value to dictionary if key isn't null /// </summary> /// <param name="dictionary">Dictionary to add to</param> /// <param name="key">Key</param> /// <param name="data">Value</param> /// <returns>True if the key was added, else false</returns> private static bool SafeAdd(this IDictionary <string, ArgumentData> dictionary, string key, ArgumentData data) { if (string.IsNullOrWhiteSpace(key)) { return(false); } if (dictionary.ContainsKey(key)) { throw new InvalidOperationException( string.Format( "Multiple params cannot have the same key: {0}", key)); } dictionary.Add(key, data); return(true); }