示例#1
0
        /// <summary>
        /// Tries to apply a default value to its related property.
        /// </summary>
        /// <remarks>
        /// This method tries to apply a default value to its related property. For
        /// the moment, only optional parameters support a usage of default values.
        /// </remarks>
        /// <param name="setting">
        /// An instance of class <see cref="ArgumentProcessorSetting"/> to get the
        /// default value from to be applied.
        /// </param>
        /// <returns>
        /// The same instance of class <see cref="ArgumentProcessorSetting"/> that
        /// has been provided as parameter.
        /// </returns>
        /// <exception cref="DefaultValueException">
        /// This exception is thrown in all cases of applying a default value fails.
        /// </exception>
        private ArgumentProcessorSetting ApplyDefaultValue(ArgumentProcessorSetting setting)
        {
            if (!(setting.Attribute is OptionParameterAttribute attribute))
            {
                return(setting);
            }

            if (!attribute.HasDefaultValue)
            {
                return(setting);
            }

            try
            {
                setting.Property.SetValue(this.Instance, OptionTypeConverter.Convert(attribute.DefaultValue?.ToString(), setting.Property.PropertyType));
            }
            catch (Exception exception)
            {
                throw new DefaultValueException(
                          $"Could not apply default value of \"{(attribute.DefaultValue is null ? "null" : attribute.DefaultValue.ToString())}\" " +
                          $"to property \"{setting.Property.Name}\". See inner exception for more information.", exception);
            }

            return(setting);
        }
示例#2
0
        // This doesn't actually work! [TestCase("-3.40282347E+38", typeof(Single), true, Single.MinValue)]
        // This doesn't actually work! [TestCase("-3.40282347E+38", typeof(Single?), true, Single.MinValue)]
        // This doesn't actually work! [TestCase("3.40282347E+38", typeof(Single), true, Single.MaxValue)]
        // This doesn't actually work! [TestCase("3.40282347E+38", typeof(Single?), true, Single.MaxValue)]
        public void TryConvert_SingleConversion_ResultIsEqual(String actual, Type type, Boolean isTrue, Object expected)
        {
            Object result = null;

            if (isTrue)
            {
                Assert.IsTrue(OptionTypeConverter.TryConvert(actual, type, out result));
            }
            else
            {
                Assert.IsFalse(OptionTypeConverter.TryConvert(actual, type, out result));
            }

            Assert.AreEqual(result, expected);
        }
示例#3
0
        public void TryConvert_DecimalConversion_ResultIsEqual(String actual, Type type, Boolean isTrue, Object expected)
        {
            Object result = null;

            if (isTrue)
            {
                Assert.IsTrue(OptionTypeConverter.TryConvert(actual, type, out result));
            }
            else
            {
                Assert.IsFalse(OptionTypeConverter.TryConvert(actual, type, out result));
            }

            Assert.AreEqual(result, expected == null ? expected : Decimal.Parse(expected.ToString()));
        }
 /// <summary>
 /// Convenient method to check if a parameter type is one of the supported type.
 /// </summary>
 /// <remarks>
 /// This convenient method checks if a parameter type is one of the supported type.
 /// </remarks>
 /// <param name="parameter">
 /// The parameter to check its type.
 /// </param>
 /// <param name="property">
 /// The property information that contain additional type information.
 /// </param>
 /// <returns>
 /// True, if type is supported and false otherwise.
 /// </returns>
 public static Boolean IsSupportedDataType(this ParameterObjectAttribute parameter, PropertyInfo property)
 {
     if (parameter is SwitchParameterAttribute)
     {
         return(property.PropertyType == typeof(Boolean) || property.PropertyType == typeof(Boolean?));
     }
     else if (parameter is OptionParameterAttribute)
     {
         return(OptionTypeConverter.IsSupportedType(property.PropertyType));
     }
     else if (parameter is VerbalParameterAttribute)
     {
         return(property.PropertyType == typeof(List <String>) || property.PropertyType == typeof(String[]));
     }
     else
     {
         return(false);
     }
 }
示例#5
0
        public void TryConvert_DateTimeConversion_ResultIsEqual(String actual, Type type, Boolean isTrue, Object expected)
        {
            Object result = null;

            if (expected != null && expected.ToString().StartsWith("current-date"))
            {
                expected = (Object)expected.ToString().Replace("current-date", DateTime.Today.ToString("dd.MM.yyyy"));
            }

            if (isTrue)
            {
                Assert.IsTrue(OptionTypeConverter.TryConvert(actual, type, out result));
            }
            else
            {
                Assert.IsFalse(OptionTypeConverter.TryConvert(actual, type, out result));
            }

            Assert.AreEqual(result, expected == null ? expected : DateTime.Parse(expected.ToString()));
        }
