Пример #1
0
        private static void ParseOverrideConfig(Arguments arguments, string[] values)
        {
            if (values == null || values.Length == 0)
            {
                return;
            }

            var parser = new OverrideConfigOptionParser();

            // key=value
            foreach (var keyValueOption in values)
            {
                var keyAndValue = QuotedStringHelpers.SplitUnquoted(keyValueOption, '=');
                if (keyAndValue.Length != 2)
                {
                    throw new WarningException($"Could not parse /overrideconfig option: {keyValueOption}. Ensure it is in format 'key=value'.");
                }

                var optionKey = keyAndValue[0].ToLowerInvariant();
                if (!parser.SupportedProperties.Contains(optionKey))
                {
                    throw new WarningException($"Could not parse /overrideconfig option: {keyValueOption}. Unsuported 'key'.");
                }
                else
                {
                    parser.SetValue(optionKey, keyAndValue[1]);
                }
            }
            arguments.OverrideConfig = parser.GetConfig();
        }
        internal void SetValue(string key, string value)
        {
            if (!SupportedProperties.Contains(key))
            {
                return;
            }

            var unwrappedText = QuotedStringHelpers.UnquoteText(value);

            foreach (var pi in SupportedProperties[key])
            {
                Type unwrapped = Nullable.GetUnderlyingType(pi.PropertyType);
                if (unwrapped == null)
                {
                    unwrapped = pi.PropertyType;
                }

                if (unwrapped == typeof(string))
                {
                    pi.SetValue(_lazyConfig.Value, unwrappedText);
                }
                else if (unwrapped.IsEnum)
                {
                    try
                    {
                        var parsedEnum = Enum.Parse(unwrapped, unwrappedText);
                        pi.SetValue(_lazyConfig.Value, parsedEnum);
                    }
                    catch (ArgumentException)
                    {
                        var sb = new System.Text.StringBuilder();

                        sb.Append($"Could not parse /overrideconfig option: {key}={value}.");
                        sb.AppendLine(" Ensure that 'value' is valid for specified 'key' enumeration: ");
                        foreach (var name in Enum.GetNames(unwrapped))
                        {
                            sb.AppendLine(name);
                        }

                        throw new WarningException(sb.ToString());
                    }
                }
                else if (unwrapped == typeof(int))
                {
                    if (int.TryParse(unwrappedText, out int parsedInt))
                    {
                        pi.SetValue(_lazyConfig.Value, parsedInt);
                    }
                    else
                    {
                        throw new WarningException($"Could not parse /overrideconfig option: {key}={value}. Ensure that 'value' is valid integer number.");
                    }
                }
                else if (unwrapped == typeof(bool))
                {
                    if (bool.TryParse(unwrappedText, out bool parsedBool))
                    {
                        pi.SetValue(_lazyConfig.Value, parsedBool);
                    }
                    else
                    {
                        throw new WarningException($"Could not parse /overrideconfig option: {key}={value}. Ensure that 'value' is 'true' or 'false'.");
                    }
                }
            }
        }
Пример #3
0
        public Arguments ParseArguments(string commandLineArguments)
        {
            var arguments = QuotedStringHelpers.SplitUnquoted(commandLineArguments, ' ');

            return(ParseArguments(arguments));
        }