Пример #1
0
        internal static bool SetOptionValue <T>(T instance, MappingInfo mappingInfo, IDictionary <string, CommandLineArgument> arguments)
        {
            foreach (var name in mappingInfo.GetNames())
            {
                if (arguments.TryGetValue(name, out var argument))
                {
                    if (argument.Value != null)
                    {
                        throw new CommandLineArgumentException($"The option '{argument.Name}' was specified with a value. This is not allowed for option.")
                              {
                                  Reason = ErrorReason.OptionWithValue
                              }
                    }
                    ;

                    mappingInfo.PropertyInfo.SetValue(instance, true, null);
                    mappingInfo.CommandLineArgument = argument;
                    argument.Mapped = true;

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

                    return(true);
                }
            }

            return(false);
        }
Пример #2
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);
        }