示例#6
0
        /// <summary>
        /// Performs command line argument processing.
        /// </summary>
        /// <remarks>
        /// This method performs the command line argument processing.
        /// </remarks>
        public void Process()
        {
            try
            {
                this.Initialize();

                List <String> parameters = new List <String>(this.Arguments);

                List <ArgumentProcessorSetting> processed = new List <ArgumentProcessorSetting>();

                while (parameters.Count > 0)
                {
                    String parameter = parameters[0];
                    parameters.RemoveAt(0);

                    ArgumentProcessorSetting setting;

                    if (!parameter.IsParameter())
                    {
                        if (this.TryFindSetting(out setting))
                        {
                            List <String> items = new List <String> {
                                (String)OptionTypeConverter.Convert(parameter, typeof(String))
                            };

                            while (true)
                            {
                                if (parameters.Count > 0)
                                {
                                    parameter = parameters[0];

                                    if (!parameter.IsParameter())
                                    {
                                        parameters.RemoveAt(0);
                                        items.Add((String)OptionTypeConverter.Convert(parameter, typeof(String)));
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                                else
                                {
                                    break;
                                }
                            }

                            if (setting.Property.PropertyType == typeof(List <String>))
                            {
                                setting.Property.SetValue(this.Instance, items);
                                processed.Add(setting);
                            }
                            else if (setting.Property.PropertyType == typeof(String[]))
                            {
                                setting.Property.SetValue(this.Instance, items.ToArray());
                                processed.Add(setting);
                            }
                            else
                            {
                                throw new VerbalViolationException(
                                          "A verbal parameter can only be a string list or a string array.");
                            }

                            continue;
                        }
                        else
                        {
                            throw new VerbalViolationException(
                                      "A property of type verbal parameter couldn't be determined.");
                        }
                    }
                    else
                    {
                        if (this.TryFindSetting(parameter, out setting))
                        {
                            if (setting.IsSwitchParameter())
                            {
                                setting.Property.SetValue(this.Instance, true);
                                processed.Add(setting);
                            }
                            else if (setting.IsOptionParameter())
                            {
                                String argument = String.Empty;
                                Int32  offset   = parameter.IndexOf((setting.Attribute as OptionParameterAttribute).Separator);

                                if (offset != -1)
                                {
                                    argument  = parameter.Substring(offset + 1);
                                    parameter = parameter.Substring(0, offset);
                                }
                                else if (parameters.Count > 0)
                                {
                                    argument = parameters[0];
                                    parameters.RemoveAt(0);
                                }

                                if (String.IsNullOrWhiteSpace(argument) || argument.IsParameter())
                                {
                                    throw new OptionViolationException(
                                              $"The argument of parameter \"{parameter}\" is missing.");
                                }

                                if (setting.HasCustomConverter)
                                {
                                    setting.Property.SetValue(this.Instance, setting.InvokeCustomConverter(parameter, argument, (setting.Attribute as OptionParameterAttribute).Delimiter));
                                }
                                // TODO: Remove obsolete code later on.
                                /* obsolete start */
                                else if (setting.Property.PropertyType.HasConverter())
                                {
                                    setting.Property.SetValue(this.Instance, setting.Property.PropertyType
                                                              .InvokeConverter(parameter, argument, (setting.Attribute as OptionParameterAttribute).Delimiter));
                                }
                                /* obsolete end */
                                else
                                {
                                    setting.Property.SetValue(this.Instance, OptionTypeConverter.Convert(argument, setting.Property.PropertyType));
                                }

                                processed.Add(setting);
                            }
                        }
                        else
                        {
                            throw new SupportViolationException(
                                      $"The parameter \"{parameter}\" is not supported.");
                        }
                    }
                }

                this.ValidateExclusiveProperties(processed);
                this.ValidateRequiredProperties(processed);
                this.ValidateDependencyProperties(processed);
            }
            catch (Exception exception)
            {
                exception.ThrowArgumentParserException();
            }
        }
示例#7
0
 public void Convert_UnsupportedType_ThrowsException(String actual, Type type)
 {
     Assert.Throws <NotSupportedException>(() => { OptionTypeConverter.Convert(actual, type); });
 }
示例#8
0
 public void Convert_SupportedType_ResultIsEqual(String actual, Type type, Object expected)
 {
     Assert.AreEqual(OptionTypeConverter.Convert(actual, type), expected);
 }
示例#9
0
 public void TryConvert_CheckUnsupportedType_ResultIsEqual(Type type)
 {
     Assert.IsFalse(OptionTypeConverter.IsSupportedType(type));
 }