Beispiel #1
0
        internal static bool SetArgumentValue <T>(T instance, MappingInfo mappingInfo, IDictionary <string, CommandLineArgument> arguments)
        {
            PropertyInfo         propertyInfo = mappingInfo.PropertyInfo;
            CommandLineAttribute attribute    = mappingInfo.CommandLineAttribute;
            var  count = 0;
            bool trim  = attribute.TrimQuotation();

            CommandLineArgument argument;
            string stringValue;

            foreach (var name in mappingInfo.GetNames())
            {
                if (arguments.TryGetValue(name, out argument))
                {
                    if (argument.Value == null)
                    {
                        throw new CommandLineArgumentException($"The value of the argument '{argument.Name}' was not specified.")
                              {
                                  Reason = ErrorReason.ArgumentWithoutValue
                              }
                    }
                    ;

                    stringValue = GetValue(argument.Value, trim);
                    propertyInfo.SetValue(instance, ConvertValue(propertyInfo.PropertyType, stringValue, (t, v) => CreateErrorMessage(t, v, name)), null);
                    mappingInfo.CommandLineArgument = argument;
                    argument.Mapped = true;

                    if (!mappingInfo.IsShared())
                    {
                        arguments.Remove(name);
                    }

                    count++;
                }
            }

            if (count == 0 && TryGetByIndex(arguments, mappingInfo, out var entry))
            {
                argument = entry.Value;
                if (argument != null && argument.Value == null)
                {
                    stringValue = GetValue(argument.Name, trim);

                    propertyInfo.SetValue(instance, ConvertValue(propertyInfo.PropertyType, stringValue, (t, v) => CreateErrorMessage(t, v, attribute.Name)), null);
                    mappingInfo.CommandLineArgument = argument;
                    arguments.Remove(entry.Key);
                    count++;
                }
            }

            if (count > 1)
            {
                throw new AmbiguousCommandLineArgumentsException($"The value for the argument '{mappingInfo.Name}' was specified multiple times.");
            }

            if (count == 0 && attribute.IsRequired())
            {
                throw new MissingCommandLineArgumentException(mappingInfo.Name);
            }

            return(count > 0);
        }
Beispiel #2
0
        private static bool TryGetByIndex(IDictionary <string, CommandLineArgument> arguments, MappingInfo mappingInfo, out KeyValuePair <string, CommandLineArgument> argument)
        {
            var index = mappingInfo.CommandLineAttribute.GetIndex();

            if (index >= 0)
            {
                foreach (var commandLineArgument in arguments)
                {
                    if (commandLineArgument.Value.Index == index)
                    {
                        var betterNameMatch = mappingInfo.GetNameMatch(commandLineArgument.Value.Name);
                        if (betterNameMatch == null)
                        {
                            argument = commandLineArgument;
                            return(true);
                        }

                        break;
                    }
                }
            }

            argument = new KeyValuePair <string, CommandLineArgument>();
            return(false);
        }