Esempio n. 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);
        }
Esempio n. 2
0
 public void Convert_UnsupportedType_ThrowsException(String actual, Type type)
 {
     Assert.Throws <NotSupportedException>(() => { OptionTypeConverter.Convert(actual, type); });
 }
Esempio n. 3
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();
            }
        }
Esempio n. 4
0
 public void Convert_SupportedType_ResultIsEqual(String actual, Type type, Object expected)
 {
     Assert.AreEqual(OptionTypeConverter.Convert(actual, type), expected);
 }